2014-04-01 62 views
0

我正在返回一個向量與對象FLineType返回數組與JSON

迴歸了Methode看起來像

@GET 
@Path("/linetypes") 
@Produces(MediaType.APPLICATION_JSON) 
public Vector<FLineType> getLineTypes(){ 
    db = Database.getInstance(); 
    return db.getLineTypes(); 
} 

FLineType看起來像

import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class FLineType { 
    private int id; 
    private String bez; 

public FLineType(){} 

public FLineType(int id, String bez) { 
    super(); 
    this.id = id; 
    this.bez = bez; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getBez() { 
    return bez; 
} 

public void setBez(String bez) { 
    this.bez = bez; 
} 

} 

和我收到的數據在Java對象看起來像

{"fLineType":[{"bez":"Linie 1","id":"1"},{"bez":"Linie 2","id":"2"},{"bez":"Linie 3","id":"3"},{"bez":"Powerplay 1","id":"4"},{"bez":"Powerplay 2","id":"5"},{"bez":"Unterzahl 1","id":"6"},{"bez":"Unterzahl 2","id":"7"}]} 

但我只想在[]括號中的JSON數組數據。沒有對象名稱。 因爲我收到時收到JSONException that the Data must start with [

我對REST服務器使用澤西1.18。

有人可以幫我嗎?

+0

爲什麼'Vector'?爲什麼不是一個簡單的'List'或甚至一個'Collection'? – fge

+0

@fge:我得到了與'List'或'Array'相同的結果 – Stone

+0

然後使用'List'。 'Vector'已過時。 – fge

回答

0

我通過將這些行添加到我的web.xml文件中解決了這個問題。

<init-param> 
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
    <param-value>true</param-value> 
</init-param>