繁体   English   中英

使用CXF Rest客户端解析响应实体

[英]Parse Response Entity with CXF Rest client

我正在使用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();

也许我使服务错误或客户端有一个糟糕的conf。 拜托,我需要你的帮助!

该服务是使用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> 

谢谢!

不是通过在客户端上调用get方法来获取Response对象,而是尝试:

CompanyVO company = client.get(CompanyVO.class);

我想这可能能解决你的问题。

看看webclient api

另外我不认为你需要在你的webservice方法上使用@Consumes注释应用程序/ json等...因为你在方法中使用Path参数。

这里没有针对CXF 2.3.X的干净解决方案,除了切换到使用JAXRSClientFactory代理或使用双调用(get() - get(someclass.class).Webclient不支持读者提供者。

CXF 2.7.X实现了JAX-RS 2.0(差不多),从这个版本开始,你可以调用client.readEntity()。

对于Proxy 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM