Reputation: 169
I am new to spring boot and I am adding custom exceptions to my controller through controllerAdvisor. In MyController.class, I am doing three operations
Below mentioned class is MyController.class.
@RestController
public class MyController {
@Autowired
private TicketServiceImpl ticketService;
@GetMapping(/ticket)
public ResponseEntity<?> getAllTickets() {
List<Ticket> list=(List<Ticket>)ticketService.findAll();
return new ResponseEntity<>(list, HttpStatus.OK);
}
@GetMapping(Constant.BASE_URL+"/{id}")
public ResponseEntity<Ticket> getTicketById(@PathVariable("id") long id) {
Optional<Ticket> ticketData = Optional.ofNullable(ticketService.get(id));
if(!ticketData.isPresent()){
throw new RecordNotFoundException("id-" + id);
}
return new ResponseEntity<>(ticketData.get(), HttpStatus.OK);
}
@PostMapping(/ticket)
public ResponseEntity<Ticket> createTicket(@RequestBody Ticket ticket) {
ticketService.save(ticket);
return new ResponseEntity<>(ticket, HttpStatus.CREATED);
}
}
Below class is my custom exception handle class where I am handling my custom exceptions.
@SuppressWarnings({"unchecked","rawtypes"})
@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
List<String> details = new ArrayList<>();
details.add(ex.getLocalizedMessage());
ErrorResponse error = new ErrorResponse("Server Error", details);
return new ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(RecordNotFoundException.class)
public final ResponseEntity<Object> handleUserNotFoundException(RecordNotFoundException ex, WebRequest request) {
List<String> details = new ArrayList<>();
details.add(ex.getLocalizedMessage());
ErrorResponse error = new ErrorResponse("Record Not Found", details);
return new ResponseEntity(error, HttpStatus.NOT_FOUND);
}
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
List<String> details = new ArrayList<>();
for(ObjectError error : ex.getBindingResult().getAllErrors()) {
details.add(error.getDefaultMessage());
}
ErrorResponse error = new ErrorResponse("Validation Failed", details);
return new ResponseEntity(error, HttpStatus.BAD_REQUEST);
}
}
The below class is specific for not found exceptions.
@ResponseStatus(HttpStatus.NOT_FOUND)
public class RecordNotFoundException extends RuntimeException {
public RecordNotFoundException(String exception) {
super(exception);
}
}
In application.properties, I am adding these three conditions
server.error.whitelabel.enabled=false
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
When I hit my server with GET API http://localhost:8080/ticke .It should throw bad request exception but its throwing Not found "404" exception and when I hit GET API http://localhost:8080/ticket/24, in this my id is not created. Hence, it should return resource not found exception.But, its returning, 500 internal server error.
Please advise on this.
Upvotes: 0
Views: 206
Reputation: 11992
The error you are getting is because Spring was not able to find the endpoint you are looking for. So it does not apply any controller advices to it, since it did not find a controller that maps to the requested URL.
Here is a small article explaining it in a little more detail.
Upvotes: 1