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