ERROR 500 - But I don’t know where is wrong, someone help me!

Asked

Viewed 84 times

-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

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

  • 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 class Categoria.java and also the class to which the variable belongs categoriaRepository (would be CategoriaRepository.java?).

  • 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.

  • OK Piovezan, thank you very much!

  • 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. :)

Show 1 more comment

1 answer

0


The Setter country nome is not doing the assignment correctly.

In class Categoria.java, in the method setNome(String name), change the parameter name for nome.

This way the assignment will be correct:

public void setNome(String nome) {
    this.nome = nome;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.