2016-04-13 2 views
-1

Ich bin ziemlich neu in Spring MVC und das ist mein erstes Mal, dass ich ein JSON produziere.Wie kann ich diesen JSON, der von einer Spring MVC Controller-Methode erstellt wurde, in einen Wrapper einbinden?

So habe ich die folgende Situation:

Ich habe dieses Bild 2 Modellklassen:

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

} 

schließlich in eine Controller-Klasse I diese Controller-Methode habe, die einen JSON mit den CountryData Informationen erzeugen:

@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; 

} 

Das funktioniert ziemlich gut, und wenn es durchgeführt wird, erzeugen diesen JSON als Ausgabe in meinen Browser :

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

Ok, das einzige Problem besteht darin, dass nach der vorgesehenen Spezifikation diese Aufgabe sein müssen:

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

Also im Grunde die erzeugte JSON haben in der countrydata (...)

Wie kann ich diese Spezifikation implementieren gewickelt werden? Was vermisse ich?

Antwort

0

Es sieht so aus, als ob Sie versuchen, JSONP anstelle von normalem JSON zu generieren. Eine Möglichkeit, das zu tun, mit Spring MVC ist auf Ihre Controller zu lassen, wie sie ist, und erstellen Sie dann einen Filter, wie hier beschrieben: https://patrickgrimard.com/2010/07/28/tutorial-implementing-a-servlet-filter-for-jsonp-callback-with-springs-delegatingfilterproxy/

Das ist, wo Sie Ihre JSON in der countrydata Funktion wickeln würden und auch den Inhaltstyp zu text/javascript eingestellt .

Verwandte Themen