MessageBodyWriter not found for media type=application/xml
12,612
Solution 1
Do you have the jaxb dependency in your project?
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>x.x.x</version>
</dependency>
If yes, perhaps you could try to wrap the exception.getErrors()
(which appears to be a Map?) into a GenericEntity
and giving that entity to the response:
GenericEntity<Map<?, ?>> entity = new GenericEntity..
return Response.status(Status.X).entity(entity).type(MediaType.APPLICATION_XML).build();
Solution 2
Adding GenericEntity<>
, as described by ialex
, solved my problem. This is what I did:
GenericEntity<List<?>> genericEntity = new GenericEntity<List<?>> (exception.getErrors()) {};
Response.status(Status.BAD_REQUEST).entity(genericEntity).build();
Comments
-
Brahim LAMJAGUAR almost 2 years
I want to make a REST API Response in two formats depending on the HttpHeaders of the request :
@Context HttpHeaders headers; public Response toResponse(MyValidationException exception) { if(headers.getMediaType().toString().equals(MediaType.APPLICATION_XML)){ return Response.status(Status.BAD_REQUEST).entity(exception.getErrors()).type(MediaType.APPLICATION_XML).build(); }else{ return Response.status(Status.BAD_REQUEST).entity(exception.getErrors()).type(MediaType.APPLICATION_JSON).build(); }}
It's working for the MediaType.APPLICATION_JSON, but for MediaType.APPLICATION_XML I get the following Error :
org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo MessageBodyWriter not found for media type=application/xml, type=class java.util.HashMap$EntrySet, genericType=class java.util.HashMap$EntrySet.
Any Idea to solve this problem?
-
Brahim LAMJAGUAR over 6 yearsTo cast the exception I wrote : GenericEntity<Map<String,String>> entity = (GenericEntity<Map<String,String>>) exception.getErrors(); but I got an exception : java.lang.ClassCastException: java.util.HashMap cannot be cast to javax.ws.rs.core.GenericEntity
-
ialex over 6 yearsDon't
cast
the object directly, just create it anew:GenericEntity<Map<String,String>> entity = new GenericEntity<Map<String,String>>(exception.getErrors()){};
-
Brahim LAMJAGUAR over 6 yearsI tried but I have the same first problem of the post :/