postman 在Sping Boot 中获取POST、GET请求的空响应

km0tfn4u  于 2023-10-18  发布在  Postman
关注(0)|答案(1)|浏览(169)

我是新来的Spring Boot 。因此,当我使用Postman发送get,post请求时,我面临着一个问题。我得到了零响应,即使一切似乎都很好。
我的控制器文件如下:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.bind.annotation.*;

import com.javatechie.crud.example.entity.Product;
import com.javatechie.crud.example.service.ProductService;

@RestController
public class ProductController {
    
    @Autowired
    private ProductService service;

    @PostMapping("/addProduct")
    public Product addProduct(@RequestBody Product product) {
        return service.saveProduct(product);
    }

    @PostMapping("/addProducts")
    public List<Product> addProducts(@RequestBody List<Product> products) {
        return service.saveProductAll(products);
    }

    @GetMapping("/products")
    public List<Product> findAllProducts() {
        return service.getProductAll();
    }

    @GetMapping("/productById/{id}")
    public Product findProductById(@PathVariable int id) {
        return service.getProductById(id);
    }

    @GetMapping("/product/{name}")
    public Product findProductByName(@PathVariable String name) {
        return service.getProductByName(name);
    }

    @DeleteMapping("/delete/{id}")
    public String deleteProduct(@PathVariable int id) {
        return service.deleteProduct(id);
    }
}

我的实体类文件如下:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "PRODUCT_TBL")

public class Product {
    
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private int quantity;
    private double price;

}

我的服务类文件如下:

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 Product saveProduct(Product product) {
        return repository.save(product);
    }
    
    public List<Product> saveProductAll(List<Product> products) {
        return repository.saveAll(products);
    }
    
    public List<Product> getProductAll() {
        return repository.findAll();
    }
    
    public Product getProductById(int id) {
        return repository.findById(id).orElse(null);
    }

    public Product getProductByName(String name) {
        return repository.findByName(name);
    }
    
    public String deleteProduct(int id) {
        repository.deleteById(id);
        return "Product havig id " +id +"deleted successfully";
    }
}

我的application.properties文件是:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = india
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
server.port=9191

**编辑:**以下是我的repo类:

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.javatechie.crud.example.entity.Product;

@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {

    Product findByName(String name);

}

我通过 Postman 发送以下POST请求-http://localhost:9191/addProduct
对于上述问题,我得到了以下回应- {}
对于以下GET请求,我也得到空白响应-http://localhost:9191/products
我用的是mysql db的lombok。
请让我知道出了什么问题。

2g32fytz

2g32fytz1#

这看起来像是Lombok插件安装问题。
当我们使用Eclipse和SpringToolSuite等IDE时,通常会出现这种情况。
1.如果我们可以在Entity类中手动添加getter/setter,那就更好了。

1.通过参考下面的网站在我们的IDE中安装Lombok插件。
Lombok is not generating getter and setter
另外,不要直接传递数据库返回的结果作为响应。
请使用相同的DTO类并在响应中返回它们。
有关此类Map,请参考https://mapstruct.org

相关问题