我正在使用 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);
实施:
@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 中的对象实体作为 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>
谢谢!