Rest Easy Client Framework 值在解组后丢失

发布于 2024-09-28 10:44:24 字数 2384 浏览 3 评论 0原文

昨天我尝试使用 RestEasy 框架的客户端。该接口有一个方法:

@PUT
@Path("document/autoincrement")
@Consumes("application/xml")
BaseClientResponse<String> insertPointOfInterest(PoiDocument poiDocument);

对某些(泽西岛)休息服务的调用如下所示:

    String restServerServiceUrl = "http://my.jersey.server/rest/serviceFoo/v1/";
    NSSClientService client = ProxyFactory.create(NSSClientService.class, restServerServiceUrl);

    PoiDocument poiDocument = new PoiDocument("Parkirišče", "90", 390262.85133115170, 42240.33558245482);

    BaseClientResponse<String> response = client.insertPointOfInterest(poiDocument);
    assert response.getResponseStatus() == Response.Status.OK;

    // Expected result
    //<?xml version="1.0" encoding="UTF-8" standalone="yes"?><insertedRecord><record>14</record></insertedRecord>

    logger.info("Returned: " + response.getEntity());

记录器打印:

14

与预期类似。

但我想要一个对象而不是字符串,因此我可以轻松断言返回的值。接口:

@PUT
@Path("document/autoincrement")
@Consumes("application/xml")
BaseClientResponse<InsertedResponse> insertPointOfInterest(PoiDocument poiDocument);

现在不再是 String,而是一个 InsertedResponse 类,它看起来像:

@XmlRootElement(name="insertedRecord")
public class InsertedResponse extends ResponseResult{

    String insertedRecord;

    public InsertedResponse(int insertedRecord) {
        this.insertedRecord = Integer.toString(insertedRecord);
    }

    public InsertedResponse(){
        insertedRecord = "";
    }

    @XmlElement(name="record")
    public String getInsertedRecords(){
        return insertedRecord;
    }

    public void add(int recNo) {
        insertedRecord = Integer.toString(recNo);
    }
}

...及其超类:

@XmlRootElement(name = "result")
public abstract class ResponseResult {

    protected String getClearString(String string) {

        if (string != null) {
            return Constants.removeInvalidXMLCharacters(string);

        }
        return "";
    }
}

现在,当我将客户端调用更改为:

    BaseClientResponse<InsertedResponse> response = client.insertPointOfInterest(poiDocument);
    logger.info("Returned: " + response.getEntity().getInsertedRecords());

我得到一个空字符串而不是某个值。

那么,问题来了——价值去了哪里?它应该打印一个数字,如上例中的 14。

Yesterday I tried to use the client side of the RestEasy framework. The interface has a method:

@PUT
@Path("document/autoincrement")
@Consumes("application/xml")
BaseClientResponse<String> insertPointOfInterest(PoiDocument poiDocument);

and the call to some (Jersey) rest service looks like:

    String restServerServiceUrl = "http://my.jersey.server/rest/serviceFoo/v1/";
    NSSClientService client = ProxyFactory.create(NSSClientService.class, restServerServiceUrl);

    PoiDocument poiDocument = new PoiDocument("Parkirišče", "90", 390262.85133115170, 42240.33558245482);

    BaseClientResponse<String> response = client.insertPointOfInterest(poiDocument);
    assert response.getResponseStatus() == Response.Status.OK;

    // Expected result
    //<?xml version="1.0" encoding="UTF-8" standalone="yes"?><insertedRecord><record>14</record></insertedRecord>

    logger.info("Returned: " + response.getEntity());

And the logger prints:

14

Kind of expected.

But I want an object not a string, so I can easely assert the values returned. The interface:

@PUT
@Path("document/autoincrement")
@Consumes("application/xml")
BaseClientResponse<InsertedResponse> insertPointOfInterest(PoiDocument poiDocument);

Instead of String there is now a InsertedResponse class which looks like:

@XmlRootElement(name="insertedRecord")
public class InsertedResponse extends ResponseResult{

    String insertedRecord;

    public InsertedResponse(int insertedRecord) {
        this.insertedRecord = Integer.toString(insertedRecord);
    }

    public InsertedResponse(){
        insertedRecord = "";
    }

    @XmlElement(name="record")
    public String getInsertedRecords(){
        return insertedRecord;
    }

    public void add(int recNo) {
        insertedRecord = Integer.toString(recNo);
    }
}

...and its superclass:

@XmlRootElement(name = "result")
public abstract class ResponseResult {

    protected String getClearString(String string) {

        if (string != null) {
            return Constants.removeInvalidXMLCharacters(string);

        }
        return "";
    }
}

Now, when I change the client call also to:

    BaseClientResponse<InsertedResponse> response = client.insertPointOfInterest(poiDocument);
    logger.info("Returned: " + response.getEntity().getInsertedRecords());

I get an empty string instead of some value.

So, the question is - where did the value of go? It should print a number, like 14 in the above example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-10-05 10:44:24

缺少一个 JAXB 注释 (@XmlSeeAlso)

@XmlRootElement(name = "result")
@XmlSeeAlso( { InsertedResponse.class, OtherChild.class, SomeOtherChild.class })
public abstract class ResponseResult {

    ...
}

和添加的 setter 方法

@XmlRootElement(name="insertedRecord")
public class InsertedResponse extends ResponseResult{

    ...

    public void setInsertedRecords(String insertedRecord) {            
        this.insertedRecord = insertedRecord;
    }

解决了该问题。

One missing JAXB annotation (@XmlSeeAlso)

@XmlRootElement(name = "result")
@XmlSeeAlso( { InsertedResponse.class, OtherChild.class, SomeOtherChild.class })
public abstract class ResponseResult {

    ...
}

and an added setter method

@XmlRootElement(name="insertedRecord")
public class InsertedResponse extends ResponseResult{

    ...

    public void setInsertedRecords(String insertedRecord) {            
        this.insertedRecord = insertedRecord;
    }

solved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文