程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring MVC 4:“ application / json”内容类型未正确设置大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring MVC 4:“ application / json”内容类型未正确设置?

开发过程中遇到Spring MVC 4:“ application / json”内容类型未正确设置的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring MVC 4:“ application / json”内容类型未正确设置的解决方法建议,希望对你解决Spring MVC 4:“ application / json”内容类型未正确设置有所启发或帮助;

要了解的第一件事是,requestMapping#produces()在元素

@requestMapPing(value = "/Json", method = requestMethod.GET, produces = "application/Json")

仅用于限制请求处理程序的映射。

然后,假设您的方法的返回类型为,String并带有注释@ResponseBody,则将通过处理返回值并将StringhttpmessageConverter其设置Content- typetext/plain。如果您想自己返回JsON字符串并将标头设置为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);
}

请注意,您可能应该有

<mvc:Annotation-driven />

在Servlet上下文配置中使用最合适的默认设置来设置MVC配置。

解决方法

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

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

我返回了一个有效的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/XMLscheR_400_11845@a-instance"
    xsi:scheR_400_11845@aLOCATIOn="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/scheR_400_11845@a/beans"
    xmlns:context="http://www.springframework.org/scheR_400_11845@a/context"
    xmlns:xsi="http://www.w3.org/2001/XMLscheR_400_11845@a-instance" xmlns:p="http://www.springframework.org/scheR_400_11845@a/p"
    xsi:scheR_400_11845@aLOCATIOn="
    http://www.springframework.org/scheR_400_11845@a/beans     
    http://www.springframework.org/scheR_400_11845@a/beans/spring-beans-4.1.xsd
    http://www.springframework.org/scheR_400_11845@a/context 
    http://www.springframework.org/scheR_400_11845@a/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作为我的应用服务器。

大佬总结

以上是大佬教程为你收集整理的Spring MVC 4:“ application / json”内容类型未正确设置全部内容,希望文章能够帮你解决Spring MVC 4:“ application / json”内容类型未正确设置所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。