[英]Serialization/mapping issue Dropwizard/Jersey
我正在嘗試使用我的POJO映射REST調用。 POJO看起來像這樣:
public class ResultWrapper implements Serializable{
private int total;
private List<Movies> movies; ... getters and setters
在我使用的電話中:
WebResource webResource = client.resource(RequestURI + URLEncoder.encode(movie, "UTF-8"));
ResultWrapper result = webResource.accept("application/json").get(ResultWrapper.class);
錯誤:
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
客戶是澤西島客戶。 我試過從Chrome(Postman)發出呼叫,並且它返回的應用程序類型是“text / javascript”,而不是“application / json”,正如人們所期望的那樣? 我認為這將是我的問題。
有沒有什么方法可以讓ObjectMapper解決它實際上是“application / json”而不是“text / javascript”。 我已經嘗試使用String.class然后我得到了Json對象就好了。
我的目的是使用Jersey客戶端的自動映射。
感謝您的任何提示或建議。
嘗試添加注釋@Produces (MediaType.APPLICATION_JSON )
你可以試試這個:
@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);
}
}
添加你的應用程序run():
// Serializer
environment.jersey().register(new YourTestBodyWriter ());
這是你的應用程序的正常方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.