Olá.
Eu estou fazendo o exemplo do vídeo Como criar uma REST API com Spring Boot (tutorial passo a passo) - YouTube, porém apresentou um erro que eu não consegui identificar o motivo e como corrigir.
Erro apresentado no postman:
“timestamp”: “2022-04-13T10:56:04.201+00:00”,
“status”: 500,
“error”: “Internal Server Error”,
“trace”: FICOU GIGANTE
“message”: “not-null property references a null or transient value : com.example.crm.model.Cliente.nome; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.example.crm.model.Cliente.nome”,
“path”: “/clientes/”
erro apresentado no eclipse:
2022-04-13 07:56:04.198 ERROR 28856 — [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.example.crm.model.Cliente.nome; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.example.crm.model.Cliente.nome] with root cause
Código:
package com.example.crm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CrmApiApplication {
public static void main(String[] args) {
SpringApplication.run(CrmApiApplication.class, args);
}
}
package com.example.crm.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Data
@Entity
public class Cliente {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String nome;
@Column(nullable = false)
private String texto;
}
package com.example.crm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.example.crm.model.Cliente;
import com.example.crm.repository.ClienteRepository;
@RestController
@RequestMapping("/clientes")
public class ClienteController {
@Autowired
private ClienteRepository clienteRepository;
@GetMapping
public List<Cliente> listar(){
return clienteRepository.findAll();
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Cliente adicionar(@RequestBody Cliente cliente) {
return clienteRepository.save(cliente);
}
}
package com.example.crm.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.crm.model.Cliente;
@Repository
public interface ClienteRepository extends JpaRepository<Cliente, Long>{
}