-3
@PostMapping//Create
public ResponseEntity<Categoria> criar(@RequestBody Categoria categoria, HttpServletResponse response) {
Categoria categoriaSalva = categoriaRepository.save(categoria);
URI uri = ServletUriComponentsBuilder.fromCurrentRequestUri().path("/{codigo}").buildAndExpand(categoriaSalva.getCodigo()).toUri();
return ResponseEntity.created(uri).body(categoriaSalva);
}
category file.java
package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "categoria")
public class Categoria {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long codigo;
private String nome;
public Long getCodigo() {
return codigo;
}
public void setCodigo(Long codigo) {
this.codigo = codigo;
}
public String getNome() {
return nome;
}
public void setNome(String name) {
this.nome = nome;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Categoria other = (Categoria) obj;
if (codigo == null) {
if (other.codigo != null)
return false;
} else if (!codigo.equals(other.codigo))
return false;
return true;
}
}
Send more information about the error, copy and paste the console content
– J. Pedro A.
java.sql.Sqlintegrityconstraintviolationexception: Column 'name' cannot be null ... 2018-09-19 17:37:09.020 DEBUG 3460 --- [io-8080-exec-10] o.s.web.servlet.Dispatcherservlet : Exiting from "ERROR" Dispatch, status 500 at Postman I am sending the following: { "name": "Financing" }
– Danrley Fernandes Lopes
Hello, avoid adding information in the comments, instead edit your question and add the information there. If you need to format some code select the code and click the button
{}
. I ask you to post the contents of the classCategoria.java
and also the class to which the variable belongscategoriaRepository
(would beCategoriaRepository.java
?).– Piovezan
Try to post code as text and not as images. I ask the kindness to correct so that other users can take advantage of the question too.
– Piovezan
OK Piovezan, thank you very much!
– Danrley Fernandes Lopes
The staff is still hammering the negative votes, I think better to add the error log to the question too or else someone please explain the negative for the illustrious padawan correct the question. :)
– Piovezan