lombok作用:消除模板代码。

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

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

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

1、项目中引入lombok

         <!-- import lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
<scope>provided</scope>
</dependency>

2、com.xxx.firstboot.domain.Address

 package com.xxx.firstboot.domain;

 import lombok.Builder;

 @Builder
public class Address {
private int id;
private String province;
private String city;
private String country;
}

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

3、com.xxx.firstboot.web.AddressController

 package com.xxx.firstboot.web;

 import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.xxx.firstboot.domain.Address; @RestController
@RequestMapping("/address")
public class AddressController { @RequestMapping("/getAddress")
public Address getAddress(){
Address address = Address.builder().province("内蒙古自治区")
.city("呼和浩特市")
.country("回民区")
.build();
return address;
} }

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

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

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

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

 package com.xxx.firstboot.domain;

 import lombok.Builder;
import lombok.Getter; @Builder
@Getter
public class Address {
private int id;
private String province;
private String city;
private String country;
}

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

再测试即可通过。

参考:

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

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

第十三章 springboot + lombok的更多相关文章

  1. 第二十三章 springboot + 全局异常处理

    一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...

  2. 第十六章 springboot + OKhttp + String.format

    模拟浏览器向服务器发送请求四种方式: jdk原生的Http包下的一些类 httpclient(比较原始,不怎么用了):第一章 HttpClient的使用 Okhttp(好用,推荐) retrofit( ...

  3. PRML读书会第十三章 Sequential Data(Hidden Markov Models,HMM)

    主讲人 张巍 (新浪微博: @张巍_ISCAS) 软件所-张巍<zh3f@qq.com> 19:01:27 我们开始吧,十三章是关于序列数据,现实中很多数据是有前后关系的,例如语音或者DN ...

  4. <构建之法>第十三章到十七章有感以及这个项目读后感

    <构建之法>第十三章到十七章有感 第13章:软件测试方法有哪些? 主要讲了软件测试方法:要说有什么问题就是哪种效率最高? 第14章:质量保障 软件的质量指标是什么?怎么样能够提升软件的质量 ...

  5. 《Linux命令行与shell脚本编程大全》 第二十三章 学习笔记

    第二十三章:使用数据库 MySQL数据库 MySQL客户端界面 mysql命令行参数 参数 描述 -A 禁用自动重新生成哈希表 -b 禁用 出错后的beep声 -B 不使用历史文件 -C 压缩客户端和 ...

  6. 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高

    第十一章 搭建云端服务器 该章主要介绍了移动后端服务的概念以及Bmob的使用,比较简单,所以略过不总结. 第十三章 Android实例提高 该章主要介绍了拼图游戏和2048的小项目实例,主要是代码,所 ...

  7. Gradle 1.12 翻译——第十三章 编写构建脚本

    有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...

  8. [汇编学习笔记][第十三章int指令]

    第十三章int指令 13.1 int指令 格式: int n, n 为中断类型码 可以用int指令调用任何一个中断的中断处理程序(简称中断例程). 13.4 BIOS和DOS 所提供的中断例程 BIO ...

  9. perl5 第十三章 Perl的面向对象编程

    第十三章 Perl的面向对象编程 by flamephoenix 一.模块简介二.Perl中的类三.创建类四.构造函数 实例变量 五.方法六.方法的输出七.方法的调用八.重载九.析构函数十.继承十一. ...

随机推荐

  1. 图解在Eclipse中如何上传项目到svn

    方法/步骤 1.在Eclipse中新建project,如下图所示: 2.右键project --> team --> share project,如下图所示: 3.选择repository ...

  2. 深入理解ajax系列第四篇

    前面的话 现代Web应用中频繁使用的一项功能就是表单数据的序列化,XMLHttpRequest 2级为此定义了FormData类型.FormData为序列化表单以及创建与表单格式相同的数据提供了便利. ...

  3. JavaScript ES6箭头函数指南

    前言 胖箭头函数(Fat arrow functions),又称箭头函数,是一个来自ECMAScript 2015(又称ES6)的全新特性.有传闻说,箭头函数的语法=>,是受到了CoffeeSc ...

  4. 转 MySQL连接超时

    在负载较重的MySQL服务器上,有时你偶尔会看到一些连接超时的错误,诸如: Can’t connect to MySQL server on ‘mydb’(110).如果当时你有多个连接请求,你会发现 ...

  5. hdu 4403 爆搜

    题意:给一串数字,在其间加入若干加号和一个等号,问使等式成立的方案总数 if the digits serial is "1212", you can get 2 equation ...

  6. String 字符串详解 / 常用API

    String 详解 / 常用API 简介 String 是不可改变的字符串序列.String 为字符串常量 StringBuilder 与StringBuffer 均为可改变的字符串序列.为字符串变量 ...

  7. Loadrunner问题:Monitor name :Windows Resources. Cannot create measurement Processor|% Processor Time|_Total on machine 192.168.0.1

    说明: 在Loadrunner监控windows系统资源的时候,在添加好windows Resources后运行发现报如下错误: int: Check that there is such a mea ...

  8. chrome --headless --disable-gpu --dump-dom http://www.python.org

    Driving Headless Chrome with Python:Python chrome --headless --disable-gpu --dump-dom http://www.pyt ...

  9. css3实现卷页效果http://jingyan.baidu.com/article/73c3ce2806aef9e50343d93a.html

    css3实现卷页效果 | 浏览:31 | 更新:2015-01-08 13:30 1 2 3 4 5 6 7 分步阅读 百度经验:jingyan.baidu.com 页面上经常会看到鼠标移动上去,对象 ...

  10. 背景建模或前景检測之PBAS

    申明,本文非笔者原创,原文转载自:http://blog.csdn.net/kcust/article/details/9931575 Pixel-Based Adaptive Segmenter(P ...