我正在使用CXF 2.3.2,我制作了这个REST服务:
接口:
@WebMethod
@GET
@Path("/object/{id}")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML})
public Response object(@PathParam("id") Long id);
默认地将Impl:
@Override
public Response object(Long id) {
CompanyVO company = new CompanyVO();
company.setAddress("address");
company.setFantasyName("fantasy name");
company.setFiscalId("fiscalid");
company.setGroups("groups");
return Response.ok().type(MediaType.APPLICATION_XML).entity(company).build();
}
我需要使用CXF REST客户端使用该服务,并将de Response中的对象Entity作为Java对象获取,而不是作为InputStream。
我做了如下的第一个实现,使用ResponseReader类来包装我的Java类:
String operation = "/object/{id}";
ResponseReader reader = new ResponseReader();
reader.setEntityClass(CompanyVO.class);
WebClient client = WebClient.create(PATH, Collections.singletonList(reader));
client.path(operation, 12L);
client.accept(MediaType.APPLICATION_XML);
client.type(MediaType.APPLICATION_XML);
//the response's entity object should be this Class.
CompanyVO company = new CompanyVO();
Response response = client.get();
//i get the entity object as a InputStream, but i need a CompanyVO.
//i made this once, but i can't see the difference.
Object entity = response.getEntity();
也许我使服务错误或客户端有错误。我需要你的帮助!
该服务是使用Spring 3.0.5配置的:
<jaxrs:server id="serviceAdvisorRestServer" address="/rest">
<jaxrs:serviceBeans>
<ref bean="fileService"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
<entry key="html" value="text/html"/>
<entry key="pdf" value="application/pdf"></entry>
</jaxrs:extensionMappings>
谢谢!
答案 0 :(得分:3)
不要通过在客户端调用get方法获取Response对象,而是尝试:
CompanyVO company = client.get(CompanyVO.class);
我认为这可以解决您的问题。
查看webclient api
另外,我认为你的web服务方法对于application / json等你需要@Consumes注释......因为你在方法中使用了Path参数。
答案 1 :(得分:1)
此处没有针对CXF 2.3.X的干净解决方案,除了切换到使用JAXRSClientFactory的代理或使用double调用(get() - get(someclass.class).Webclient不支持读者提供者。
CXF 2.7.X实现了JAX-RS 2.0(差不多),从这个版本开始,你可以调用client.readEntity()。
答案 2 :(得分:0)
对于代理API ,它应该像这样工作:
尝试:
ResponseReader reader = new ResponseReader();
reader.setEntityClass(CompanyVO.class);
InterfaceClass proxy = JAXRSClientFactory.create(PATH, InterfaceClass.class, Collections.singletonList(reader));
然后:
Response res = proxy.get();
CompanyVO company = (CompanyVO) res.getEntity();
对于 WebClient ,它应该完全相同:
尝试:
ResponseReader reader = new ResponseReader();
reader.setEntityClass(CompanyVO.class);
WebClient client = WebClient.create(PATH, Collections.singletonList(reader));
然后:
Response res = client.get();
CompanyVO company = (CompanyVO) res.getEntity();