lombok的坑

我使用lombok的@Data注解带来的坑。

代码如下:

公共类:

bash 复制代码
package com.tyler.oshi.common;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author: TylerZhong
 * @description:
 */

@Data
@NoArgsConstructor
public class R {
    private int code;
    private String msg;
    private Object data;

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public R(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static R ok() {
        return new R(200, "success");
    }

    public static R ok(Object data) {
        return new R(200, "success", data);
    }

    public static R error() {
        return new R(500, "error");
    }

    public static R error(Object data) {
        return new R(500, "error", data);
    }

    public static R error(int code, String msg) {
        return new R(code, msg);
    }
}

接口:

java 复制代码
package com.tyler.oshi.controller;

import com.tyler.oshi.common.R;
import com.tyler.oshi.service.CpuLoadMetricsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: TylerZhong
 * @description:
 */
@RequestMapping("/metrics")
@RestController
public class CpuLoadRestController {

    @Autowired
    private CpuLoadMetricsService cpuLoadMetricsService;

    @GetMapping(value = "/cpuload")
    public R getCpuLoad() {
        double[] cpuLoad = cpuLoadMetricsService.getCpuLoad();
        return R.ok(cpuLoad);
    }
}

pom.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.10</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tyler</groupId>
    <artifactId>oshi-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>oshi-app</name>
    <description>oshi-app</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.oshi</groupId>
            <artifactId>oshi-core</artifactId>
            <version>6.6.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

postman请求返回如下:

json 复制代码
{
    "timestamp": "2025-04-10T02:26:12.897+00:00",
    "status": 406,
    "error": "Not Acceptable",
    "path": "/metrics/cpuload"
}

经排查,是lombok注解没有生效。

验证步骤:

检查 Lombok 是否生成 Getter

在 IDE 中编译项目,查看 R 类的编译后的类文件(位于 target/classes 或 build/classes),确认是否存在 getCode()、getMsg()、getData() 方法。

可以看到我的classpath下面的R没有给我生成 getter 方法:

我试了换成 @Getter 注解也没用。

解决方案:手动加上 Getter 和 Setter 方法:

java 复制代码
package com.tyler.oshi.common;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author: TylerZhong
 * @description:
 */

@Data
@NoArgsConstructor
public class R {
    private int code;
    private String msg;
    private Object data;

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public R(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static R ok() {
        return new R(200, "success");
    }

    public static R ok(Object data) {
        return new R(200, "success", data);
    }

    public static R error() {
        return new R(500, "error");
    }

    public static R error(Object data) {
        return new R(500, "error", data);
    }

    public static R error(int code, String msg) {
        return new R(code, msg);
    }

    // 手动添加Getter和Setter方法
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
相关推荐
不想写bug呀2 小时前
多线程案例——单例模式
java·开发语言·单例模式
心平愈三千疾3 小时前
通俗理解JVM细节-面试篇
java·jvm·数据库·面试
我不会写代码njdjnssj3 小时前
网络编程 TCP UDP
java·开发语言·jvm
第1缕阳光3 小时前
Java垃圾回收机制和三色标记算法
java·jvm
funnyZpC3 小时前
好用的文档工具👉smart-doc
java
一只叫煤球的猫4 小时前
🔥 同事混用@Transactional和TransactionTemplate被我怼了,三种事务管理到底怎么选?
java·spring boot·后端
华子w9089258599 天前
基于 SpringBoot+JSP 的医疗预约与诊断系统设计与实现
java·spring boot·后端
feifeigo1239 天前
Java 正则表达式高级用法
java·mysql·正则表达式
empti_9 天前
Java中的List实现类详解
java
亲爱的非洲野猪9 天前
一次性理解Java垃圾回收--简单直接方便面试时使用
java·jvm·面试