【问题标题】:Spring Data Rest problems with Project Lombok龙目岛项目的 Spring Data Rest 问题
【发布时间】: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


    【解决方案1】:

    根据Java doc of RestResource

    使用此注释存储库以影响其导出方式以及链接中 rel 属性的值。
    从 Spring Data REST 2.0 开始,更喜欢使用 RepositoryRestResource 也能够自定义存储库公开的项目资源的关系类型和描述。

    因此,不要将@RestResource(path = "types", rel = "types") 添加到类DeviceType,而是将@RepositoryRestResource(path = "types", collectionResourceRel = "types") 添加到存储库接口DeviceTypeRepository

    path 值定义了要导出此资源的路径段。所以URI从http://localhost:8082/deviceTypes变成http://localhost:8082/types。在 POST 请求中使用新的 URI。

    collectionResourceRel 值定义了在生成指向集合资源的链接时要使用的关系值,即_links 下的名称。在您的示例中,

        {
          "_links": {
            "self": {
              "href": "http://localhost:8082/types/22"
            },
            "types": {  <-- this name !!!!!!!!!!!!!!!!!!!!!!!!!
              "href": "http://localhost:8082/types/22"
            }
          }
        }
    

    示例项目已上传到Github。第一次提交在添加@RepositoryRestResource 之前,第二次提交在添加@RepositoryRestResource 之后。该项目包含测试用例来验证路径和集合关系的变化。

    【讨论】:

    • 我尝试按照您的建议替换注释,但不幸的是,这并不能解决我的问题。无论如何,谢谢。
    • @chrx 我添加了一个示例项目和测试用例来验证更改。可能存在未在问题中发布的配置类或文件。如果您还上传一个示例来演示您的问题,这将有很大帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-10-08
    • 2021-03-05
    • 2016-06-29
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    相关资源
    最近更新 更多