我最近开始使用jersey 1.6来开发Rest Web服务。我有两个生产JSON的服务。我为它们设置了MessageBodyWriters来处理Object to Json转换。我完成了我的第一个Web服务,一切都很顺利,正如预期的那样。当我试图实现我的第二个服务时,我遇到了一个泽西岛问题,该问题是我为第一个Web服务而不是我创建的新服务设置的messagebodywriter提供程序。这会生成一个类强制转换异常。如果我从第一个服务中删除提供程序,以便只有第二个服务器,它按预期工作。以下是我的设置:
服务#1
@POST
@Path("/medical")
@Produces(MediaType.APPLICATION_JSON)
public RateListWrapper getMedicalRate(@FormParam("zip") String zip, @FormParam("cpt") List<String> cptList, @FormParam("dos") String dos, @FormParam("mod1") List<String> mod1List,
@FormParam("mod2") List<String> mod2List, @FormParam("mod3") List<String> mod3List, @FormParam("mod4") List<String> mod4List, @FormParam("units") List<String> unitsList,
@FormParam("pos") List<String> posList) {
}
服务#1提供商
@Provider
public class RateListWraperProvider implements MessageBodyWriter<RateListWrapper>{
@Override
public long getSize(RateListWrapper t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
//System.out.println("Get Size");
return -1;
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
//System.out.println("Is Writable");
return true;
}
@Override
public void writeTo(RateListWrapper t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
final GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy-MM-dd");
Gson gson = gsonBuilder.serializeNulls().create();
entityStream.write(gson.toJson(t).getBytes());
}
}
服务#2
@POST
@Path("/medical/post")
@Produces(MediaType.APPLICATION_JSON)
public RbrvsListWrapper medicalRatePost(@FormParam("zip") String zip, @FormParam("facilitytype") String facilityType, @FormParam("cpt") List<String> cptList, @FormParam("dos") String dos, @FormParam("mod1") List<String> mod1List) {
return getMedicalRate(zip, facilityType, cptList, dos, mod1List);
}
服务#2提供商
@Provider
public class RbrvsListWrapperProvider implements MessageBodyWriter<RbrvsListWrapper>{
@Override
public long getSize(RbrvsListWrapper t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
//System.out.println("Get Size");
return -1;
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
//System.out.println("Is Writable");
return true;
}
@Override
public void writeTo(RbrvsListWrapper t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
final GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy-MM-dd");
Gson gson = gsonBuilder.serializeNulls().create();
entityStream.write(gson.toJson(t).getBytes());
}
}
这是我的web.xml中的泽西配置:
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.dataws</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
更新
我升级到1.17,问题仍然存在。以下是类强制转换异常
的堆栈跟踪java.lang.ClassCastException: com.dataws.json.RbrvsListWrapper cannot be cast to com.dataws.json.RateListWrapper
at com.dataws.provider.RateListWraperProvider.getSize(RateListWraperProvider.java:1)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:289)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1479)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
答案 0 :(得分:1)
使用@maslim建议并使用Jackson创建JSON。删除了我的自定义提供程序,这两个服务现在都按预期工作。