使用带有弹簧mvc 2.5的jackson

时间:2013-07-29 08:33:09

标签: ajax json spring-mvc jackson

对于一般性问题感到抱歉,但我不能谷歌上的任何内容,我们在项目中使用spring mvc 2.5,因此没有@ResponseBody注释,如果没有它,我怎样才能创建像this这样的东西?

1 个答案:

答案 0 :(得分:1)

您可以将其作为使用Jackson对象映射器构建的字符串返回:

public String getCustomDetails(@PathVariable String variable1) {

    CustomDetails details = new CustomDetails(variable1);
    ObjectMapper mapper = new ObjectMapper();
    String result = null;   

    result = mapper.writeValueAsString(details);

    return result;
}

那应该有用。可能必须在try-catch中包含对writeValueAsString的调用。

编辑:我应该澄清“CustomDetails”和“variable1”只是示例值...它们可以是任何东西。