Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessageBodyWriter not found for media type=application/xml

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?

like image 241
Brahim LAMJAGUAR Avatar asked Jan 30 '23 05:01

Brahim LAMJAGUAR


1 Answers

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();
like image 96
ialex Avatar answered Feb 02 '23 10:02

ialex