2014-01-06 123 views
2

如何使restful Response中的自定義對象看起來像本機類型。換句話說,我不想使用getter和setter,而是讓對象編組並且像字符串一樣解組。我可以使用unmarshall的構造函數,但我不知道如何通過服務的響應來獲取對象。當我運行樣本時,我得到了這個結果:如何在響應中創建一個RESTful服務處理自定義對象?

<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; 
    } 
} 
+1

結帳以下帖子http://stackoverflow.com/questions/17533280/resteasy-jax-rs-sending-custom-object-in-response-body – sasankad

+0

你使用的是什麼庫或框架? – SpacePrez

+0

我正在嘗試使用RESTEasy。這是否意味着我需要一個MessageBodyWriter? – NicholasKarl

回答

3

答案是你需要的註釋添加到自定義類來告訴它如何並行化和串行不同的領域。你的get方法看起來很好,但是自定義類根本沒有註釋。 MyType的和NotMyType也應該有@ XmlRootElement將以及標註來標記哪些字段@短暫的,哪些是@ 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; 
     } 
    } 

還,因爲你所做的數據的那些對象的部分已經,只是要求一個單一的數據對象,然後你可以從那裏拉別人。

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

如果你不習慣使用getter和setter,那麼你需要設置字段'public'。因爲RESTeasy需要訪問這些字段。

+0

我不希望XML中的字段。我想讓這個對象像一個本地對象一樣行動,並且馬歇爾,解開它就好像沒有字段。我認爲MessageBodyWriter是答案,但我還沒有嘗試過。 – NicholasKarl

相關問題