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>

'''

相关推荐
inxunoffice14 分钟前
批量将 JSON 转换为 Excel/思维导入等其它格式
json·excel
luback27 分钟前
页面编辑器CodeMirror初始化不显示行号或文本内容
前端·codemirror
一勺-_-28 分钟前
Chrome浏览器和Microsoft Edge浏览器的导出收藏链接
前端·chrome·edge
无名之逆34 分钟前
[特殊字符] 超轻高性能的 Rust HTTP 服务器 —— Hyperlane [特殊字符][特殊字符]
java·服务器·开发语言·前端·网络·http·rust
蘑菇头爱平底锅40 分钟前
数字孪生-DTS-孪创城市-导览功能、虚拟现实
前端·数据可视化
一口一个橘子1 小时前
[ctfshow web入门] web40
前端·web安全·网络安全
Z编程1 小时前
vue3实现markdown工具栏的点击事件监听
前端·javascript·vue.js
华科云商xiao徐1 小时前
多语言编写的图片爬虫教程
前端
日升_rs1 小时前
Electron 开发:获取当前客户端 IP
前端·javascript
华科云商xiao徐1 小时前
Python使用爬虫IP抓取数据过程
前端