Lombok错误解决

这个有可能是Lombok新手使用中最常见的错误

有如下代码是引入了Lombok工具类的。

@Data
public class User {

    private int id;
    private String name;
    private int age;
    private String address;
    private String mobile;
}
@Slf4j
public class HomeController {

    @RequestMapping("/")
    public
    @ResponseBody
    User home(){
        User user = new User();
        user.setId(1);
        user.setName("youming");
        log.info(user.toString());
        return  user;
    }
}

在IntelliJ IDEA运行时找不到log,User类的getset方法也找不到,错误输出如下图所示。
这里写图片描述

开发环境:Java8+Maven+IntelliJ IDEA(预装了Lombok Plugin)。

解决思路:
1、Lombok jar是否引入的有问题?

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>${lombok.version}</version>
    <scope>provided</scope>
</dependency>

查看了jar包引入没有任何问题,而且代码编辑区中的User类和HomeController类也没有报错,说明jar是正确引入的。

2、编译时出错了?
网上搜索了之后,发现有可能是编译时没有enable注解处理器。而且在我重新打开项目以后编译器也提示我需要打开Annotation Processors。
这里写图片描述
如上图所示,Annotation Processors > Enable annotation processing。设置完成之后程序正常运行。

Lombok是如何工作的?

编译时通过注解来生成相应的代码,如最上方的两个代码块中,我使用到了@Data@Slf4j@Data 注解将会被注解处理器识别并生成getter、setter、toString、以及hashcode、equals等方法
这个我们可以反编译一下User.class

package cn.gov.zcy.entity;

public class User {
    private int id;
    private String name;
    private int age;
    private String address;
    private String mobile;

    public User() {
    }

    public User(int id, String name, int age, String address, String mobile) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
        this.mobile = mobile;
    }

    public User(String name, int age, String address, String mobile) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.mobile = mobile;
    }

    public User(String name) {
        this.name = name;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public int getAge() {
        return this.age;
    }

    public String getAddress() {
        return this.address;
    }

    public String getMobile() {
        return this.mobile;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof User)) {
            return false;
        } else {
            User other = (User)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getId() != other.getId()) {
                return false;
            } else {
                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                if (this.getAge() != other.getAge()) {
                    return false;
                } else {
                    Object this$address = this.getAddress();
                    Object other$address = other.getAddress();
                    if (this$address == null) {
                        if (other$address != null) {
                            return false;
                        }
                    } else if (!this$address.equals(other$address)) {
                        return false;
                    }

                    Object this$mobile = this.getMobile();
                    Object other$mobile = other.getMobile();
                    if (this$mobile == null) {
                        if (other$mobile != null) {
                            return false;
                        }
                    } else if (!this$mobile.equals(other$mobile)) {
                        return false;
                    }

                    return true;
                }
            }
        }
    }

    public boolean canEqual(Object other) {
        return other instanceof User;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        int result = result * 31 + this.getId();
        Object $name = this.getName();
        result = result * 31 + ($name == null ? 0 : $name.hashCode());
        result = result * 31 + this.getAge();
        Object $address = this.getAddress();
        result = result * 31 + ($address == null ? 0 : $address.hashCode());
        Object $mobile = this.getMobile();
        result = result * 31 + ($mobile == null ? 0 : $mobile.hashCode());
        return result;
    }

    public String toString() {
        return "User(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ", address=" + this.getAddress() + ", mobile=" + this.getMobile() + ")";
    }
}

好用!强大!虽然我们也可以通过编译器去生成gettersetter 方法,但是很多时候我们并需要知道这些方法,而这些方法放在代码里也会影响我们阅读。
当然了,我们很多时候并不需要使用@Data 这么全面的注解,我们可以按需选用@Getter@Setter注解。

@Slf4j注解

package cn.gov.zcy.controller;

import cn.gov.zcy.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {
    private static final Logger log = LoggerFactory.getLogger(HomeController.class);

    public HomeController() {
    }

    @RequestMapping({"/"})
    @ResponseBody
    public User home() {
        User user = new User();
        user.setId(1);
        user.setName("youming");
        log.info(user.toString());
        return user;
    }
}
private static final Logger log = LoggerFactory.getLogger(HomeController.class);

@Slf4j会生成一个log变量,这样我们就可以直接使用log了,而无需去声明一个log。

  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 26
    评论
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值