spring 中json使用
1.json序列化工具
public class JsonUtils {
/**
* Logger for this class
*/
private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
private final static ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
objectMapper.configure(JsonParser.Feature.INTERN_FIELD_NAMES, true);
objectMapper.configure(JsonParser.Feature.CANONICALIZE_FIELD_NAMES, true);
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
private JsonUtils() {
}
public static String encode(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonGenerationException e) {
logger.error("encode(Object)", e); //$NON-NLS-1$
} catch (JsonMappingException e) {
logger.error("encode(Object)", e); //$NON-NLS-1$
} catch (IOException e) {
logger.error("encode(Object)", e); //$NON-NLS-1$
}
return null;
}
/**
* 将json string反序列化成对象
*
* @param json
* @param valueType
* @return
*/
public static <T> T decode(String json, Class<T> valueType) {
try {
return objectMapper.readValue(json, valueType);
} catch (JsonParseException e) {
logger.error("decode(String, Class<T>)", e);
} catch (JsonMappingException e) {
logger.error("decode(String, Class<T>)", e);
} catch (IOException e) {
logger.error("decode(String, Class<T>)", e);
}
return null;
}
/**
* 将json array反序列化为对象
*
* @param json
* @param jsonTypeReference
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T decode(String json, TypeReference<T> typeReference) {
try {
return (T) objectMapper.readValue(json, typeReference);
} catch (JsonParseException e) {
logger.error("decode(String, JsonTypeReference<T>)", e);
} catch (JsonMappingException e) {
logger.error("decode(String, JsonTypeReference<T>)", e);
} catch (IOException e) {
logger.error("decode(String, JsonTypeReference<T>)", e);
}
return null;
}
}
2.springmvc中自定义JsonConvert
@ControllerAdvice
public class MyWebBindingInitializer implements WebBindingInitializer { @Autowired
private MappingJackson2HttpMessageConverter jackson2HttpMessageConverter; @InitBinder
@Override
public void initBinder(WebDataBinder webDataBinder) {
webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); ObjectMapper objectMapper=jackson2HttpMessageConverter.getObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
SimpleModule simpleModule = new SimpleModule();
objectMapper.registerModule(simpleModule);
objectMapper.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
} }
spring 中json使用的更多相关文章
- Spring学习---Spring中利用jackson进行JSON转换
Spring中利用jackson进行JSON转换 import java.util.List; import com.fasterxml.jackson.core.JsonProcessingExce ...
- Spring中文文档
前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...
- Spring中Ordered接口简介
目录 前言 Ordered接口介绍 Ordered接口在Spring中的使用 总结 前言 Spring中提供了一个Ordered接口.Ordered接口,顾名思义,就是用来排序的. Spring是一个 ...
- 不使用SpringBoot如何将原生Feign集成到Spring中来简化http调用
在微服务架构中,如果使用得是SpringCloud,那么只需要集成SpringFeign就可以了,SpringFeign可以很友好的帮我们进行服务请求,对象解析等工作. 然而SpingCloud是依赖 ...
- Spring对JSON请求加解密
Spring中处理JSON请求通常使用@RequestBody和@ResponseBody注解,针对JSON请求加解密和过滤字符串,Spring提供了RequestBodyAdvice和Respons ...
- Spring中添加新的配置表,并对新的配置表进行处理
实习过程中boss交代的任务(以下出现的代码以及数据只给出小部分,提供一个思路) 目的:Spring中添加新的配置表,并对新的配置表进行处理:替换的新的配置表要友好,同时保证替换前后功能不能发生变化. ...
- spring接收json字符串的两种方式
一.前言 前几天遇到一个问题,前端H5调用我的springboot一个接口(post方式,@RequestParameter接收参数),传入的参数接收不到.自己测试接口时使用postman的form- ...
- spring 中常用的配置项
1.spring 中常用的配置项 application.properties #端口 server.port=8081 #调试模式 debug=false #上下文 #一般情况下,小项目通常都是在t ...
- Spring中与Spring相关的注解
# 一.Spring的常用组件类注解 ## @Component 被该注解所修饰的类是一个普通的spring bean类,该注解可以替代@Controller.@Service.@Reposi ...
随机推荐
- day02_1spring3
面向切面编程.AOP手动代理和spring编写代理 一.什么是AOP 1.AOP简介: 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方 ...
- Django 初试水(一)
2020年注定是一个不平凡的一年!坚持就是胜利,一起加油! 至于为什么使用 Django,也不想说太多.个人喜欢,这里不做介绍.直接进入主题.show me the code!!! python 的环 ...
- Echart 地图实例
1.地图实例 function func_InEchart() { require.config({ paths: { echarts: '../Scripts/echart2/echarts' }, ...
- DTU DeepLearning: exercise 7
torch activation functions: sigmoid, relu, tanh, softplus. https://morvanzhou.github.io/tutorials/ma ...
- Python七夕记
- SpringBoot中的bean加载顺序
https://www.dazhuanlan.com/2019/10/22/5daebc5d16429/ 最近在做传统Spring项目到SpringBoot项目迁移过程中,遇到了一些bean加载顺序的 ...
- leetcode 925. Long Pressed Name
判定是否长按 var isLongPressedName = function (name, typed) { var i = 1, j = 0, n = name.length, m = typed ...
- Dockerfile深度剖析
Dockerfile主要是用来定制镜像 Dockerfile指令集 FROM ###指定基础镜像第一条指令.scratch是虚拟的镜像,表示一个空白的镜像. FROM centos:7.5 MAI ...
- c#解析json字符串处理
本文链接:https://blog.csdn.net/sajiazaici/article/details/77647625本文是全网第二简单的方法,因为我女票也发了一篇博客说是全网最简单的方法,我不 ...
- 并发之ATOMIC原子操作--Unsafe解析(三)
Atomic 类的原子操作是依赖java中的魔法类sun.misc.Unsafe来实现的,而这个类为我们提供了访问底层的机制,这种机制仅供java核心类库使用,而不应该被普通用户使用. 获取Unsaf ...