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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10807449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:40:07  来源:igfitidea点击:

Parse Response Entity with CXF Rest client

javaweb-servicesrestcxf

提问by cristian370

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:

实施:

@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 中的对象实体作为 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. i need your help, please!

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

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!

谢谢!

回答by Sikorski

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.

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

回答by Peter De Winter

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))。Web 客户端不支持读取器提供程序。

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()。

回答by Robin Wieruch

For the Proxy APIit should work like this:

对于代理 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 WebClientit 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();