1

我在这里寻找一些解决方案,但我没有找到我的问题的任何正确答案,所以我想问你。

我有一些简单的属性的 POJO。和另一个 POJO 的一个列表。

public class Standard implements Serializable {
    private String id;
    private String title;
    private String description;
    private Set<Interpretation> interpretations = new LinkedHashSet<Interpretation>();
}

public class Interpretation implements Serializable {
    private String id;
    private String title;
    private String description;
}

在我的控制器类中,我使用 GSON 返回标准 POJO。

@RequestMapping(value="/fillStandard", method= RequestMethod.GET)
public @ResponseBody String getStandard(@RequestParam String id) {
    Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
    return new Gson().toJson(s);
}

问题是,我能否使用 jQuery 获得标准 POJO 中的解释列表?就像是 :

function newStandard() {
$.get("standard/fillStandard.htm", {id:"fe86742b2024"}, function(data) {
    alert(data.interpretations[0].title);
});

}

非常感谢 !

编辑: 好吧,感谢@Atticus,我的问题有了解决方案。希望它会帮助某人。

@RequestMapping(value="/fillStandard", method= RequestMethod.GET, produces="application/json")
    public @ResponseBody Standard getStandard(@RequestParam String id) {
        Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
        return s;
    }

Using@ResponseBody允许您返回整个 POJO,但您需要添加produces="application/json"@RequestMapping注释中。然后,您将能够像我想象的那样在 jQuery 中将返回的对象作为 JSON 捕获。

function newStandard() {
$.get("standard/fillStandard.htm", {id:"idOfStandard"}, function(data) {
    alert(data.id);    //Standard id
    alert(data.interpretations[0].title);   //id of Interpretation on first place in array
});
4

1 回答 1

0

那么您必须创建并注册您的自定义序列化程序。

它是这样的:

//You create your builder that registers your custom serializer with the class you want to serialize
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Standard.class, new StandardSerializer());

//Then you create your Gson object
Gson gson = builder.create();
//Then you pass your object to the gson like
Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
gson.toJson(s);

您的序列化程序如下所示:

public class StandardSerializer implements JsonSerializer<Standard>{

    @Override
    public JsonElement serialize(Standard src, Type typeOfSrc,
            JsonSerializationContext context) {

            JsonObject obj = new JsonObject();
            //You put your simple objects in like this
            obj.add("id",new JsonPrimitive(src.getId()));
            //You put your complex objects in like this
            JsonObject interpretations = new JsonObject();
            //Here you need to parse your LinkedHashSet object and set up the values. 
            //For the sake of simplicity I just access the properties (even though I know this would not compile)
            interpretations.add("title", src.getInterpretation().getTitle());
            obj.add("interpretations", interpretations);
            return obj;
        }

}

在这种情况下,您的 Json 将类似于:

{"id":"idValue", "title":"titleValue", "description":"descriptionValue", "interpretations":["id":"interpretationIdValue"]}

现在,您可以jQuery像这样访问您的数据:

function newStandard() {
$.get("standard/fillStandard.htm", {id:"fe86742b2024"}, function(data) {
    alert(data.interpretations.title);
});
}

我希望这有帮助。

编辑: 我看到您的响应被转换为声明的方法参数类型String(如此所述:16.3.3.2 支持的方法返回类型)。但是您真正想要的是您的StandradPOJO 转换为 JSON。我对 Spring 不是很熟悉,但正如我在这里所读到的(16.3.2.6 Producible Media Types),还有另一个可能更简单的解决方案。如果要返回 JSON 对象,请将 getStandard方法的返回类型更改为Standard而不是String添加produces="application/json"到您的@RequestMapping注释中。据我所读,这应该告诉Spring返回类型应该转换为 JSON。在这种情况下,您不需要使用Gson.

于 2012-06-25T14:44:22.520 回答