第十三章 springboot + lombok
lombok作用:消除模板代码。
- getter、setter、构造器、toString()、equals()
- 便捷的生成比较复杂的代码,例如一个POJO要转化成构建器模式的形式,只需要一个注解。
注意:使用之前,做以下几步让eclipse支持该注解。
- 先下载lombok.jar: https://projectlombok.org/download.html
- 双击lombok.jar,一直选到eclipse.ini文件,点击"install/update"
- 重启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的更多相关文章
- 第二十三章 springboot + 全局异常处理
一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...
- 第十六章 springboot + OKhttp + String.format
模拟浏览器向服务器发送请求四种方式: jdk原生的Http包下的一些类 httpclient(比较原始,不怎么用了):第一章 HttpClient的使用 Okhttp(好用,推荐) retrofit( ...
- PRML读书会第十三章 Sequential Data(Hidden Markov Models,HMM)
主讲人 张巍 (新浪微博: @张巍_ISCAS) 软件所-张巍<zh3f@qq.com> 19:01:27 我们开始吧,十三章是关于序列数据,现实中很多数据是有前后关系的,例如语音或者DN ...
- <构建之法>第十三章到十七章有感以及这个项目读后感
<构建之法>第十三章到十七章有感 第13章:软件测试方法有哪些? 主要讲了软件测试方法:要说有什么问题就是哪种效率最高? 第14章:质量保障 软件的质量指标是什么?怎么样能够提升软件的质量 ...
- 《Linux命令行与shell脚本编程大全》 第二十三章 学习笔记
第二十三章:使用数据库 MySQL数据库 MySQL客户端界面 mysql命令行参数 参数 描述 -A 禁用自动重新生成哈希表 -b 禁用 出错后的beep声 -B 不使用历史文件 -C 压缩客户端和 ...
- 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高
第十一章 搭建云端服务器 该章主要介绍了移动后端服务的概念以及Bmob的使用,比较简单,所以略过不总结. 第十三章 Android实例提高 该章主要介绍了拼图游戏和2048的小项目实例,主要是代码,所 ...
- Gradle 1.12 翻译——第十三章 编写构建脚本
有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...
- [汇编学习笔记][第十三章int指令]
第十三章int指令 13.1 int指令 格式: int n, n 为中断类型码 可以用int指令调用任何一个中断的中断处理程序(简称中断例程). 13.4 BIOS和DOS 所提供的中断例程 BIO ...
- perl5 第十三章 Perl的面向对象编程
第十三章 Perl的面向对象编程 by flamephoenix 一.模块简介二.Perl中的类三.创建类四.构造函数 实例变量 五.方法六.方法的输出七.方法的调用八.重载九.析构函数十.继承十一. ...
随机推荐
- Mybatis 源码分析之事物管理
Mybatis 提供了事物的顶层接口: public interface Transaction { /** * Retrieve inner database connection * @retur ...
- 《jQuery基础教程》读书笔记
最近在看<jQuery基础教程>这本书,做了点读书笔记以备回顾,不定期更新. 第一章第二章比较基础,就此略过了... 第三章 事件 jQuery中$(document).ready()与j ...
- SKLearn数据集API(二)
注:本文是人工智能研究网的学习笔记 计算机生成的数据集 用于分类任务和聚类任务,这些函数产生样本特征向量矩阵以及对应的类别标签集合. 数据集 简介 make_blobs 多类单标签数据集,为每个类分配 ...
- BZOJ 3572: [Hnoi2014]世界树 虚树 树形dp
https://www.lydsy.com/JudgeOnline/problem.php?id=3572 http://hzwer.com/6804.html 写的时候参考了hzwer的代码,不会写 ...
- [BZOJ2669] [cqoi2012]局部极小值
[BZOJ2669] [cqoi2012]局部极小值 Description 有一个n行m列的整数矩阵,其中1到nm之间的每个整数恰好出现一次.如果一个格子比所有相邻格子(相邻是指有公共边或公共顶点) ...
- python IDLE 自动提示功能
\Python27\Lib\idlelib\目录下 config-extensions.def文件修改等待时间 [AutoComplete] enable=1 popupwait=2000(2000表 ...
- UVALive 6661 Equal Sum Sets
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- AppDelegate 方法介绍
// // AppDelegate.swift // SwifyDemo import UIKit import CoreData @UIApplicationMain // 入口函数 UIApp ...
- ROS知识(19)----写一个简单的pluginlib例子
参考资料: 官方教程:Writing and Using a Simple Plugin
- ROS知识(18)----Pluginlib原理
目录 Overview Example Providing a Plugin Registering/Exporting a Plugin The Plugin Description File Re ...