0

I have created a spring application and i am also using webservices.

I want to use jsonp callback function for which the media type should be {"application/x-javascript"}. This seems not to be working. it is returning json object and not javascript. Here is the code..

 @RequestMapping(value = "widget", method = RequestMethod.GET)
 @Produces({"application/x-javascript"})
  public @ResponseBody JSONWithPadding displayWidgetPage(Model model, HttpServletResponse                              
 response,HttpServletRequest request)
{
    String callback = request.getParameter("callback");

    PointsInfo pointsInfo =new PointsInfo();
    pointsInfo.setUsername("json");

    return new JSONWithPadding(pointsInfo,callback);
    }

I checked using the rest client...

It says the content type is : Content-Type: application/json;charset=UTF-8

It has to be : Content-Type: application/javascript;charset=UTF-8

user2768984
  • 57
  • 1
  • 11

2 Answers2

2

I think that you're mixing Jersey's @Produces and JSONWithPadding with Spring MVC, and Jersey's @Produces will not take any effect there.

If you're looking for a way to implement JSON-P with Spring MVC only, take a look at

http://patrickgrimard.com/2010/07/28/tutorial-implementing-a-servlet-filter-for-jsonp-callback-with-springs-delegatingfilterproxy/

or if you're able to upgrade to the version 4.1 or above

http://spring.io/blog/2014/07/28/spring-framework-4-1-spring-mvc-improvements

Master Slave
  • 27,771
  • 4
  • 57
  • 55
0

You are using a Spring version older than 3.1.1.RELEASE. If you are looking to set your response's media type, it should be in the RequestMapping annotation like this:

    @RequestMapping(value = "/list/rideLogs/{rideId}", method = RequestMethod.POST, 
         produces = YOUR_MEDIA_TYPE)

That being said application/javascript isn't a valid media type for Spring. You can refer to values of MediaType class.

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/MediaType.html

erginm
  • 1
  • 2