Jersey 客户端不 return 或读取来自多个 class 对象的响应

Jersey client does not return or read response from multiple class objects

我的 Web 服务从多个实体返回响应。但是当我使用 response.readEntity(A.class) 阅读响应时。注意 class A 包含 class B 和 class C 的依赖项。当我从上面的语句读取响应时,我只能读取 class A 的内容。

在邮递员中一切都很好。邮递员显示包含 class A、B、C 内容的响应。但是在球衣客户端我没有得到 class 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 是另一个 class 让我们说 B,并且 RESULT_COLLECTON 再次包含 class RESULT 的对象数组的依赖项.表示 RESULT_COLLECTION 包含 RESULT class 数组的数组。所以在我的例子中 class TAPatternA,class RESULT_COLLECTIONB 而 class 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 classes:

@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;

我强烈建议通过完全删除 RESULT_COLLECTION 并将结果对象列表添加到您的 TAPattern class。

来更改您的设计
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 对象,因此没有取回所有内容。