spring 在 SpringMVC4 中使用 @RequestMapping(headers = "Accept=application/json")

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23599146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:43:30  来源:igfitidea点击:

Using @RequestMapping(headers = "Accept=application/json") in SpringMVC4

springspring-mvc

提问by user3626306

I am using Spring MVC 4 and I want to know that I am using @RestControllerat class level and for one of its method I am Using @RequestMapping(headers = "Accept=application/json"). Now can any one tell me why just by using <mvc:annotation-driven />in my config-servlet.xmlhow I am getting the response in JSONon my browser. I mean using header attribute is only saying that this method can only respond to requests having this type of headers. How does the MVC4 knows to respond in JSONon browser. Here is my code:

我正在使用 Spring MVC 4,我想知道我@RestController在类级别使用,并且我正在使用它的方法之一@RequestMapping(headers = "Accept=application/json")。现在可以在任何一个,为什么只用告诉我,<mvc:annotation-driven />config-servlet.xml怎么我得到的回应JSON对我的浏览器。我的意思是使用 header 属性只是说这个方法只能响应具有这种类型的 headers 的请求。MVC4 如何知道JSON在浏览器上响应。这是我的代码:

My config-servlet.xml

我的 config-servlet.xml

<beans>
<context:component-scan base-package="com.songs.service.controller" />
<mvc:annotation-driven />
</beans>

My RestController Class

我的 RestController 类

@RestController
@RequestMapping("/songappWS")
public class SongServiceWS 
{ 
@RequestMapping(value = "/topmoviesWS", method = RequestMethod.GET, headers="Accept=application/json")
public List<?> getTopMoviesWS() {
//method logic
return l1;

}
}

采纳答案by geoand

The reason Spring is responding in JSON is that @RestControllerimplicitly means that the the response of getTopMoviesWS()method is handled as if it had the @ResponseBodyannotation (already present in Spring 3).

Spring 在 JSON 中响应的原因是@RestController隐式意味着getTopMoviesWS()方法的响应被处理,就好像它有@ResponseBody注释一样(已经存在于 Spring 3 中)。

What that means is that Spring will use the Hymanson JSON library (if present on the classpath) to convert the response to JSON.

这意味着 Spring 将使用 Hymanson JSON 库(如果存在于类路径中)将响应转换为 JSON。

Check out thispost.

看看这个帖子。

回答by Centonni

You get the response in JSON because of @RestController, How??

因为@RestController,你得到了 JSON 格式的响应,How??

  1. @RestControlleris itself annotated with @Controllerand @ResponseBodyimplying that @RequestMappingmethods assume @ResponseBodysemantics by default.You can have more information here.
  1. @RestController本身带有注释@Controller@ResponseBody暗示 @RequestMapping方法默认采用 @ResponseBody语义。您可以在此处获得更多信息。

To be more comprehensive, your code will look like this :

为了更全面,您的代码将如下所示:

@RequestMapping(value = "/topmoviesWS", method = RequestMethod.GET, 
  headers="Accept=application/json")   
public @ResponseBody List<?> getTopMoviesWS() { 

   return l1;    
}
  1. the @ResponseBodyannotation on the getTopMoviesWS()method tells Spring MVC that it does not need to render the response through a server-side view layer, but that instead the list returned is the response body, and should be written out directly to the HTTP response as JSON. The list returned must be converted to JSON, you don't need to do this conversion manually. Because Hymanson 2 is on the classpath, Spring's MappingHymanson2HttpMessageConverteris automatically chosen to convert the values to JSON. You can have a look hereto understand the @ResponseBodymechanism.
  1. 方法@ResponseBody上的注解getTopMoviesWS()告诉 Spring MVC 它不需要通过服务器端视图层呈现响应,而是返回的列表是响应主体,并且应该作为 JSON 直接写出到 HTTP 响应。返回的列表必须转换为 JSON,您无需手动进行此转换。因为 Hymanson 2 在类路径上,所以MappingHymanson2HttpMessageConverter会自动选择Spring来将值转换为 JSON。您可以查看此处以了解其@ResponseBody机制。