2016-04-13 1 views
-1

私はSpring MVCでかなり新しいです。これが初めてJSONを制作しています。このJSONをSpring MVCコントローラメソッドで構築したラッパーをどのようにラップすることができますか?

だから私は、次のような状況があります。

私はこの2つのモデルクラスがあります。

1)CountryData

public class CountryData { 

    private String label; 
    private String subdataConcat; 

    private ArrayList<CountryDataChild> countryDataChildList; 

    public CountryData(String label, String subdataConcat, ArrayList<CountryDataChild> countryDataChildList) { 
     super(); 
     this.label = label; 
     this.subdataConcat = subdataConcat; 
     this.countryDataChildList = countryDataChildList; 
    } 

    public String getLabel() { 
     return label; 
    } 

    public void setLabel(String label) { 
     this.label = label; 
    } 

    public String getSubdataConcat() { 
     return subdataConcat; 
    } 

    public void setSubdataConcat(String subdataConcat) { 
     this.subdataConcat = subdataConcat; 
    } 

    public ArrayList<CountryDataChild> getCountryDataChildList() { 
     return countryDataChildList; 
    } 

    public void setCountryDataChildList(ArrayList<CountryDataChild> countryDataChildList) { 
     this.countryDataChildList = countryDataChildList; 
    } 

} 

2)CountryDataChild

public class CountryDataChild { 

    private String label; 
    private String code; 

    public CountryDataChild(String label, String code) { 
     super(); 
     this.label = label; 
     this.code = code; 
    } 

    public String getLabel() { 
     return label; 
    } 

    public void setLabel(String label) { 
     this.label = label; 
    } 

    public String getCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

} 

最後に、コントローラクラスに私はCountryData情報を含むJSONを生成し、このコントローラのメソッドを持っています。

@RequestMapping(value="/getCountryData", method = RequestMethod.GET) 
public @ResponseBody CountryData getShopInJSON() { 

    ArrayList<CountryDataChild> countryDataChildList = new ArrayList<CountryDataChild>(); 

    CountryDataChild child1 = new CountryDataChild("Lazio", "lazio"); 
    CountryDataChild child2 = new CountryDataChild("Lombardia", "lombardia"); 

    countryDataChildList.add(child1); 
    countryDataChildList.add(child2); 

    CountryData countryData = new CountryData("Italia", "y", countryDataChildList); 

    return countryData; 

} 

これは、それが実行されると、かなり正常に動作して、私のブラウザに出力としてこのJSONを生成:

countrydata({"label":"Italia","subdataConcat":"y","countryDataChildList":[{"label":"Lazio","code":"lazio"},{"label":"Lombardia","code":"lombardia"}]}) 
0:

{"label":"Italia","subdataConcat":"y","countryDataChildList":[{"label":"Lazio","code":"lazio"},{"label":"Lombardia","code":"lombardia"}]} 

[OK]を、唯一の問題は、提供仕様に従って、この目的は、しなければならないことです

だから、基本的に生成されたJSONは、countrydata(...)私はこの仕様を実装するにはどうすればよい

内にラップする必要がありますか?私は何が欠けていますか?

答えて

0

通常のJSONではなくJSONPを生成しようとしているようです。ここで説明するようにSpring MVCのでそれを行う1つの方法はあるとして、あなたのControllerを残し、その後、フィルタを作成することです:あなたはcountrydata機能であなたのJSONをラップしてもtext/javascriptにコンテンツタイプを設定する場所ですhttps://patrickgrimard.com/2010/07/28/tutorial-implementing-a-servlet-filter-for-jsonp-callback-with-springs-delegatingfilterproxy/

関連する問題