0

I am getting NotFoundException while trying to implement custom exception handling in spring-boot rest application.

The code was working fine when I was using MVC (using @ControllerAdvice) annotations but not sure when I am sending a data which is violating the constraint mentioned in entity(pojo class) it is throwing only NotFoundException (for all validation failure) but not the MethodViolationException or ConstraintViolationException

I am not able to send the message for that particular violation.

Not sure where I am making this mistake. Please help

Code:

@POST
@Path("/customers/add") 
public Response addCustomer(@Valid customer cust) 
{

// Rest of the code

} 

POJO:

@Entity
@Table(name="cust")
public class Customer
{
  @NotNull
  @Size(min=1,max=50,message ="invalid name") 
  String name;

}

Exception Handler:

@Provider
public class CustomHandler implements ExceptionMapper<Exception>
{
 public Response toResponse(Exception ex) 
 {
  if(ex instanceOf ConstraintViolationException) 
  {
    Do something
  } 
} 

**UPDATE 1

If I enable the send_error_in_response i am getting the message for this but not sure why my custom exception handler is not able to catch this exception and only throwing NotFoundException

newcoder
  • 464
  • 9
  • 23
  • Read the [Bean Validation chapter](https://jersey.github.io/documentation/latest/bean-validation.html) in the Jersey docs. – Paul Samsotha Dec 28 '18 at 16:35
  • Are you getting a 404 for all error codes, or just the bean validation errors? – Paul Samsotha Dec 29 '18 at 04:13
  • @Paul Samsotha yes right now i am testing only for validation and getting this 404 error. Where i am throwing exception explicitly there i am getting it in expected format in response but not for any validation error – newcoder Dec 31 '18 at 06:48
  • _"but not sure why my custom exception handler is not able to catch this exception"_ - It's because Jersey already has [a mapper](https://github.com/eclipse-ee4j/jersey/blob/2.28-RELEASE/ext/bean-validation/src/main/java/org/glassfish/jersey/server/validation/internal/ValidationExceptionMapper.java#L50). I think you are getting the not found because of [this (but not sure)](https://stackoverflow.com/a/36600434/2587435) – Paul Samsotha Dec 31 '18 at 07:23
  • I have added a separate handler for ConstraintViolationException which resolved my issue – newcoder Jan 01 '19 at 11:50

1 Answers1

0

Try Handling Exception Using:

@ControllerAdvice
@RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

  @ExceptionHandler(StudentNotFoundException)
  public final ResponseEntity<ErrorDetails> handleUserNotFoundException(StudentNotFoundException ex, WebRequest request) {
    ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(),
        request.getDescription(false));
    return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
  }

For more information you might want to refer http://www.springboottutorial.com/spring-boot-validation-for-rest-services

sah1
  • 380
  • 1
  • 6
  • 23
  • It was working when I was using @ControllerAdvice but it's not working when I am trying to implement with jersey. – newcoder Dec 28 '18 at 09:14