Home > database >  Spring boot Json crudRepository empty post type request
Spring boot Json crudRepository empty post type request

Time:10-08

I am working on a REST api project with Spring boot, in my POJO class "book" I create a table entity to be mapped by hibernate:

package com.libreria.proyect.libreria_el_aleph.model;

@Entity
@Table(name = "Libro")
public class Libro {

@Id
@Column(name = "ID_libro")
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Integer idLibro;

@Column(name = "ISBN")
private String isbn;

@Column(name = "Nombre_libro")
private String nombreLibro;

@Column(name = "Autor")
private String autor;

@Column(name = "Precio")
private Double precio;

@Column(name = "Cantidad")
private int cantidad;


public Libro(Integer idLibro, String isbn, String nombreLibro, String autor, Double precio, int cantidad ){
    
    this.idLibro = idLibro;
    this.isbn = isbn; 
    this.nombreLibro = nombreLibro;
    this.autor = autor;
    this.precio = precio;
    this.cantidad = cantidad;

}


/**
 * @return Integer return the idLibro
 */
public Integer getIdLibro() {
    return idLibro;
}

/**
 * @param idLibro the idLibro to set
 */
public void setIdLibro(Integer idLibro) {
    this.idLibro = idLibro;
}

/**
 * @return String return the isbn
 */
public String getIsbn() {
    return isbn;
}

/**
 * @param isbn the isbn to set
 */
public void setIsbn(String isbn) {
    this.isbn = isbn;
}

/**
 * @return String return the nombreLibro
 */
public String getNombreLibro() {
    return nombreLibro;
}

/**
 * @param nombreLibro the nombreLibro to set
 */
public void setNombreLibro(String nombreLibro) {
    this.nombreLibro = nombreLibro;
}

/**
 * @return String return the autor
 */
public String getAutor() {
    return autor;
}

/**
 * @param autor the autor to set
 */
public void setAutor(String autor) {
    this.autor = autor;
}

/**
 * @return Double return the precio
 */
public Double getPrecio() {
    return precio;
}

/**
 * @param precio the precio to set
 */
public void setPrecio(Double precio) {
    this.precio = precio;
}

/**
 * @return int return the cantidad
 */
public int getCantidad() {
    return cantidad;
}

/**
 * @param cantidad the cantidad to set
 */
public void setCantidad(int cantidad) {
    this.cantidad = cantidad;
}

}

I'm trying to post to the get method that implements CrudRepository in my controller, but when I make a test request the content of all the Json elements comes back empty, how can I fix this?

@RestController
@RequestMapping("Api/libros")
public class LibroController {

    @Autowired
    LibroServices service; <-- Here is my repository crud service

    @GetMapping("/AllBooks")
    public ResponseEntity<ArrayList<Libro>> getAllBooks(){
        ArrayList<Libro> getAllBooks = this.service.listar();
        return ResponseEntity.status(HttpStatus.OK).body(getAllBooks);
    }

    
    @PostMapping("/AllBooks")
    public ResponseEntity<Libro> createNewBook(@RequestBody Libro libro){
        Libro createNewBook = service.saveNewBook(libro); 

        return ResponseEntity.status(HttpStatus.OK).body(createNewBook) ;
    }
}

enter image description here

As a request I also add the content of my services file:

package com.libreria.proyect.libreria_el_aleph.model.Services;

import java.util.ArrayList;
import java.util.List; 
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.libreria.proyect.libreria_el_aleph.model.Libro;
import com.libreria.proyect.libreria_el_aleph.model.
InterfaceServices.ILibroServices;
import 
com.libreria.proyect.libreria_el_aleph.model.Interfaces.ILibro;

@Service
public class LibroServices implements ILibroServices {

@Autowired(required = true)
ILibro repository; 

@Override
public ArrayList<Libro> listar() {
    return (ArrayList<Libro>) repository.findAll();
}

@Override
public Optional<Libro> listarById(Integer idlibro) {
    return Optional.empty();
}

@Override
public Libro saveNewBook(Libro libro) { <-- save method
    return repository.save(libro);
}

@Override
public void delete(Integer idLibro) {
    
}

}

CodePudding user response:

There is problem in your save method, whatever you are inserting i think its not saved in database. First check that the data is reflecting in database.

CodePudding user response:

Try adding a constructor without id field like below in your Pojo :

public Libro(Integer idLibro, String isbn, String nombreLibro, String autor, Double precio, int cantidad ){
    
    this.idLibro = idLibro;
    this.isbn = isbn; 
    this.nombreLibro = nombreLibro;
    this.autor = autor;
    this.precio = precio;
    this.cantidad = cantidad;

}

Also, post a object without Id as you have set @GeneratedValue(strategy = GenerationType.IDENTITY) It will auto generate the ID.

Also, don't create the variable name same as method name . Try changing it to something else like:

@PostMapping("/AllBooks")
    public ResponseEntity<Libro> createNewBook(@RequestBody Libro libro){
        Libro book = service.saveNewBook(libro); 

    return ResponseEntity<>(book,HttpStatus.CREATED) ;
}
  • Related