use*_*703 23 java spring json content-type spring-mvc
我有一个使用以下注释映射的控制器:
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String bar() {
return "{\"test\": \"jsonResponseExample\"}";
}
Run Code Online (Sandbox Code Playgroud)
我返回一个有效的JSON字符串,但是当我在浏览器中查看Chrome Dev Tools上的响应时,内容类型application/json
不仅仅是简单的text/html
.为什么没有设置内容类型?
我的web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="true" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Spring MVC Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- static assets -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Run Code Online (Sandbox Code Playgroud)
我的dispatcher-servlet.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.mydomain.controllers" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
使用WildFly 8.1作为我的应用服务器.
Sot*_*lis 61
首先要明白的是RequestMapping#produces()
元素
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
Run Code Online (Sandbox Code Playgroud)
仅用于限制请求处理程序的映射.它没有别的.
然后,假设您的方法具有返回类型String
并且带有注释@ResponseBody
,则返回值将由标头StringHttpMessageConverter
设置来处理.如果您想自己返回一个JSON字符串并将标头设置为,请使用返回类型(删除)并为其添加适当的标头.Content-type
text/plain
application/json
ResponseEntity
@ResponseBody
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<String> bar() {
final HttpHeaders httpHeaders= new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<String>("{\"test\": \"jsonResponseExample\"}", httpHeaders, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
请注意,你应该有
<mvc:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
在您的servlet上下文配置中,使用最合适的默认值设置MVC配置.
@ResponseBody
在控制器的返回类型上使用杰克逊库和注释。
如果您希望返回表示为 JSon 的 POJO,则此方法有效。如果您想返回 String 而不是 POJO 作为 JSon,请参阅 Sotirious 答案。
正如其他人评论的那样,因为您的方法的返回类型是String
Spring,所以不需要对结果执行任何操作。
如果您更改签名以使返回类型需要编组,这应该会有所帮助:
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Map<String, Object> bar() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("test", "jsonRestExample");
return map;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
86115 次 |
最近记录: |