提问人:Prova Henrique 提问时间:11/16/2023 更新时间:11/16/2023 访问量:40
Spring Boot 控制器中的 Lombok 注解导入问题
Lombok annotation import problem in spring boot controller
问:
我正在尝试使用 h2 数据库和 lombok 对 spring 进行 CRUD,它给了我这样的错误:
java.lang.IllegalArgumentException:给定的 id 不能为空
我的控制器:
package br.edu.unicesumar.crud.controller;
import br.edu.unicesumar.crud.model.domain.Pessoa;
import br.edu.unicesumar.crud.model.repository.PessoaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/pessoa")
public class PessoaController {
@Autowired
private PessoaRepository pessoaRepository;
@GetMapping
public List<Pessoa> all() {
return pessoaRepository.findAll();
}
@GetMapping("/{id}")
public Pessoa getById(@PathVariable Long id) {
return pessoaRepository.findById(id).orElse(null);
}
@PostMapping
public Pessoa create(@RequestBody Pessoa nova) {
return pessoaRepository.save(nova);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
pessoaRepository.deleteById(id);
}
}
我的实体服务:
package br.edu.unicesumar.crud.model.domain;
import jakarta.persistence.*;
@Entity
@Table(name = "ES_PESSOA") // para tabelas com nome diferente da entidade
public class Pessoa {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String nome;
@Column(name = "doc", length = 14) // se necessário, informar atribuições para a coluna
private String documento;
public Pessoa(Long id, String nome, String documento) {
this.id = id;
this.nome = nome;
this.documento = documento;
}
public Pessoa() {
}
// getters e setters necessários para serializar e deserializar a classe para json
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getDocumento() {
return documento;
}
public void setDocumento(String documento) {
this.documento = documento;
}
}
我的存储库:
package br.edu.unicesumar.crud.model.repository;
import br.edu.unicesumar.crud.model.domain.Pessoa;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PessoaRepository extends JpaRepository<Pessoa, Long> {
}
我想知道为什么即使代码中没有包含@notnull验证,也需要和应用验证。
答:
0赞
Pedro Henrique Lomba
11/16/2023
#1
我是Java的初学者,但是,我相信它可能与pom.xml有关 按照我的 pom 停止给出此错误:
<?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.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>br.edu.unicesumar</groupId>
<artifactId>crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>crud</name>
<description>Crud com MVC</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</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>
<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>
</plugins>
</build>
</project>
评论
JpaRespository.findById()
JpaRepository.deleteById()