我的控制器類是 -
package com.javatechie.crud.example.controller;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.javatechie.crud.example.entity.Product;
import com.javatechie.crud.example.service.ProductService;
public class ProductListController {
@Autowired
private ProductService service;
@GetMapping("/GetProduct")
public List<Product> addProducts(@RequestBody Product products) throws IOException, ClassNotFoundException, SQLException {
System.out.println("Inside addProducts controller method");
return service.saveProducts(1);
}
}
我的物體類是 -
package com.javatechie.crud.example.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="PRODUCT_TBL")
public class Product {
@Id
@GeneratedValue
private int id;
private String name;
private String quantity;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
JpaRepository 實作是 -
package com.javatechie.crud.example.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="PRODUCT_TBL")
public class Product {
@Id
@GeneratedValue
private int id;
private String name;
private String quantity;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
服務類是 -
package com.javatechie.crud.example.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.javatechie.crud.example.entity.Product;
import com.javatechie.crud.example.repository.ProductRepository;
@Service
public class ProductService {
@Autowired
private ProductRepository repository;
public List<Product> saveProducts(int id) {
return repository.GetRepo(id);
}
}
并且,它給出的意外輸出是 -

資料庫表如下——

我期待的輸出是帶有 id 、 name 、price、quantity 的 sql 記錄的 json 正文。如何僅使用本機查詢實作此輸出?請幫忙。
uj5u.com熱心網友回復:
您應該添加 @RestController 和 @RequestMapping 注釋來處理傳入的 REST 請求。
@RestController
@RequestMapping("/api")
public class ProductListController {
@Autowired
private ProductService service;
@GetMapping("/GetProduct")
public List<Product> addProducts(@RequestBody Product products) throws IOException, ClassNotFoundException, SQLException {
System.out.println("Inside addProducts controller method");
return service.saveProducts(1);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/379001.html
下一篇:為什么即使使用hibernate.hbm2ddl.auto"value="create",hibernate也不會自動創建表?
