深圳网站开发语言,有哪些网站是静态网站,如何让单位网站做防护,企业网站欣赏郑州企业形象设计文章目录 前言一、配置文件二、配置类三、注解四、json工具类1. 工具内容2. 使用工具 前言
前端在给我发送请求的时候一般包含三个部分url#xff0c;header#xff0c;body。那么就会涉及我们后端如何接收这些请求参数并且我们处理完毕参数后前端又如何接收参数
通过url传… 文章目录 前言一、配置文件二、配置类三、注解四、json工具类1. 工具内容2. 使用工具 前言
前端在给我发送请求的时候一般包含三个部分urlheaderbody。那么就会涉及我们后端如何接收这些请求参数并且我们处理完毕参数后前端又如何接收参数
通过url传递参数一般情况我们不需要序列化/反序列化处理而通过body传递的参数我们就需要**反序列化**处理。处理完毕后参数返回给前端就是**序列化**
一、配置文件
在rouyi-admin的application.yml文件中配置了如下内容
spring:jackson:# 日期格式化date-format: yyyy-MM-dd HH:mm:ssserialization:# 格式化输出indent_output: false# 忽略无法转换的对象fail_on_empty_beans: falsedeserialization:# 允许对象忽略json中不存在的属性fail_on_unknown_properties: false二、配置类
位于package com.ruoyi.framework.config添加如下配置并且交给bean管理
Slf4j
Configuration
public class JacksonConfig {Beanpublic Jackson2ObjectMapperBuilderCustomizer customizer() {return builder - {// 全局配置序列化返回 JSON 处理JavaTimeModule javaTimeModule new JavaTimeModule();javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss);javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));builder.modules(javaTimeModule);builder.timeZone(TimeZone.getDefault());log.info(初始化 jackson 配置);};}
}三、注解
如果配置的无法满足需求可以通过注解的方式解决
/*** 搜索值*/
JsonIgnore // 不进行序列化与反序列化处理
TableField(exist false)
private String searchValue;/**
* 请求参数
*/
JsonInclude(JsonInclude.Include.NON_EMPTY) // 只要非空的时候进行序列化与反序列化处理
TableField(exist false)
private MapString, Object params new HashMap();JsonIgnore
JsonProperty // 指定别名
public String getPassword() {return password;
}JsonFormat(pattern yyyy-MM-dd) // 指定日期序列化与反序列化格式格式四、json工具类
1. 工具内容
位于package com.ruoyi.common.utils;包下
/*** JSON 工具类** author 芋道源码*/
// 生成一个私有的无参构造函数
NoArgsConstructor(access AccessLevel.PRIVATE)
public class JsonUtils {// 创建OBJECT_MAPPER实例对象private static final ObjectMapper OBJECT_MAPPER SpringUtils.getBean(ObjectMapper.class);// 获取OBJECT_MAPPER实例对象public static ObjectMapper getObjectMapper() {return OBJECT_MAPPER;}// 将对象转换为字符串public static String toJsonString(Object object) {if (ObjectUtil.isNull(object)) {return null;}try {return OBJECT_MAPPER.writeValueAsString(object);} catch (JsonProcessingException e) {throw new RuntimeException(e);}}// 将字符串转换为对象public static T T parseObject(String text, ClassT clazz) {if (StringUtils.isEmpty(text)) {return null;}try {return OBJECT_MAPPER.readValue(text, clazz);} catch (IOException e) {throw new RuntimeException(e);}}// 将字节数组转换为对象public static T T parseObject(byte[] bytes, ClassT clazz) {if (ArrayUtil.isEmpty(bytes)) {return null;}try {return OBJECT_MAPPER.readValue(bytes, clazz);} catch (IOException e) {throw new RuntimeException(e);}}// 将字符串转换为对象使用TypeReferencepublic static T T parseObject(String text, TypeReferenceT typeReference) {if (StringUtils.isBlank(text)) {return null;}try {return OBJECT_MAPPER.readValue(text, typeReference);} catch (IOException e) {throw new RuntimeException(e);}}// 将字符串转换为字典使用比hashmap更简单public static Dict parseMap(String text) {if (StringUtils.isBlank(text)) {return null;}try {return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class));} catch (MismatchedInputException e) {// 类型不匹配说明不是jsonreturn null;} catch (IOException e) {throw new RuntimeException(e);}}// 将字符串转换为字典列表public static ListDict parseArrayMap(String text) {if (StringUtils.isBlank(text)) {return null;}try {return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, Dict.class));} catch (IOException e) {throw new RuntimeException(e);}}// 将字符串转换为对象列表public static T ListT parseArray(String text, ClassT clazz) {if (StringUtils.isEmpty(text)) {return new ArrayList();}try {return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));} catch (IOException e) {throw new RuntimeException(e);}}}2. 使用工具 测试getObjectMapper方法 RestController
RequestMapping(/demo/test)
SaIgnore // 忽略校验
public class TestController {GetMapping(JsonUtils)public void testGetObjectMapper(){// 获取objectMapper ObjectMapper objectMapper JsonUtils.getObjectMapper();// 打印Console.log(objectMapper{},objectMapper);}
}测试toJsonString方法将对象转换为字符串 GetMapping(JsonUtils)public void testGetObjectMapper(){// 创建对象User user new User(张三 , 18);// 序列化String str JsonUtils.toJsonString(user);Console.log(str);}测试parseObject方法将字符串转换对象 GetMapping(JsonUtils)public void testGetObjectMapper(){// 创建jsonString json{\name\:\张三\,\age\:18};// 反序列化User user JsonUtils.parseObject(json, User.class);Console.log(user);}测试parseObject方法将字节数组转换对象 GetMapping(JsonUtils)public void testGetObjectMapper(){// 创建jsonString json{\name\:\张三\,\age\:18};// 反序列化User user JsonUtils.parseObject(StrUtil.utf8Bytes(json), User.class);Console.log(user);}测试parseObject方法将字符串转换为复杂类型 GetMapping(JsonUtils)public void testGetObjectMapper(){// 创建jsonString json[{\name\:\张三\,\age\:18}];// 反序列化ListUser users JsonUtils.parseObject(json, new TypeReferenceListUser() {});Console.log(users);}测试parseMap方法将字符串转换字典Dict继承了LinkedHashMap对其做了进一步增强 GetMapping(JsonUtils)public void testGetObjectMapper(){// 创建jsonString json{\name\:\张三\,\age\:18};// 反序列化Dict dict JsonUtils.parseMap(json);if (dict ! null) {Console.log(dict.get(name));}}测试parseArrayMap方法将字符串转换字典列表Dict继承了LinkedHashMap对其做了进一步增强 GetMapping(JsonUtils)public void testGetObjectMapper(){// 创建jsonString json[{\name\:\张三\,\age\:18}];// 反序列化ListDict dictList JsonUtils.parseArrayMap(json);if (CollectionUtil.isNotEmpty(dictList)) {for (Dict dict : dictList) {Console.log(name: {}, age: {}, dict.getStr(name), dict.getInt(age));}}}测试parseArrayMap方法将字符串转换字典列表 GetMapping(JsonUtils)public void testGetObjectMapper(){// 创建jsonString json[{\name\:\张三\,\age\:18}];// 反序列化ListUser userList JsonUtils.parseArray(json, User.class);if (CollectionUtil.isNotEmpty(userList)) {for (User user : userList) {Console.log(name: {}, age: {}, user.getName(), user.getAge());}}}