我希望能够根据请求的HttpHeaders,以两种格式生成REST API响应:
它适用于MediaType.APPLICATION_JSON,但对于MediaType.APPLICATION_XML,我会得到以下错误:
有没有解决这个问题的想法?
@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();
}}
它适用于MediaType.APPLICATION_JSON,但对于MediaType.APPLICATION_XML,我会得到以下错误:
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.
有没有解决这个问题的想法?
GenericEntity<Map<String,String>> entity = new GenericEntity<Map<String,String>>(exception.getErrors()){};
- ialex