这是我的代码。我正在调用RESTful服务,它返回实体类型MexpCategoriesDTOWrapper的JSON。
String url = "http://" + servlet.getServletContext().getAttribute(ServicesManagerServlet.MPDS_HOST_NAME) +
":" + servlet.getServletContext().getAttribute(ServicesManagerServlet.MPDS_PORT_NAME) + "/mexpCategoryRates";
ClientConfig clientConfig = new DefaultClientConfig();
Client client = Client.create(clientConfig);
client.addFilter(new LoggingFilter(System.out));
WebResource webResource = client.resource(url);
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
.get(ClientResponse.class);
MexpCategoriesDTOWrapper mcdtow = response.getEntity(MexpCategoriesDTOWrapper.class);
这是堆栈跟踪
11:05:41,272 INFO [SamExceptionHandler] Exception while processing request
com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class com.pronto.mpds.wrapper.MexpCategoriesDTOWrapper, and Java type class com.pronto.mpds.wrapper.MexpCategoriesDTOWrapper, and MIME media type application/json was not found
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:561)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:517)
at com.rc.mexp.action.enrollment.RateCardAction.execute(RateCardAction.java:48)
at com.rc.mexp.action.SamAction.execute(SamAction.java:57)
at com.rc.commonbusinesswebapp.action.BusinessWebappLoggedAction.executeWithLogging(BusinessWebappLoggedAction.java:66)
at com.rc.commonbusinesswebapp.action.BusinessWebappLoggedAction.execute(BusinessWebappLoggedAction.java:46)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at com.rc.commonbusinesswebapp.controller.BusinessWebappRequestProcessor.processActionPerform(BusinessWebappRequestProcessor.java:202)
at com.rc.commonbusinesswebapp.controller.BusinessWebappRequestProcessor.process(BusinessWebappRequestProcessor.java:522)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.rc.commonbusinesswebapp.servlet.filter.GlobalRootManager.doFilter(GlobalRootManager.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.rc.model.persistence.proxyhandler.TransactionHandler.invoke(TransactionHandler.java:44)
at com.sun.proxy.$Proxy17.doFilter(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.rc.model.persistence.proxyhandler.SessionHandler.invoke(SessionHandler.java:42)
at com.sun.proxy.$Proxy17.doFilter(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.rc.model.persistence.proxyhandler.SessionHandler.invoke(SessionHandler.java:42)
at com.sun.proxy.$Proxy17.doFilter(Unknown Source)
at com.rc.commonwebapp.servlet.filter.SessionTXManager.doFilter(SessionTXManager.java:134)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:695)
我在ClientResponse中跟踪它:
private <T> T getEntity(Class<T> c, Type type) {
if (getStatus() == 204) {
throw new UniformInterfaceException(this);
}
MediaType mediaType = getType();
if (mediaType == null) {
mediaType = MediaType.APPLICATION_OCTET_STREAM_TYPE;
}
final MessageBodyReader<T> br = workers.getMessageBodyReader(
c, type,
EMPTY_ANNOTATIONS, mediaType);
if (br == null) {
close();
String message = "A message body reader for Java class " + c.getName() +
", and Java type " + type +
", and MIME media type " + mediaType + " was not found";
LOGGER.severe(message);
Map<MediaType, List<MessageBodyReader>> m = workers.getReaders(mediaType);
LOGGER.severe("The registered message body readers compatible with the MIME media type are:\n" +
workers.readersToString(m));
throw new ClientHandlerException(message);
}
...
}
调用MessageBodyFactory.getMessageBodyReader()方法:
public <T> MessageBodyReader<T> getMessageBodyReader(Class<T> c, Type t,
Annotation[] as,
MediaType mediaType) {
MessageBodyReader reader;
if(!customReaderProviders.isEmpty()) {
reader = _getMessageBodyReader(c, t, as, mediaType, customReaderProviders);
if(reader != null)
return reader;
}
reader = _getMessageBodyReader(c, t, as, mediaType, readerProviders);
return reader;
}
customReaderProviders
为空。当我查看MessageBOdyFactory initReaders()方法时,我没有看到任何在我的类的customReaderProviders中注册的内容。
private void initReaders() {
this.customReaderProviders = new KeyComparatorHashMap<MediaType, List<MessageBodyReader>>(
MEDIA_TYPE_COMPARATOR);
this.readerProviders = new KeyComparatorHashMap<MediaType, List<MessageBodyReader>>(
MEDIA_TYPE_COMPARATOR);
if(deprecatedProviderPrecedence) {
initReaders(this.readerProviders, providerServices.getProvidersAndServices(MessageBodyReader.class));
} else {
initReaders(this.customReaderProviders, providerServices.getProviders(MessageBodyReader.class));
initReaders(this.readerProviders, providerServices.getServices(MessageBodyReader.class));
}
}
我应该在某个地方传递一些我没有传入的东西吗?
我已经确认我在我的客户端中使用了与服务中使用的相同的jaxb jar(jaxb-impl-2.2.3-1)。没有快乐。
我已确认从服务返回正确的输出,无论是从命令行调用CURL还是在运行客户端时调试输出。