[英]Parse Response Entity with CXF Rest client
I'm working with CXF 2.3.2, I made this REST Service: 我正在使用CXF 2.3.2,我做了这个REST服务:
Interface: 接口:
@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: 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();
}
I need to consume that service using a CXF REST Client and obtain the object Entity inside de Response as a Java Object, not as a InputStream. 我需要使用CXF REST客户端来使用该服务,并将de Response中的对象Entity作为Java对象获取,而不是作为InputStream。
I made a first implementation as follows, using a ResponseReader class to wrap my Java Class: 我做了如下的第一个实现,使用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();
Maybe i made the service wrong or the client has a bad conf. 也许我使服务错误或客户端有一个糟糕的conf。 i need your help, please!
拜托,我需要你的帮助!
The service is was configurated using Spring 3.0.5: 该服务是使用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>
Thanks! 谢谢!
Instead of getting the Response object by invoking the get method on client try this: 不是通过在客户端上调用get方法来获取Response对象,而是尝试:
CompanyVO company = client.get(CompanyVO.class);
I think this might be able to solve your problem. 我想这可能能解决你的问题。
Have a look at webclient api 看看webclient api
Also i dont think you would need @Consumes annotation on your webservice method for application/json etc... as you are using a Path parameter in the method. 另外我不认为你需要在你的webservice方法上使用@Consumes注释应用程序/ json等...因为你在方法中使用Path参数。
No clean solution here for CXF 2.3.X except switching to the usage of proxies with JAXRSClientFactory or use the double calls (get() - get(someclass.class). The webclient does' t support the reader provider. 这里没有针对CXF 2.3.X的干净解决方案,除了切换到使用JAXRSClientFactory代理或使用双调用(get() - get(someclass.class).Webclient不支持读者提供者。
CXF 2.7.X implements JAX-RS 2.0 (almost) and from this version on you can call client.readEntity(). CXF 2.7.X实现了JAX-RS 2.0(差不多),从这个版本开始,你可以调用client.readEntity()。
For the Proxy API it should work like this: 对于Proxy API,它应该像这样工作:
Try: 尝试:
ResponseReader reader = new ResponseReader();
reader.setEntityClass(CompanyVO.class);
InterfaceClass proxy = JAXRSClientFactory.create(PATH, InterfaceClass.class, Collections.singletonList(reader));
Then: 然后:
Response res = proxy.get();
CompanyVO company = (CompanyVO) res.getEntity();
For the WebClient it should work quite the same: 对于WebClient,它应该完全相同:
Try: 尝试:
ResponseReader reader = new ResponseReader();
reader.setEntityClass(CompanyVO.class);
WebClient client = WebClient.create(PATH, Collections.singletonList(reader));
Then: 然后:
Response res = client.get();
CompanyVO company = (CompanyVO) res.getEntity();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.