1

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

谢谢!

4

3 回答 3

3

Instead of getting the Response object by invoking the get method on client try this:

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

I think this might be able to solve your problem.

Have a look at 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.

于 2012-06-02T17:06:56.363 回答
1

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

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

于 2013-11-13T13:11:24.730 回答
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();
于 2012-07-17T10:17:38.313 回答