【发布时间】:2021-01-28 14:21:44
【问题描述】:
将 Spring Data Rest 与 Lombok 一起使用是否有任何已知问题?
我有一个 Spring Boot 项目,其中包含 Spring Data JPA、Spring Data REST、MySql 连接器和 Project Lombok 作为依赖项。
如果我定义一个简单的实体(带有名称和描述字段):
@Entity
@Table(name = "Devicetype")
@Data
@NoArgsConstructor
public class DeviceType {
/** Unique Entity Id */
@Id
@GeneratedValue
private long uid;
/** DeviceType name */
private String name;
/** DeviceType description */
private String description;
}
然后构建并运行该服务,我可以使用 curl 命令调用它来创建 (POST) 一个新实体:
curl -d "{ \"name\":\<TYPE NAME>\", \"description\":\"<TYPE DESC>\" }" -H "Content-Type: application/json" -X POST http://localhost:8082/deviceTypes | jq
正确返回json资源:
{
"name": "<TYPE NAME>",
"description": "<TYPE DESC>",
"_links": {
"self": {
"href": "http://localhost:8082/deviceTypes/22"
},
"deviceType": {
"href": "http://localhost:8082/deviceTypes/22"
}
}
}
如果添加@RestRepository注解改变端点名称:
@Entity
@Table(name = "Devicetype")
@Data
@NoArgsConstructor
@RestResource(path = "types", rel = "types")
public class DeviceType {
.
.
.
}
那么 POST 导致没有将这两个字段添加到生成的实体中
curl -d "{ \"name\":\"<TYPE NAME>\", \"description\":\"<TYPE DESC>\" }" -H "Content-Type: application/json" -X POST http://localhost:8082/deviceTypes | jq
{
"_links": {
"self": {
"href": "http://localhost:8082/types/22"
},
"types": {
"href": "http://localhost:8082/types/22"
}
}
}
这是一个简单的实体,但是我需要添加的其他包含很多字段,所以不使用@Data注解将是一个很大的损失。
以下是我的 pom.xml 文件。在lombok和spring data resta annotations之间有什么不兼容的问题吗?
<?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>2.5.0-M1</version>
<relativePath/>
</parent>
<groupId>eu.myapp</groupId>
<artifactId>myapp-service</artifactId>
<version>2.0.0-SNAPSHOT</version>
<name>myapp-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<docker.image.prefix>myapp.eu</docker.image.prefix>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<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>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.2.3</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<!-- Maven Dependency Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
【问题讨论】:
标签: spring-boot spring-data-rest lombok