2021年1月28日星期四

Spring Data Rest problems with Project Lombok

Is there any known problems in using Spring Data Rest with Lombok?

I have a Spring Boot project with Spring Data JPA, Spring Data REST, MySql connector and Project Lombok as dependencies.

If I define a simple entity (with name and description fields):

@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;    }  

Then building and running the service, I can invoke it with a curl command to create (POST) a new entity:

 curl -d "{ \"name\":\<TYPE NAME>\", \"description\":\"<TYPE DESC>\" }" -H "Content-Type: application/json" -X POST http://localhost:8082/deviceTypes | jq     

correctly returning the json resource:

{    "name": "<TYPE NAME>",    "description": "<TYPE DESC>",    "_links": {      "self": {        "href": "http://localhost:8082/deviceTypes/22"      },      "deviceType": {        "href": "http://localhost:8082/deviceTypes/22"      }    }  }  

If add the @RestRepository annotation to change the endpoint name:

@Entity  @Table(name = "Devicetype")  @Data  @NoArgsConstructor  @RestResource(path = "types", rel = "types")  public class DeviceType {      .      .      .  }  

Then the POST results in not adding the two fields to the generated entity

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"          }        }      }  

This is a simple entity, but other that I need to add contains a lot of fields, so not using the @Data annotation would represent a big loss.

The following is my pom.xml file. In there any problem of incompatibility between lombok and 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>  
https://stackoverflow.com/questions/65939000/spring-data-rest-problems-with-project-lombok January 28, 2021 at 10:21PM

没有评论:

发表评论