Spring MVC 4:"application/json" 内容类型设置不正确
Spring MVC 4: "application/json" Content Type is not being set correctly
我有一个映射了以下注释的控制器:
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String bar() {
return "{\"test\": \"jsonResponseExample\"}";
}
我 return 一个有效的 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>
我的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>
使用 WildFly 8.1 作为我的应用程序服务器。
在控制器的 return 类型上使用 jackson 库和 @ResponseBody
注释。
如果您希望 return 表示为 JSon 的 POJO,此方法有效。如果您想 return 字符串而不是 POJO 作为 JSon,请参阅 Sotirious 答案。
首先要理解的是RequestMapping#produces()
元素在
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
仅用于限制请求处理程序的映射。 它什么都不做。
然后,假设你的方法有一个 return 类型的 String
并且用 @ResponseBody
注释,return 值将由 [=23= 处理] 将 Content-type
header 设置为 text/plain
。如果你想自己 return 一个 JSON 字符串并将 header 设置为 application/json
,请使用 return 类型的 ResponseEntity
(去掉@ResponseBody
) 并向其添加适当的 header。
@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);
}
请注意,您可能应该
<mvc:annotation-driven />
在您的 servlet 上下文配置中使用最合适的默认设置设置您的 MVC 配置。
正如其他人评论的那样,因为您的方法的 return 类型是 String
Spring 感觉不需要对结果做任何事情。
如果您更改签名以使 return 类型需要编组,那应该会有所帮助:
@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;
}
当我升级到 Spring 4 时,我需要按如下方式更新 jackson 依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.1</version>
</dependency>
我有@Greg 指定的依赖项post。我仍然面临这个问题,可以通过添加以下额外的 jackson 依赖项来解决它:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.7.4</version>
</dependency>
不完全适用于此 OP,但适用于遇到 404 且无法将响应 content-type
设置为 "application/json"
(任何 content-type
)的用户。一种可能是服务器实际响应 406,但资源管理器(例如,chrome)将其打印为 404。
如果您不自定义消息转换器,spring 将使用 AbstractMessageConverterMethodProcessor.java
。它将 运行:
List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(request);
List<MediaType> producibleMediaTypes = getProducibleMediaTypes(request, valueType, declaredType);
如果它们没有任何重叠(相同的项目),它会抛出 HttpMediaTypeNotAcceptableException
,这最终会导致 406。无论是 ajax 还是 GET/POST,或表单操作,如果请求 uri 以 .html
或任何后缀结尾,则 requestedMediaTypes
将是 "text/[that suffix]",这与 producibleMediaTypes
冲突,通常是:
"application/json"
"application/xml"
"text/xml"
"application/*+xml"
"application/json"
"application/*+json"
"application/json"
"application/*+json"
"application/xml"
"text/xml"
"application/*+xml"
"application/xml"
"text/xml"
"application/*+xml"
我有一个映射了以下注释的控制器:
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String bar() {
return "{\"test\": \"jsonResponseExample\"}";
}
我 return 一个有效的 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>
我的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>
使用 WildFly 8.1 作为我的应用程序服务器。
在控制器的 return 类型上使用 jackson 库和 @ResponseBody
注释。
如果您希望 return 表示为 JSon 的 POJO,此方法有效。如果您想 return 字符串而不是 POJO 作为 JSon,请参阅 Sotirious 答案。
首先要理解的是RequestMapping#produces()
元素在
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
仅用于限制请求处理程序的映射。 它什么都不做。
然后,假设你的方法有一个 return 类型的 String
并且用 @ResponseBody
注释,return 值将由 [=23= 处理] 将 Content-type
header 设置为 text/plain
。如果你想自己 return 一个 JSON 字符串并将 header 设置为 application/json
,请使用 return 类型的 ResponseEntity
(去掉@ResponseBody
) 并向其添加适当的 header。
@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);
}
请注意,您可能应该
<mvc:annotation-driven />
在您的 servlet 上下文配置中使用最合适的默认设置设置您的 MVC 配置。
正如其他人评论的那样,因为您的方法的 return 类型是 String
Spring 感觉不需要对结果做任何事情。
如果您更改签名以使 return 类型需要编组,那应该会有所帮助:
@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;
}
当我升级到 Spring 4 时,我需要按如下方式更新 jackson 依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.1</version>
</dependency>
我有@Greg 指定的依赖项post。我仍然面临这个问题,可以通过添加以下额外的 jackson 依赖项来解决它:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.7.4</version>
</dependency>
不完全适用于此 OP,但适用于遇到 404 且无法将响应 content-type
设置为 "application/json"
(任何 content-type
)的用户。一种可能是服务器实际响应 406,但资源管理器(例如,chrome)将其打印为 404。
如果您不自定义消息转换器,spring 将使用 AbstractMessageConverterMethodProcessor.java
。它将 运行:
List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(request);
List<MediaType> producibleMediaTypes = getProducibleMediaTypes(request, valueType, declaredType);
如果它们没有任何重叠(相同的项目),它会抛出 HttpMediaTypeNotAcceptableException
,这最终会导致 406。无论是 ajax 还是 GET/POST,或表单操作,如果请求 uri 以 .html
或任何后缀结尾,则 requestedMediaTypes
将是 "text/[that suffix]",这与 producibleMediaTypes
冲突,通常是:
"application/json"
"application/xml"
"text/xml"
"application/*+xml"
"application/json"
"application/*+json"
"application/json"
"application/*+json"
"application/xml"
"text/xml"
"application/*+xml"
"application/xml"
"text/xml"
"application/*+xml"