@Produces({“application/x-javascript”}) 在 Spring MVC 版本 4.0.

@Produces({"application/x-javascript"}) not working in spring mvc version 4.0.5.RELEASE

本文关键字:Spring MVC 版本 application x-javascript @Produces      更新时间:2023-09-26

我创建了一个 spring 应用程序,我也在使用 Web 服务。

我想使用jsonp回调函数,其媒体类型应该是{"application/x-javascript"}。这似乎不起作用。它返回的是 JSON 对象而不是 JavaScript。这是代码..

 @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);
    }

我使用其余客户端进行了检查...

它说内容类型是:内容类型:application/json;charset=UTF-8

它必须是:内容类型:application/javascript;charset=UTF-8

我认为你把泽西的@Produces和JSONWithPadding与Spring MVC混合在一起,泽西的@Produces在那里不会有任何影响。

如果您正在寻找一种仅使用 Spring MVC 实现 JSON-P 的方法,请查看

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

或者如果您能够升级到版本 4.1 或更高版本

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

您使用的是早于 3.1.1.RELEASE 的 Spring 版本。如果要设置响应的媒体类型,则应将其置于 RequestMapping 注释中,如下所示:

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

话虽如此,应用程序/javascript 不是 Spring 的有效媒体类型。您可以引用 MediaType 类的值。

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