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转换工具内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
一文深入理解Java中的java.lang.reflect.InvocationTargetException错误
这篇文章主要给大家介绍了关于Java中java.lang.reflect.InvocationTargetException错误的相关资料,java.lang.reflect.InvocationTargetException是Java中的一个异常类,它通常是由反射调用方法时抛出的异常,需要的朋友可以参考下2024-03-03
MyBatis JdbcType 与Oracle、MySql数据类型对应关系说明
这篇文章主要介绍了MyBatis JdbcType 与Oracle、MySql数据类型对应关系说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-09-09
SpringBoot使用@NotEmpty、@NotBlank、@NotNull注解进行参数校验
我们经常需要对请求参数进行校验,本文主要介绍了SpringBoot使用@NotEmpty、@NotBlank、@NotNull注解进行参数校验,具有一定的参考价值,感兴趣的可以了解一下2024-08-08


最新评论