springmvc:设置后端响应给前端的json数据转换成String格式

设置spring-mvc.xml:

'''xml

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
    <!--将bean交给dispatcherServlet使用-->
    <context:component-scan base-package="com.ekgc.controller"/>
    <!--    设置后端响应给前端的json数据转换成String格式-->
    <bean id="stringConverter"
          class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="writeAcceptCharset" value="false" />
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="stringConverter" />
                <ref bean="jsonConverter" />
            </list>
        </property>
    </bean>
</beans>

'''

web.xml读取spring-mvc.xml

'''xml

XML 复制代码
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-config.xml</param-value>
  </context-param>

<!-- 监听spring容器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

'''

相关推荐
开开心心就好7 分钟前
高效全能PDF工具,支持OCR识别
java·前端·python·pdf·ocr·maven·jetty
郭尘帅6662 小时前
vue3基础学习(上) [简单标签] (vscode)
前端·vue.js·学习
njsgcs2 小时前
opencascade.js stp vite webpack 调试笔记
开发语言·前端·javascript
T0uken3 小时前
【前端】:单 HTML 去除 Word 批注
前端·html·word
st紫月4 小时前
用vue和go实现登录加密
前端·vue.js·golang
岁岁岁平安4 小时前
Vue3学习(组合式API——计算属性computed详解)
前端·javascript·vue.js·学习·computed·计算属性
HWL56794 小时前
Express项目解决跨域问题
前端·后端·中间件·node.js·express
刺客-Andy5 小时前
React 第三十九节 React Router 中的 unstable_usePrompt Hook的详细用法及案例
前端·javascript·react.js
Go_going_5 小时前
【js基础笔记] - 包含es6 类的使用
前端·javascript·笔记
浩~~5 小时前
HTML5 浮动(Float)详解
前端·html·html5