Java – use Lombok with gradle and spring boot

I'm trying to build a project with Lombok, which is the dependent project I have

dependencies {
   compile("org.springframework.boot:spring-boot-starter-thymeleaf")
   compile("org.springframework.social:spring-social-facebook")
   compile("org.springframework.social:spring-social-twitter")
   testCompile("org.springframework.boot:spring-boot-starter-test")
   testCompile("junit:junit")
   compile("org.springframework.boot:spring-boot-devtools")
   compile("org.springframework.boot:spring-boot-starter-data-jpa")
   compile("MysqL:mysql-connector-java")
   compileOnly("org.projectlombok:lombok:1.16.10")
}

I can include anonations, and I include Lombok. Com in the editor I can even use Lombok to compile code and cal the methods generated by Lombok

This is my entity:

@Data
@Entity
@Table(name = "TEA_USER",uniqueConstraints = {
    @UniqueConstraint(columnNames = { "USR_EMAIL" }),@UniqueConstraint(columnNames = { "USR_NAME" })
})
public class User {


   @NotNull
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name="USR_ID")
   private long id;

   @NotNull
   @Column(name="USR_FNAME")
   private String firstName;

   @NotNull
   @Column(name="USR_LNAME")
   private String lastName;


   @NotNull
   @Min(5)
   @Max(30)
   @Column(name="USR_NAME")
   private String username;

   @Column(name="USR_EMAIL")
   private String email;

   @Min(8)
   @NotNull
   @Column(name="USR_PASSWORD")
   private String password;
}

This is a compiled function:

@PostMapping("/registration/register")
public String doRegister (@modelattribute @Valid User user,BindingResult result){
    user.getEmail();
    System.out.println(user.getFirstName());
    if (result.hasErrors()) {
         return "register/customRegister";
    }
    this.userRepository.save(user);
    return "register/customRegistered";
}

But when I run bootrun and try to access the function, this is the exception I get:

org.springframework.beans.NotReadablePropertyException: Invalid property 'firstName' of bean class [com.lucasfrossard.entities.User]: Bean property 'firstName' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

However, if I include setters and getters manually, it will work properly I don't know what's going on and how to solve it Any ideas?

Solution

After a period of hard work, I found that this plug-in can solve this problem:

https://github.com/franzbecker/gradle-lombok

My gradle file looks like this:

buildscript {
    repositories {
        maven { url "https://repo.spring.io/libs-milestone" }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
        classpath 'org.springframework:springloaded:1.2.6.RELEASE'
    }
}

plugins {
    id 'io.franzbecker.gradle-lombok' version '1.8'
    id 'java'
}



apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'



jar {
    baseName = 'gs-accessing-facebook'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/libs-milestone" }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    // compile("org.projectlombok:lombok:1.16.10")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.social:spring-social-facebook")
    compile("org.springframework.social:spring-social-twitter")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("junit:junit")
    compile("org.springframework.boot:spring-boot-devtools")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("MysqL:mysql-connector-java")
}

idea {
    module {
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/main/")
    }
}

bootRun {
    addResources = true
    jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
}

I've encountered this plugin before, but I did something wrong and it didn't work I'm glad it works now

thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>