一切都正确,但是显示404错误

问题描述 投票:0回答:1

我在 Spring Boot 中创建了一个普通的 POST、GET 映射。已成功连接数据库(PostgreSQL)。 然后,我想在postman工具中尝试这个,但它显示错误404。如何解决这个问题?

我试图找到方法。但没有成功。

这是我的实体类


import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import lombok.*;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name="firsts", schema="public")
public class Entit {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private String address;
        
    
}

这是控制器


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.post.entity.Entit;
import com.post.serv.Serv;

@RestController
public class Control {

    @Autowired
    Serv serv;
    
    @PostMapping("/create")
    private Entit create(@RequestBody Entit enti) {
        return serv.create(enti);
    }
    
    @GetMapping("/getAll")
    private List<Entit> getAll(){
        return serv.getAll();
    }
    
    @GetMapping("/get/{id}")
    private Entit getById(@PathVariable("id") int id) {
        return serv.getById(id);
    }
    
}

这就是服务


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.post.entity.Entit;
import com.post.repo.Repo;

@Service
public class Serv {

    @Autowired
    Repo repo;
    
    public Entit create(Entit enti) {
        return repo.save(enti);
    }

    public Entit getById(int id) {
        return repo.findById(id).get();
    }
    
    public List<Entit> getAll(){
        return repo.findAll();
    }
    
    }

这是存储库


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

import com.post.entity.Entit;

@Repository
public interface Repo extends JpaRepository<Entit, Integer>{

    
}

这是我在邮递员中给出的json查询

 http://localhost:8080/getAll

java spring-boot spring-mvc http-status-code-404 crud
1个回答
0
投票

您的控制器方法设置为private,这意味着它们无法从类外部访问。因此,将控制器方法从私有更改为public

© www.soinside.com 2019 - 2024. All rights reserved.