https://github.com/bmarwell/jaxrs-jsonb-exceptionmapper
The JAX-RS spec does not specify ExceptionMappers for MessageBodyReader instances, the outcome will be dependent on the container. This example shows how to catch such an exception anyway.
https://github.com/bmarwell/jaxrs-jsonb-exceptionmapper
Last synced: 2 months ago
JSON representation
The JAX-RS spec does not specify ExceptionMappers for MessageBodyReader instances, the outcome will be dependent on the container. This example shows how to catch such an exception anyway.
- Host: GitHub
- URL: https://github.com/bmarwell/jaxrs-jsonb-exceptionmapper
- Owner: bmarwell
- License: apache-2.0
- Created: 2020-08-14T10:46:11.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-14T13:10:28.000Z (almost 5 years ago)
- Last Synced: 2025-04-02T01:11:09.351Z (2 months ago)
- Language: Java
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
- License: LICENSE
Awesome Lists containing this project
README
= jaxrs-jsonb-exceptionmapper
The JAX-RS spec does not specify ExceptionMappers for MessageBodyReader instances, the outcome will be dependent on the container.
This example shows how to catch such an exception anyway.== The problem
If you have a request body in a `@POST` request and want it to have parsed into a java value object, the MessageBodyReader supplied by your container vendor should take care of this.
However, the exception thrown on missing fields is dependent on the vendor’s implementation:
The exact signature is:[source,java]
----
public T readFrom(
Class type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap httpHeaders,
InputStream entityStream) throws
java.io.IOException,
javax.ws.rs.WebApplicationException;
----Now, the vendor can decide to wrap the `JsonbException` into a `WebApplicationException`.
Why is this important?
Well, now your `JsonbExceptionMapper` will not see the exception.
You will get an empty response entity, no matter how you define your `JsonbExceptionMapper`.== The solution
At least for Open Liberty, a `BadRequestException` is thrown.
The idea here is to catch that exception, peek into the cause (if it exists) and map this exception.== How to run this project
Execute `./mvnw clean liberty:dev`.
A OpenLiberty server will boot up with the application.
Just press `Enter` to execute the IT test case.== Example implementations
=== OpenLiberty
Jsonb MessageBodyReader implementation: https://github.com/OpenLiberty/open-liberty/blob/integration/dev/com.ibm.ws.org.apache.cxf.cxf.rt.frontend.jaxrs.3.2/src/com/ibm/ws/jaxrs21/providers/json/JsonBProvider.java[jaxrs-2.1 JsonBProvider.java]
=== Payara
Jsonb MessageBodyReader implementation: https://github.com/payara/Payara/blob/master/appserver/ejb/ejb-http-remoting/endpoint/src/main/java/fish/payara/ejb/http/protocol/rs/JsonbInvokeMethodMessageBodyReader.java[rs JsonbInvokeMethodMessageBodyReader.java]