簡體   English   中英

如何在Response中使RESTful服務處理自定義對象?

[英]How do I make a RESTful service handle custom object in the Response?

如何在寧靜的Response中使自定義對象看起來像本機類型。 換句話說,我不想使用getter和setter,而是將對象像字符串一樣編組和解組。 我可以使用構造函數進行編組,但是我不知道如何通過服務的響應來獲取對象。 運行樣本時,我得到以下結果:

<data>
  <nType>123</nType>
  <myType/>
  <notMyType>
    <value>def</value>
  </notMyType>
</data>

我試圖讓myType看起來像nType。 這是一個簡短的代碼示例:

package rscust;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.xml.bind.annotation.XmlRootElement;

@Path("/")
public class Service extends Application {

    @XmlRootElement
    public static class Data {
        public String nType;
        public MyType myType;
        public NotMyType notMyType;
        public Data() {}
    }

    // custom class does not work in Response
    public static class MyType {
        private String value;
        public MyType() {}
        public MyType(String value) {
            this.value=value;
        }
        public String toString() {
            return value;
        }
    }

    // works with getter and setter, but I don't want that
    public static class NotMyType {
        private String value;
        public NotMyType() {}
        public NotMyType(String value) {
            this.setValue(value);
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }

    @GET
    @Produces(value={MediaType.APPLICATION_XML})
    public Response get(
            @QueryParam(value = "ntype")String nType,
            @QueryParam(value = "mytype")MyType myType, 
            @QueryParam(value = "notmytype")NotMyType notMyType
    ) {
        Data data = new Data();
        data.nType = nType;
        data.myType = myType;
        data.notMyType = notMyType;
        return Response.ok().entity(data).build();

    }

    private HashSet<Object> singletons;
    public Service() {
        super();
        singletons = new HashSet<Object>();
        singletons.add(this);
    }
    public Set<Object> getSingletons() {
        return singletons;
    }
}

答案是您需要在自定義類中添加注釋,以告訴它如何反序列化和序列化不同的字段。 您的get方法看起來不錯,但是自定義類根本沒有注釋。 MyType和NotMyType還應該具有@ XmlRootElement以及用於標記哪些字段為@ Transient和哪些字段為@JsonProperty的注釋

@XmlRootElement
    public static class Data {
        @JsonProperty
        public String nType;
        @JsonProperty
        public MyType myType;
        @JsonProperty
        public NotMyType notMyType;
        public Data() {}
    }

@XmlRootElement
public static class MyType {
        @JsonProperty
        private String value;
        public MyType() {}
        public MyType(String value) {
            this.value=value;
        }
        public String toString() {
            return value;
        }
    }

同樣,由於您已經將那些對象作為Data的一部分,因此只需請求一個數據對象,然后就可以從那里拉其他對象。

public Response get( @QueryParam(value = "data")Data d,){
Data d = data.nType;
}

如果您不使用getter和setter,則需要將字段設置為“ public”。 因為RESTeasy需要訪問字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM