Spring MVC 4:“应用程序/json"内容类型设置不正确 [英] Spring MVC 4: "application/json" Content Type is not being set correctly

查看:32
本文介绍了Spring MVC 4:“应用程序/json"内容类型设置不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个映射有以下注释的控制器:

I have a controller mapped with the following annotation:

@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String bar() {
    return "{"test": "jsonResponseExample"}";
}

我返回了一个有效的 JSON 字符串,但是,当我在浏览器中的 Chrome Dev Tools 上查看响应时,内容类型不是 application/json 而只是普通的 text/html.为什么没有设置内容类型?

I return a valid JSON string however, the content-type when I view the response on Chrome Dev Tools in browser is not application/json but just plain text/html. Why is the content type not being set?

我的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 作为我的应用服务器.

Using WildFly 8.1 as my app server.

推荐答案

首先要了解的是 RequestMapping#produces() 元素在

First thing to understand is that the RequestMapping#produces() element in

@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")

仅用于限制请求处理程序的映射.它什么都不做.

serves only to restrict the mapping for your request handlers. It does nothing else.

然后,假设您的方法的返回类型为 String 并使用 @ResponseBody 进行注释,则返回值将由 StringHttpMessageConverter它将 Content-type 标头设置为 text/plain.如果你想自己返回一个JSON字符串并将头部设置为application/json,使用ResponseEntity的返回类型(去掉@ResponseBody>) 并为其添加适当的标题.

Then, given that your method has a return type of String and is annotated with @ResponseBody, the return value will be handled by StringHttpMessageConverter which sets the Content-type header to text/plain. If you want to return a JSON string yourself and set the header to application/json, use a return type of ResponseEntity (get rid of @ResponseBody) and add appropriate headers to it.

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

<小时>

请注意,您可能应该拥有


Note that you should probably have

<mvc:annotation-driven /> 

在您的 servlet 上下文配置中,使用最合适的默认值设置您的 MVC 配置.

in your servlet context configuration to set up your MVC configuration with the most suitable defaults.

这篇关于Spring MVC 4:“应用程序/json"内容类型设置不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆