在Spring MVC 3.2中实现JSONP [英] Implementing JSONP in Spring MVC 3.2

查看:75
本文介绍了在Spring MVC 3.2中实现JSONP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在Spring MVC的早期版本中可以使用自定义过滤器来实现JSONP.另外,此示例描述了一种通过扩展MappingJacksonHttpMessageConverter类并修改域对象在Spring MVC 3.1中实现JSONP的方法.

I understand that custom filters can be used in earlier version of Spring MVC to implement JSONP. Additionally this example describes a method to implement JSONP in Spring MVC 3.1 by extending the MappingJacksonHttpMessageConverter class and modifying the domain objects.

除了使用上述方法之外,在Spring MVC 3.2中是否有更简单(或常规)的方法来处理JSONP?在Spring 3.2文档中我根本看不到JSONP.

Is there a simpler (or conventional) method to address JSONP in Spring MVC 3.2 besides using the above methods? I did not see JSONP addressed at all in the Spring 3.2 documentation.

推荐答案

更简单的方法

@RequestMapping(value = "/jsonp", method = RequestMethod.GET)
@ResponseBody
public String jsonp(@RequestParam("c")String callBack) throws Exception{
    ObjectMapper objectMapper = new ObjectMapper();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("data", "<p>jsonp data<p>");
    return objectMapper.writeValueAsString(new JSONPObject(callBack,map));
}

这篇关于在Spring MVC 3.2中实现JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆