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?