2013-11-23 2 views
0

내 POJO와 REST 호출을 매핑하려고합니다. POJO와는 다음과 같습니다직렬화/매핑 문제 Dropwizard/저지

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)에서 전화를 걸려고 시도했는데 예상했던대로 "application/json"이 아닌 "text/javascript"가 반환되는 응용 프로그램 유형입니다. 그게 내 문제라고 생각해.

ObjectMapper에서 실제로 "application/json"이고 "text/javascript"가 아닌지 확인할 수있는 방법이 있습니까? 나는 String.class를 사용하여 시도한 다음 Json 객체를 얻는다.

나의 목적은 Jersey Client에서 자동 매핑을 사용하는 것입니다.

어떤 조언이나 조언을 주셔서 감사합니다.

답변

2

시도 추가 주석 @Produces (MediaType.APPLICATION_JSON)

0

이 작업을 시도 할 수 있습니다 :

@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); 
    } 

} 

앱 실행()에 추가

// Serializer 
environment.jersey().register(new YourTestBodyWriter()); 

를 응용 프로그램에서 정상적인 방법 이잖아.