Skip to content

Commit

Permalink
Handle UnmarshalException as Bad Request
Browse files Browse the repository at this point in the history
If UnmarshalException is thrown (which happens for example when the xml
is invalid)  then a WebApplicationException is thrown with status code
400 Bad Request.

This is also how resteasy-reactive-jackson handles invalid JSON.
  • Loading branch information
ketola committed Mar 23, 2024
1 parent 359a8ab commit 98c138c
Showing 1 changed file with 4 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Providers;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.UnmarshalException;
import jakarta.xml.bind.Unmarshaller;

import org.jboss.resteasy.reactive.common.util.StreamUtil;
Expand Down Expand Up @@ -73,6 +75,8 @@ protected Object unmarshal(InputStream entityStream, Class<Object> type) {
JAXBElement<Object> item = getUnmarshall(type)
.unmarshal(new StreamSource(entityStream), type);
return item.getValue();
} catch (UnmarshalException e) {
throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 98c138c

Please sign in to comment.