Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing JSONP in Spring MVC 3.2

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.

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.

like image 534
Tom Avatar asked Mar 07 '13 22:03

Tom


1 Answers

simpler way like this

@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));
}
like image 85
Larry.Z Avatar answered Sep 24 '22 11:09

Larry.Z