如何通过CXF将POJO类用于其余WS [英] How to use POJO class for rest WS with CXF

查看:114
本文介绍了如何通过CXF将POJO类用于其余WS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按以下方式将POJO类引用返回给其余WS(CXF 3.1.2)中的客户端

I am trying to return POJO class reference to client in rest WS(CXF 3.1.2) as below,

服务方法声明:

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes({"application/xml", MediaType.TEXT_PLAIN})
    @Path("/agentLogout")
    public ResponseEvent agentLogout(String ext) {
    ResponseEvent response= new ResponseEvent();
    response.setDn(ext);
    return response;
    }

客户代码:

    WebClient client = WebClient.create(REST_URI);    
    client.path("agentLogout").accept(MediaType.APPLICATION_JSON);
    Response agentLogoutResponse = client.post("3101");
    String responseStr=agentLogoutResponse.readEntity();

POJO:

    public class ResponseEvent {
    private String dn;
    public String getDn() {
    return dn;
    }
    public void setDn(String ext) {
    this.dn=ext;
    }
    }

问题:


  1. 我如何在客户端代码中检索/访问返回的引用?

  1. how i can retrieve/access the returned reference in client code ?

String responseStr = agentLogoutResponse.readEntity(); // //是否也需要在客户端代码中创建pojo类/接口。.

String responseStr=agentLogoutResponse.readEntity(); // Do i need to create the pojo class/interface in client code too..?

此POJO引用是否在CXF中呈现为JSON?如果可以的话,如何在客户端代码中使用它?

Whether this POJO reference rendered as JSON in CXF? if so how we can use it in client code?

谢谢

推荐答案

说您已经在客户端创建了接口(可以使用WADL2Java插件自动生成该接口)。

Say You have created interface(It could be auto generated using WADL2Java plugin) at client side.

public interface MyService{

 @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes({"application/xml", MediaType.TEXT_PLAIN})
    @Path("/agentLogout")
    public ResponseEvent agentLogout(String ext);

}

创建单例类并创建服务实例

Create A singleton Class and Create instance of Service

public class CxfRestSingleton {

    public static GenService obj;

    public static GenService getInstance() {

        if (obj == null) {
            obj = JAXRSClientFactory.create("http://localhost:8080/api/hello", MyService.class, Arrays.asList(new JacksonJaxbJsonProvider()));
        }
        return obj;
    }

}

**注意:**此处我使用的是Jackson JAXB JSON provivder,它可以封送/解编xml和JSON,您可以使用提供者的选择。 Jackson提供者不是cxf的一部分,因此您需要单独包括这些依赖项

**NOTE:**Here I'm using Jackson JAXB JSON proivder, which can marshal/unmarshal both xml and JSON you could use your choice of provider. An Jackson provider is not part of cxf so you need to include these dependency separately

然后您就可以访问如下所示的pojo类

And the You could access your pojo class as shown Below

CxfRestSingleton.getInstance().agentLogout(12345).getDn();

这篇关于如何通过CXF将POJO类用于其余WS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆