Jersey客户端不返回或读取多个类对象的响应

时间:2017-10-16 06:15:27

标签: java web-services jersey-client

我的网络服务返回多个实体的回复。但是当我通过使用response.readEntity(A.class)读取响应时。注意类A包含类B和类C的依赖关系。当我从上面的语句中读取响应时,我只能读取类A的内容。

邮递员一切都很好。邮递员显示包含A,B,C类内容的回复。但在球衣客户端,我没有得到B级和C级的内容。

你能帮我解决这个问题。

以下是我的Java Pojo代码

@XmlRootElement(name = "TA_TESTPATTERN")
@XmlAccessorType(XmlAccessType.FIELD)
public class TAPattern {
    private User User;
    private String TP_Type;
    private String Car;
    private String Category;
    private String Description;
    private TestCase TestCase;
    private String status;
    private String Customizable_Field;
    private RESULT_COLLECTION RESULT_COLLECTION;
    private String Id;
    private String xmlns;
    private String Variant;
    private String Code;

此处RESULT_COLLECTION是另一个类,例如BRESULT_COLLECTON再次包含类RESULT的对象数组的依赖项。方法RESULT_COLLECTION包含RESULT类数组的数组。因此,在我的案例中,班级TAPatternA,班级RESULT_COLLECTIONB,班级RESULTC。测试用例是D

以下是我的球衣客户代码:

Response response = invocationBuilder.header("Content-Type", "application/xml")
        .post(Entity.entity(vstpattern, MediaType.APPLICATION_XML));
return response.readEntity(TAPattern.class);

以下是RESULT_COLLECTIONRESULT和测试用例的POJO课程:

@XmlRootElement(name = "RESULT_COLLECTION")
@XmlAccessorType(XmlAccessType.FIELD)
public class RESULT_COLLECTION {
    private Result[] Result;
}

@XmlRootElement(name = "Result")
@XmlAccessorType(XmlAccessType.FIELD)
public class Result {
    private String Method;
    private String Href;
    private String Rel;
}

@XmlRootElement(name = "TestCase")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestCase {
    private String Method;
    private String Href;
    private String Rel;
}

以下是我的球衣客户代码:

ConfigPropertyHandler configPropertyHandler = ConfigPropertyHandler.getInstance();
        ClientConfig config = new ClientConfig();
        HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(configPropertyHandler.getTid(),
                configPropertyHandler.getPassword());
        Client client = ClientBuilder.newClient(config);
        client.register(feature);

        WebTarget webTarget = client.target(configPropertyHandler.getVstUrl()).path(configPropertyHandler.getVstPath());
        Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_XML);

        Response response = invocationBuilder.header("Content-Type", "application/xml")
                .post(Entity.entity(vstpattern, MediaType.APPLICATION_XML));
        //return 0;


        return response.readEntity(TAPattern.class);

    }

以下是xml POSTMAN请求:

 <TA_TESTPATTERN xmlns="http://examples.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Car>MyCar</Car>
        <Id>122</Id>
        <Code>myCode</Code>
        <Description> TA TEST</Description>
        <TP_Type>VF</TP_Type>
        <Category>0</Category>
        <Variant>0</Variant>
        <Customizable_Field> nda </Customizable_Field>
        <status>w</status>
        <RESULT_COLLECTION>
            <Result>
                <Href>http://examples.com</Href>
                <Method>GET</Method>
                <Rel>READ</Rel>
            </Result>
            <Result>
                <Href>http://examples.com</Href>
                <Method>GET</Method>
                <Rel>READ</Rel>
            </Result>
        </RESULT_COLLECTION>
        <TestCase>
            <Href>http://examples.com</Href>
            <Method>GET</Method>
            <Rel>READ</Rel>
        </TestCase>
        <User>
            <ID>f10711a</ID>
            <email>abc@external.com</email>
            <Name>abc</Name>
        </User>
    </TA_TESTPATTERN>

以下是我的TA_Pattern

@XmlRootElement(name = "TA_TESTPATTERN")
@XmlAccessorType(XmlAccessType.FIELD)
public class TAPattern {


    @XmlElement(name = "User")
    private User User;

    @XmlElement(name = "TP_Type")
    private String TP_Type;

    @XmlElement(name = "Car")
    private String Car;

    @XmlElement(name = "Category")
    private String Category;

    @XmlElement(name = "Description")
    private String Description;

    @XmlElement(name = "TestCase")
    private TestCase TestCase;

    @XmlElement(name = "status")
    private String status;

    @XmlElement(name = "Customizable_Field")
    private String Customizable_Field;

    @XmlElement(name = "RESULT_COLLECTION") RESULT_COLLECTION RESULT_COLLECTION;

    @XmlElement(name = "Id")
    private String Id;

    @XmlElement(name = "xmlns")
    private String xmlns;

    @XmlElement(name = "Variant")
    private String Variant;

    @XmlElement(name = "Code")
    private String Code;

以下是VSTTAPattern的POJO

@XmlRootElement(name = "TA_TESTPATTERN")
@XmlAccessorType(XmlAccessType.FIELD)
public class VSTTAPattern {


        private User User;

        private String TP_Type;

        private String Car;

        private String Category;

        private String Description;

        private TestCase TestCase;

        private String status;

        private String Customizable_Field;

        private RESULT_COLLECTION RESULT_COLLECTION;

        private String Id;

        private String xmlns;

        private String Variant;

        private String Code;

1 个答案:

答案 0 :(得分:0)

我强烈建议您通过完全删除RESULT_COLLECTION并将结果列表List添加到TAPattern类来更改设计。

class TAPattern{

...
@XmlElement
private List<Result> resultList;
}

public List<Result> getResultList(){
if(resultList==null)
    resultList = new ArrayList<Result>();

 return resultList;
}

@XmlAccessorType(XmlAccessType.FIELD) 
class Result {
....
}

这应该是一个更清洁的设计。也就是说,我不确定你是如何在服务器上生成xml响应的,也许还有一些我不知道的约束。

更新:以下代码适合我。

ClientConfig config = new ClientConfig();

        Client client = ClientBuilder.newClient(config);

        WebTarget target = client.target(UriBuilder.fromUri("http://localhost:8088/JerseyRest").build());   

        User u = new User();
        u.email = "xx@xx.com";
        u.ID = "sjdgzuauoj";
        u.Name = "user name";
        TestCase tc = new TestCase();
        tc.Href = "yyyy@hreg.com";
        tc.Method = "POST";
        tc.Rel = "link@link.com";

        Result[] rArr = new Result[]{ new Result(), new Result()};
        rArr[0].Href = "href@href.com";
        rArr[0].Method = "GET";
        rArr[0].Rel = "READ";

        rArr[1].Href = "href@href1.com";
        rArr[1].Method = "POST";
        rArr[1].Rel = "WRITE";
        RESULT_COLLECTION rCol = new RESULT_COLLECTION();

        rCol.Result = rArr;
        TAPattern req = new TAPattern();
        req.RESULT_COLLECTION = rCol;
        req.Car = "Volvo";
        req.Category = "0";
        req.Code = "1";
        req.Customizable_Field = "0";
        req.Description = "Test";
        req.status = "Y";
        req.Variant = "0";
        req.TestCase = tc;
        req.User = u;
        //GenericType<List<TAPattern>> taList = new GenericType<List<TAPattern>>() {};
        try{

        // Get XML for application   
       Response res = target.path("rest").path("UserService").path("xxxr").request()
                .accept(MediaType.APPLICATION_XML).post(Entity.entity(req, MediaType.APPLICATION_XML),Response.class);

        TAPattern ta = (TAPattern)res.readEntity(TAPattern.class);
       for(Result r : ta.RESULT_COLLECTION.Result ){
           System.out.println("Href: "+r.Href);
           System.out.println("Rel: "+r.Rel);
           System.out.println("Mehod: "+r.Method);
       }

    }catch(Exception e){
        System.out.println(e);
        return;
    }

您可能没有正确填充TAPattern对象,因此无法恢复所有内容。