“状态”:404,“错误”:“未找到”,“路径”:“/GetProduct”

Posted

技术标签:

【中文标题】“状态”:404,“错误”:“未找到”,“路径”:“/GetProduct”【英文标题】:"status": 404, "error": "Not Found", "path": "/GetProduct" 【发布时间】:2022-01-14 21:36:28 【问题描述】:

我的控制器类是 -

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 正文。如何仅使用本机查询来实现此输出?请帮忙。

【问题讨论】:

【参考方案1】:

您应该添加@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);
       

【讨论】:

是的,成功了。添加这些注释后。它给出了预期的输出。非常感谢您的回复。

以上是关于“状态”:404,“错误”:“未找到”,“路径”:“/GetProduct”的主要内容,如果未能解决你的问题,请参考以下文章

错误:加载资源失败:服务器响应状态为 404(未找到)

Web 服务错误 HTTP 状态 404 - 未找到

Tomcat错误HTTP状态[404] - [未找到] [重复]

状态为404的Springboot Whitelabel错误页面:无法解析JSP的路径

Spring 问题:出现意外错误(类型=未找到,状态=404)

HTTP状态 404 - 未找到的一种解决方式