SpringMvc切换Json转换工具的操作代码
作者:org0610
SpringBoot切换使用goolge的Gson作为SpringMvc的Json转换工具,本文给大家讲解SpringMvc切换Json转换工具的操作代码,感兴趣的朋友一起看看吧
SpringMvc切换Json转换工具
SpringBoot切换使用goolge的Gson作为SpringMvc的Json转换工具
<!-- gson依赖 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>
@Configuration
public class JsonWebConfig {
@Bean
public GsonHttpMessageConverter gsonHttpMessageConverter() {
return new GsonHttpMessageConverter();
}
}


补充:
Spring使用json转换工具
一、直接调用
1、导入坐标
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>2、使用工具
User user = new User();
user.setName("lisi");
user.setAge(18);
//使用json的转换工具
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
return json;二、使用适配器
1、在spring-mvc.xml中进行配置
<!-- 配置处理器映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>2、直接使用,返回的就是json格式
User user = new User();
user.setName("lisi");
user.setAge(18);
return user;三、使用注解配置
1、在spring-mvc.xml中进行配置,先添加命名空间
<!-- mvc的注解驱动-->
<mvc:annotation-driven/>到此这篇关于SpringMvc切换Json转换工具的文章就介绍到这了,更多相关SpringMvc Json转换工具内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
