BAEL-1149 - Spring ResponseStatusException (#3648)
This commit is contained in:
parent
4d64a12271
commit
97f56f33f6
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.execption;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@RestController
|
||||
public class ActorController {
|
||||
|
||||
@Autowired
|
||||
ActorService actorService;
|
||||
|
||||
@GetMapping("/actor/{id}")
|
||||
public String getActorName(@PathVariable("id") int id) {
|
||||
try {
|
||||
return actorService.getActor(id);
|
||||
} catch (ActorNotFoundException ex) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Actor Not Found", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/actor/{id}")
|
||||
public String getActor(@PathVariable("id") int id) {
|
||||
return actorService.removeActor(id);
|
||||
}
|
||||
|
||||
@PutMapping("/actor/{id}/{name}")
|
||||
public String updateActorName(@PathVariable("id") int id, @PathVariable("name") String name) {
|
||||
try {
|
||||
return actorService.updateActor(id, name);
|
||||
} catch (ActorNotFoundException ex) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Provide correct Actor Id", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.execption;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Actor Not Found")
|
||||
public class ActorNotFoundException extends Exception {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public ActorNotFoundException(String errorMessage) {
|
||||
super(errorMessage);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.execption;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
@Service
|
||||
public class ActorService {
|
||||
List<String> actors = Arrays.asList("Jack Nicholson", "Marlon Brando", "Robert De Niro", "Al Pacino", "Tom Hanks");
|
||||
|
||||
public String getActor(int index) throws ActorNotFoundException {
|
||||
if (index >= actors.size()) {
|
||||
throw new ActorNotFoundException("Actor Not Found in Repsoitory");
|
||||
}
|
||||
return actors.get(index);
|
||||
}
|
||||
|
||||
public String updateActor(int index, String actorName) throws ActorNotFoundException {
|
||||
if (index >= actors.size()) {
|
||||
throw new ActorNotFoundException("Actor Not Found in Repsoitory");
|
||||
}
|
||||
actors.set(index, actorName);
|
||||
return actorName;
|
||||
}
|
||||
|
||||
public String removeActor(int index) {
|
||||
if (index >= actors.size()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Actor Not Found in Repsoitory");
|
||||
}
|
||||
return actors.remove(index);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.execption;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
|
||||
@ComponentScan(basePackages = { "com.baeldung.execption" })
|
||||
public class SpringExceptionApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringExceptionApplication.class, args);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue