zoukankan      html  css  js  c++  java
  • 第十三章 springboot + lombok

    lombok作用:消除模板代码。

    • getter、setter、构造器、toString()、equals()
    • 便捷的生成比较复杂的代码,例如一个POJO要转化成构建器模式的形式,只需要一个注解。

    注意:使用之前,做以下几步让eclipse支持该注解。

    需求:我这里假设有一个field比较多的POJO,我想使用构建器模式对其进行操作。(使用构建器模式的场景:effective java第二版 第2条

    1、项目中引入lombok

    1         <!-- import lombok -->
    2         <dependency>
    3             <groupId>org.projectlombok</groupId>
    4             <artifactId>lombok</artifactId>
    5             <version>1.16.8</version>
    6             <scope>provided</scope>
    7         </dependency>
    View Code

    2、com.xxx.firstboot.domain.Address

     1 package com.xxx.firstboot.domain;
     2 
     3 import lombok.Builder;
     4 
     5 @Builder
     6 public class Address {
     7     private int id;
     8     private String province;
     9     private String city;
    10     private String country;
    11 }
    View Code

    说明:@Builder将该类生成一个构建器模式的类。可以查看outline窗口。如下:

    3、com.xxx.firstboot.web.AddressController

     1 package com.xxx.firstboot.web;
     2 
     3 import org.springframework.web.bind.annotation.RequestMapping;
     4 import org.springframework.web.bind.annotation.RestController;
     5 
     6 import com.xxx.firstboot.domain.Address;
     7 
     8 @RestController
     9 @RequestMapping("/address")
    10 public class AddressController {
    11     
    12     @RequestMapping("/getAddress")
    13     public Address getAddress(){
    14         Address address = Address.builder().province("内蒙古自治区")
    15                                            .city("呼和浩特市")
    16                                            .country("回民区")
    17                                            .build();
    18         return address;
    19     }
    20     
    21 }
    View Code

    说明:注意上边构建Address类并设置属性的方式。

    测试:启动应用,打开swagger,访问即可。结果出错如下,

    这应该是Jackson转换器需要使用Address类属性的getter方法,而我们值添加了@Builder注解,是没有getter方法的(这一点查看outline即可)

    为Address类添加@Getter注解,如下:

     1 package com.xxx.firstboot.domain;
     2 
     3 import lombok.Builder;
     4 import lombok.Getter;
     5 
     6 @Builder
     7 @Getter
     8 public class Address {
     9     private int id;
    10     private String province;
    11     private String city;
    12     private String country;
    13 }
    View Code

    查看outline,出现了各个属性的getter方法。

    再测试即可通过。

    参考:

    https://projectlombok.org/features/Builder.html :这篇文章详细介绍了@Builder注解的作用和用法,包括

    https://projectlombok.org/features/index.html所有的注解都在这篇文章

  • 相关阅读:
    BNU 51002 BQG's Complexity Analysis
    BNU OJ 51003 BQG's Confusing Sequence
    BNU OJ 51000 BQG's Random String
    BNU OJ 50999 BQG's Approaching Deadline
    BNU OJ 50998 BQG's Messy Code
    BNU OJ 50997 BQG's Programming Contest
    CodeForces 609D Gadgets for dollars and pounds
    CodeForces 609C Load Balancing
    CodeForces 609B The Best Gift
    CodeForces 609A USB Flash Drives
  • 原文地址:https://www.cnblogs.com/java-zhao/p/5465652.html
Copyright © 2011-2022 走看看