0

I created a rest-api with openapi-codegen. As a parameter there is an object that contains two ENUMs.

e.g.

public enum DocumentType {
FOTO_ID("PHOTO_ID");
[...]
}

If i send a request with a DocumentType other than "PHOTO_ID" I get the following Response

Cannot construct instance of `[...]`, problem: Unexpected value '[...]'
at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 2, column: 21] (through reference chain: [...])

which is correct, but i would like to catch it and send my own ErrorResponse.

I could not find a way to do that.

AdrianL
  • 335
  • 2
  • 18

1 Answers1

0

If somebody has a similar problem, I just found a solution:

You have to set your own ExceptionMapper for JsonMappingException. It is import to set a Priority, otherwise your Mapper will not be used.

    @Named
    @Singleton
    @Provider
    @Priority(1)
    public class JsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException> {

    @Override
    public Response toResponse(JsonMappingException exception) {
        [...]
        }
    }

Credit to: Jersey unable to catch any Jackson Exception for pointing out the priority topic.

AdrianL
  • 335
  • 2
  • 18