@GetMapping 在 Spring Boot 中工作正常,但在 Postman 中返回错误 404

时间:2020-12-19 07:13:34

标签: spring-boot spring-data-jpa get postman restapi

我使用 JPA 从 postgres 获取数据。我在数据库中创建了一个表 - smartbank 并相应地创建了模型类。现在,在 sts 中一切正常并且没有错误并且 tomcat 服务器启动,但是当我去邮递员并使用 get 方法时,它显示状态:404。 请在下面找到控制器的代码:

package com.scb.smartbank.model.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.scb.smartbank.model.Model;
import com.scb.smartbank.service.Service;
@RestController
@CrossOrigin
public class Controller {
    @Autowired
    Service service;
    @GetMapping("/getdata")
    List<Model> getDetails(){
        return service.findAll();
    }
}

模型类代码:

package com.scb.smartbank.model;

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

@Entity
@Table(name = "details")
public class Model {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    
    @Column(name = "creditcard_num")
    Integer creditcard_num;
    @Column(name = "cardname")
    String cardname;
    @Column(name = "username")
    String username;
    @Column(name = "points")
    Integer points;
    @Column(name = "rewards")
    Integer rewards;
    public Integer getCreditcard_num() {
        return creditcard_num;
    }
    public void setCreditcard_num(Integer creditcard_num) {
        this.creditcard_num = creditcard_num;
    }
    public String getCardname() {
        return cardname;
    }
    public void setCardname(String cardname) {
        this.cardname = cardname;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Integer getPoints() {
        return points;
    }
    public void setPoints(Integer points) {
        this.points = points;
    }
    public Integer getRewards() {
        return rewards;
    }
    public void setRewards(Integer rewards) {
        this.rewards = rewards;
    }

}

服务接口代码:

package com.scb.smartbank.service;

import java.util.List;

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

import com.scb.smartbank.model.Model;

@Repository
public interface Service extends JpaRepository<Model,Integer>{
    @Override
    List<Model> findAll();

}

应用程序属性:

server.port = 8086
spring.datasource.url=jdbc:postgresql://localhost:5432/smartbank
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.show-sql=true 
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.hibernate.ddl-auto = update
spring.main.allow-bean-definition-overriding=true

现在,当我使用 http://localhost:8086/getdata 在 Postman 中运行它时,出现以下错误:

{
"timestamp": "2020-12-19T07:07:13.639+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/getdata"
}

请提前帮助并感谢您。

0 个答案:

没有答案