Skip to content

Commit 97f56f3

Browse files
askrakeshpivovarit
authored andcommitted
BAEL-1149 - Spring ResponseStatusException (eugenp#3648)
1 parent 4d64a12 commit 97f56f3

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.execption;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.web.bind.annotation.DeleteMapping;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.PutMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
import org.springframework.web.server.ResponseStatusException;
11+
12+
@RestController
13+
public class ActorController {
14+
15+
@Autowired
16+
ActorService actorService;
17+
18+
@GetMapping("/actor/{id}")
19+
public String getActorName(@PathVariable("id") int id) {
20+
try {
21+
return actorService.getActor(id);
22+
} catch (ActorNotFoundException ex) {
23+
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Actor Not Found", ex);
24+
}
25+
}
26+
27+
@DeleteMapping("/actor/{id}")
28+
public String getActor(@PathVariable("id") int id) {
29+
return actorService.removeActor(id);
30+
}
31+
32+
@PutMapping("/actor/{id}/{name}")
33+
public String updateActorName(@PathVariable("id") int id, @PathVariable("name") String name) {
34+
try {
35+
return actorService.updateActor(id, name);
36+
} catch (ActorNotFoundException ex) {
37+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Provide correct Actor Id", ex);
38+
}
39+
}
40+
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.execption;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Actor Not Found")
7+
public class ActorNotFoundException extends Exception {
8+
private static final long serialVersionUID = 1L;
9+
10+
public ActorNotFoundException(String errorMessage) {
11+
super(errorMessage);
12+
}
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.execption;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.web.server.ResponseStatusException;
9+
10+
@Service
11+
public class ActorService {
12+
List<String> actors = Arrays.asList("Jack Nicholson", "Marlon Brando", "Robert De Niro", "Al Pacino", "Tom Hanks");
13+
14+
public String getActor(int index) throws ActorNotFoundException {
15+
if (index >= actors.size()) {
16+
throw new ActorNotFoundException("Actor Not Found in Repsoitory");
17+
}
18+
return actors.get(index);
19+
}
20+
21+
public String updateActor(int index, String actorName) throws ActorNotFoundException {
22+
if (index >= actors.size()) {
23+
throw new ActorNotFoundException("Actor Not Found in Repsoitory");
24+
}
25+
actors.set(index, actorName);
26+
return actorName;
27+
}
28+
29+
public String removeActor(int index) {
30+
if (index >= actors.size()) {
31+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Actor Not Found in Repsoitory");
32+
}
33+
return actors.remove(index);
34+
}
35+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.execption;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
6+
import org.springframework.context.annotation.ComponentScan;
7+
8+
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
9+
@ComponentScan(basePackages = { "com.baeldung.execption" })
10+
public class SpringExceptionApplication {
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringExceptionApplication.class, args);
13+
}
14+
}

0 commit comments

Comments
 (0)