三元运算符
Boolean authorizationConsent = dto.getAuthorizationConsent();
boolean result = (authorizationConsent != null && authorizationConsent);
Optional 来处理 null 值
boolean result = Optional.ofNullable(dto.getAuthorizationConsent()).orElse(false);
功能:
将 dto.getAuthorizationConsent() 的返回值包装成一个 Optional 对象。
如果 dto.getAuthorizationConsent() 返回 null,则创建一个空的 Optional。
如果返回非 null,则创建一个包含该值的 Optional。
调用 dto.getAuthorizationConsent() 获取值。
使用 Optional.ofNullable() 将值包装为 Optional。
使用 .orElse(false) 提供默认值:
如果值为 null,返回 false。
如果值为非 null,返回实际的布尔值(true 或 false)
优化:
- 使用 @NotNull 注解的意义
(1) 数据验证
@NotNull 是 Java Bean Validation(JSR 380)规范中的一个注解,用于确保某个字段的值不能为 null。
如果字段为 null,验证框架会在运行时抛出验证异常,并返回指定的错误消息。
(2) 提前发现问题
在 DTO 或实体类中使用 @NotNull,可以在数据进入业务逻辑之前就进行验证,从而避免后续代码中对 null 值的处理。
(3) 提升代码质量
使用注解的方式可以让代码更加简洁,减少重复的 null 检查逻辑。
DTO 或实体类时,推荐优先使用包装类
对于 boolean 类型,无论字段是否存在或值为 null,它都会被设置为 false。
对于 Boolean 类型,字段不存在时为 null,值为 null 时也为 null。
5. 总结与建议
如果字段是 必填字段,并且总是有值(true 或 false),可以使用 boolean。
如果字段是 可选字段,或者需要区分 “未提供” 和 “值为 false” 的情况,应该使用 Boolean。
在实际开发中,尤其是在设计 DTO 或实体类时,推荐优先使用包装类(如 Boolean、Integer 等),因为它们更灵活,能够更好地处理 JSON 数据和其他外部输入
fastjson参考
参考:http://doc.yaojieyun.com/www.runoob.com/w3cnote/java-json-instro.html
参考: http://doc.yaojieyun.com/www.runoob.com/w3cnote/fastjson-intro.html
序列化:JSON.toJSONString(person)将Java 对象转换为 JSON 字符串;
反序列化:JSON.parseObject() 将 JSON 字符串转换为 Java 对象;
@JSONField(name=“AGE”, serialize=false)
@JSONField(name = “DATE OF BIRTH”, deserialize=false)
Object转为其他类型
Map<String,Object> map = JsonUtils.parseObject(s, Map.class);
String accessToken = (String) map.get("access_token");
// Integer 转化为 Long
Long expiresIn = Long.valueOf(map.get("expires_in").toString());
fastjson和jackson gson和fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
JSONObject jsonObject = JSONObject.parseObject(s);
String accessToken = jsonObject.getString("access_token");
long expiresIn = jsonObject.getLongValue("expires_in");
配置MessageConverter
字段配置优先级> 全局配置(MessageConverters注释的优先级 > application.yml)
@Data
public class TestPojo {
private String msg;
private String address;
@JsonInclude(JsonInclude.Include.ALWAYS)
private String data;
@JSONField(name="DATE OF BIRTH", format="yyyy-MM-dd HH:mm:ss", ordinal = 3)
private String time;
private Integer ok;
}
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
default-property-inclusion: NON_NULL
MessageConverters注释的优先级大于application.yml
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
converters.add(new ResourceHttpMessageConverter());
converters.add(new AllEncompassingFormHttpMessageConverter());
converters.add(jackson2HttpMessageConverter());
}
@Bean
public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
//忽略未知属性
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//日期格式转换
//mapper.setDateFormat(new SimpleDateFormat(DateUtils.DATE_PATTERN));
mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
mapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
//Long类型转String类型
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
mapper.registerModule(simpleModule);
converter.setObjectMapper(mapper);
return converter;
}
}
idea快速实现pojo与json转换编辑
参考: https://editor.csdn.net/md/?articleId=130853605
Gson之toJson和fromJson方法
参考: https://blog.csdn.net/qq_43842093/article/details/121258638