Various problems of configuring json in spinning

What I built is that springboot is version 4.x, which sometimes brings a lot of problems

Question 1

In the beginning, when we used the lower version, we configured json to return

 <!--&lt;!&ndash; https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl &ndash;&gt;
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        &lt;!&ndash; https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl &ndash;&gt;
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>-->

This is the configuration file of springmvc.xml. Our org.springframework.http.converter.json.MappingJacksonHttpMessageConverter is of low version

 <!--&lt;!&ndash;To configure json analysis&ndash;&gt-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
	<property name="messageConverters">
	<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
	</property>
</bean>

Question two

There are still some problems when we configure the higher version

 <!--&lt;!&ndash; https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl &ndash;&gt;
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        &lt;!&ndash; https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl &ndash;&gt;
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>-->

Configuration like springmvc.xml may have some problems

 <!--&lt;!&ndash;To configure json analysis&ndash;&gt;
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </property>
    </bean>-->

The final solution may not be very good. Let's go around by ourselves and don't let Ping do it for us

There is no need to configure the configuration of parsing json in spingmvc. But we still need to configure the basic configuration.

 <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>9.0.17</version>
        </dependency>

        <!--jackson-->
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.8</version>
        </dependency>

Basic configuration of spingmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 1.Annotation scan location-->
    <context:component-scan base-package="com.yuxiu.edu.eb.web.controller" />

    <!-- 2.Configure map processing and adapters-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>


    <!-- 3.View parser-->
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/Modules/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

Code and instance of transferring json by yourself

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("find")
    @ResponseBody
    public void find(Integer id){
        User user =   userService.findById(id);
        System.out.println("dubbo Data of service service:"+user);
        responseJson(user);
    }

	//This is through my own way. analytic method
    public void responseJson(Object obj){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setHeader("Context-Type","text/json;charset=utf-8");
        ObjectMapper mapper = new ObjectMapper();
        try{
            response.getWriter().write(mapper.writeValueAsString(obj));
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

Keywords: JSON Spring xml Tomcat

Added by p0pb0b on Thu, 05 Dec 2019 09:57:36 +0200