我正在使用 Spring boot 開發一個 REST api 專案,在我的 POJO 類“書”中,我創建了一個表物體以由 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;
}
}
我正在嘗試發布到在我的控制器中實作 CrudRepository 的 get 方法,但是當我發出測驗請求時,所有 Json 元素的內容都回傳為空,我該如何解決這個問題?
@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) ;
}
}

作為請求,我還添加了我的服務檔案的內容:
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) {
}
}
uj5u.com熱心網友回復:
您的保存方法有問題,無論您插入什么,我認為它都沒有保存在資料庫中。首先檢查資料是否反映在資料庫中。
uj5u.com熱心網友回復:
嘗試在 Pojo 中添加一個沒有 id 欄位的建構式,如下所示:
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;
}
另外,發布一個沒有 ID 的物件,因為您設定了 @GeneratedValue(strategy = GenerationType.IDENTITY) 它會自動生成 ID。
另外,不要創建與方法名稱相同的變數名稱。嘗試將其更改為其他內容,例如:
@PostMapping("/AllBooks")
public ResponseEntity<Libro> createNewBook(@RequestBody Libro libro){
Libro book = service.saveNewBook(libro);
return ResponseEntity<>(book,HttpStatus.CREATED) ;
}
uj5u.com熱心網友回復:
看到這里您正在從郵遞員發送資料串列,并且在您的方法中您正在接收一個物件,因此如果您想保存多個資料,請在控制器中使用它
@PostMapping("/AllBooks")
public ResponseEntity<Libro> createNewBook(@RequestBody List<Libro> libro){
Libro createNewBook = service.saveNewBook(libro);
return ResponseEntity.status(HttpStatus.OK).body(createNewBook) ;
}
并在服務使用中:
@Override
public List<Libro> saveAllNewBook(List<Libro> libros) { <-- save method
return repository.saveAll(libro);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511712.html
標籤:爪哇春天弹簧靴休眠
