sping中配置json的各种问题
作者:互联网
本人搭建的是springboot是4.x版本有时候带来了很多的问题
问题一
在开始我们使用低版本的时候我们配置json返回的时候是这样的
<!--<!– https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl –>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!– https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl –>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>-->
这是springmvc.xml的配置文件我们这里的org.springframework.http.converter.json.MappingJacksonHttpMessageConverter这是低版本的
<!--<!–配置json解析–>-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</property>
</bean>
问题二
我们在配置高版本的时候有时候还是会出现一些问题
<!--<!– https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl –>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!– https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl –>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>-->
springmvc.xml 这样的配置还是可能会出现一些问题
<!--<!–配置json解析–>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</property>
</bean>-->
最终解决方案,可能不是很好,我们通过自己去转,不让sping替我们转了
配置pom文件 这里不需要再spingmvc.里面配置解析json的配置,但是我们还是需要配置基本的配置。
<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>
基本的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.注解扫描位置-->
<context:component-scan base-package="com.yuxiu.edu.eb.web.controller" />
<!-- 2.配置映射处理和适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 3.视图的解析器-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/Modules/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
自己转json的代码和实例
@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服务服务的数据:"+user);
responseJson(user);
}
//这里是通过自己的方法。解析方法
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();
}
}
}
标签:mapper,jackson,core,sping,配置,json,org,codehaus,asl 来源: https://blog.csdn.net/qq_28461661/article/details/89086642