我用以下注释映射了一个控制器:

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

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

My 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>

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

推荐答案

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

请注意,您可能应该

<mvc:annotation-driven /> 

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

Json相关问答推荐

简单条形图和有序分类中条形图的 colored颜色 梯度

规范化JSON数据

jq:将一个数组与多个数组连接起来

PostgreSQL:删除 JSONB 数组中每个元素的特定键

Spark-SQL中的from_unixtime函数未能给出正确的输出

如何强制仅有一个元素的数组在JSON中生成方括号

错误解析错误:意外令牌:在我的 .eslintrc.json 文件中.为什么?

添加到数组时出错:找不到Add的重载和参数计数:1

Oracle Apex - 将 JSON 对象分配给变量以返回

SwiftUI:如何使用 0 索引数组键为 JSON 添加类型

当值包含ansible中的字符串时解析json值

如何在 Django 的模板语言中获取 json 键和值?

使用基本身份验证通过 CURL 发布 JSON

一起使用 Argparse 和 Json

json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

JSON对象中的JavaScript递归搜索

如何使用 LWP 发出 JSON POST 请求?

如何使用 Jackson 注释从 HttpResponse 反序列化 JSON 对象?

在 Java 中比较两个 JSON 文件的最佳方法

请求返回字节,我无法解码它们