Spring Boot项目遇到的知识点

目录

JsonProperty和JsonAlias注解

@JsonIgnore

@Enablescheduling注解

@ToString.Exclude注解

@SuperBuilder和@Builder注解

Optional

io.Swagger包

禁止将mutable对象定义为常量


JsonProperty和JsonAlias注解

@JsonProperty is a marker annotation to define logical property.

@JsonProperty can be annotated at non-static setter or getter method or non-static object field. The logical property is used in serialization and de-serialization of JSON.

@JsonAlias is introduced in Jackson 2.9 release.

@JsonAlias defines one or more alternative names for a property to be accepted during deserialization i.e. setting JSON data to Java object. But at the time of serialization i.e. while getting JSON from Java object, only actual logical property name is used and not alias.

 Jackson @JsonProperty and @JsonAlias Example

@JsonIgnore

此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。
此注解用于属性或者方法上(最好是属性上)

@Enablescheduling注解

通过在配置类注解@EnableScheduling来开启对定时任务的支持,然后在要执行计划任务的方法上注解@Scheduled,声明这是需执行的定时任务!

@EnableScheduling: 启动类里面使用@EnableScheduling 注解开启功能,开启自动扫描;
@Scheduled: 添加在需要执行的定时方法上

Springboot基于@EnableScheduling注解实现定时任务(一)_能先森的博客-CSDN博客_enablescheduling注解

@ToString.Exclude注解

使得toString输出的时候,不包括被这个注解修饰的属性。

@SuperBuilder和@Builder注解

idea的lombok插件支持@SuperBuilder注解啦 - JavaShuo

Optional

理解、学习与使用 Java 中的 Optional - 张伯雨 - 博客园

io.Swagger包

Swagger使用指南 - 一步之 - 博客园

禁止将mutable对象定义为常量

使用public static final的意图是定义一个常量。如果用其修饰一个mutable(可变)对象,极易产生不当使用,造成功能异常。

class Result {
    private int resultCode;

    public Result(int resultCode) {
        reset(resultCode);
    }
    public void reset(int resultCode) {
        this.resultCode = resultCode;
    }
}
// Result是一个mutable类,实例出来的SUCCESS即便用了public static final修饰,也仍是mutable对象
public static final Result SUCCESS = new Result("Success");

public void foo() {
    SUCCESS.reset("Failure"); // 不当使用:此时,SUCCESS这个“常量”已经被改变
    // 后续代码再引用SUCCESS将带来业务异常
    if (bar() == SUCCESS) {    
    }
}

// 使用Collections.unmodifiableList()以保证EMPTY_RESULT_LIST不可变
public static final List<String> EMPTY_RESULT_LIST = Collections.unmodifiableList(new ArrayList<>());


// 更自然的写法:Collections.emptyList()
public static final List<String> EMPTY_RESULT_LIST = Collections.emptyList();

//在Java 10及后续版本,List/Map/Set等都提供了 of() 方法,其返回的是immutable对象
public static final List<Integer> PRIME_NUMS = List.of(2, 3, 5, 7, 11, 13, 17, 19);

对于精确的数值计算(货币、金融等),建议使用int、long、BigDecimal等;


使用BigDecimal时,使用浮点数很容易导致精度损失。BigDecimal应该使用字符串格式的数值来构造,也就是说,它应该

应使用BigDecimal(String VAL)代替BigDecimal(double VAL)。

当任意一个操作数是NaN(Not a Number)时,运算结果会固定。比如

Double.NaN == Double.NaN结果总为false

要使用isNaN

com.github.benmanes.caffeine.cache.Cache

Caffeine是一种高性能的缓存库,是基于Java 8的最佳(最优)缓存框架。

最佳内存缓存框架Caffeine - 废物大师兄 - 博客园

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值