[英]Serialization/mapping issue Dropwizard/Jersey
I am trying to map a REST-call with my POJO. 我正在尝试使用我的POJO映射REST调用。 The POJO looks like this:
POJO看起来像这样:
public class ResultWrapper implements Serializable{
private int total;
private List<Movies> movies; ... getters and setters
In the call I use: 在我使用的电话中:
WebResource webResource = client.resource(RequestURI + URLEncoder.encode(movie, "UTF-8"));
ResultWrapper result = webResource.accept("application/json").get(ResultWrapper.class);
The error: 错误:
com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class models.ResultWrapper, and Java type class models.ResultWrapper, and MIME media type text/javascript; charset=ISO-8859-1 was not found
Client is a Jersey client. 客户是泽西岛客户。 I have tried making the call from Chrome (Postman) and it sais that the application type returned is "text/javascript", not "application/json" as one would expect?
我试过从Chrome(Postman)发出呼叫,并且它返回的应用程序类型是“text / javascript”,而不是“application / json”,正如人们所期望的那样? I think that would be my problem.
我认为这将是我的问题。
Is there any way I can get the ObjectMapper to resolve that it is actually "application/json" and not "text/javascript". 有没有什么方法可以让ObjectMapper解决它实际上是“application / json”而不是“text / javascript”。 I have tried using String.class and then I get the Json object just fine.
我已经尝试使用String.class然后我得到了Json对象就好了。
My purpose is to use automatic mapping from Jersey Client. 我的目的是使用Jersey客户端的自动映射。
Thanks for any tips or advice. 感谢您的任何提示或建议。
尝试添加注释@Produces (MediaType.APPLICATION_JSON )
You can try this: 你可以试试这个:
@Provider
@Produces(application/json)
public class YourTestBodyWriter implements MessageBodyWriter<ResultWrapper> {
private static final Logger LOG = LoggerFactory.getLogger(YourTestBodyWriter.class);
@Override
public boolean isWriteable(
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType)
{
return ResultWrapper.class.isAssignableFrom(type);
}
@Override
public long getSize(
ResultWrapper t,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType)
{
return -1;
}
@Override
public void writeTo(
ResultWrapper t,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException, WebApplicationException
{
String message = t.someMethod()
entityStream.write(message.getBytes(Charsets.UTF_8));
LOG.info(message);
}
}
Add in your app run(): 添加你的应用程序run():
// Serializer
environment.jersey().register(new YourTestBodyWriter ());
Thats the normal way in your application. 这是你的应用程序的正常方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.