SpringMvc切换Json转换工具的操作代码
更新时间:2024年02月02日 16:10:30 作者: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转换工具内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
在SpringBoot项目中利用maven的generate插件
今天小编就为大家分享一篇关于在SpringBoot项目中利用maven的generate插件,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2019-01-01
利用Spring MVC+Mybatis实现Mysql分页数据查询的过程详解
这篇文章主要给大家介绍了关于利用Spring MVC+Mybatis实现Mysql分页数据查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。2017-08-08


最新评论