Spring Boot 一切正常,但显示404错误

ycggw6v2  于 2023-11-17  发布在  Spring
关注(0)|答案(3)|浏览(167)

我在spring Boot 中创建了一个普通的POST,GETMap。它成功地连接到了数据库(PostgreSQL)。然后,我想在postman工具中尝试这个,但它显示错误404。如何解决这个问题?
我试过了,但没用。
这是我的实体类

  1. import jakarta.persistence.Entity;
  2. import jakarta.persistence.GeneratedValue;
  3. import jakarta.persistence.GenerationType;
  4. import jakarta.persistence.Id;
  5. import jakarta.persistence.Table;
  6. import lombok.*;
  7. @Data
  8. @Builder
  9. @AllArgsConstructor
  10. @NoArgsConstructor
  11. @Entity
  12. @Table(name="firsts", schema="public")
  13. public class Entit {
  14. @Id
  15. @GeneratedValue(strategy = GenerationType.IDENTITY)
  16. private int id;
  17. private String name;
  18. private String address;
  19. }

字符串
我是指挥官

  1. import java.util.List;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RequestBody;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import com.post.entity.Entit;
  10. import com.post.serv.Serv;
  11. @RestController
  12. public class Control {
  13. @Autowired
  14. Serv serv;
  15. @PostMapping("/create")
  16. private Entit create(@RequestBody Entit enti) {
  17. return serv.create(enti);
  18. }
  19. @GetMapping("/getAll")
  20. private List<Entit> getAll(){
  21. return serv.getAll();
  22. }
  23. @GetMapping("/get/{id}")
  24. private Entit getById(@PathVariable("id") int id) {
  25. return serv.getById(id);
  26. }
  27. }


这就是服务

  1. import java.util.List;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import com.post.entity.Entit;
  5. import com.post.repo.Repo;
  6. @Service
  7. public class Serv {
  8. @Autowired
  9. Repo repo;
  10. public Entit create(Entit enti) {
  11. return repo.save(enti);
  12. }
  13. public Entit getById(int id) {
  14. return repo.findById(id).get();
  15. }
  16. public List<Entit> getAll(){
  17. return repo.findAll();
  18. }
  19. }


这是仓库

  1. import org.springframework.data.jpa.repository.JpaRepository;
  2. import org.springframework.stereotype.Repository;
  3. import com.post.entity.Entit;
  4. @Repository
  5. public interface Repo extends JpaRepository<Entit, Integer>{
  6. }


这是我在postman http://localhost:8080/getAll中给出的json查询

nbysray5

nbysray51#

你的控制器方法被设置为private,这意味着它们不能从类外部访问。所以,将你的控制器方法从private改为public

uoifb46i

uoifb46i2#

IMHO,如果可能的话,这里有一些建议可以让你解脱。

1.端点Map:

确保您的Sping Boot 应用程序运行在正确的端口上(在您的示例中为8080)。通过在浏览器中访问http://localhost:8080来确认应用程序已启动并正在运行。

2.请求类型:

确保使用正确的请求类型(例如,GET,POST)。在Postman中,验证“getAll”的请求类型设置为GET。

3.URL格式:

仔细检查您在Postman中使用的URL。对于“getAll”请求,它应该是http://localhost:8080/getAll(基于控制器的@GetMapping(“/getAll”)Map)。此外,确保URL中没有拼写错误。

4.控制器方法可见性:

确保你的控制器方法被设置为public。在你的代码中,它们没有被显式地标记为public。确保它们可以被公开访问以处理传入的请求。

  1. @GetMapping("/getAll")
  2. public List<Entit> getAll(){
  3. return serv.getAll();
  4. }

字符串

5.控制器配置:

确保您的@RestController(Control)被Spring application context正确注册和扫描。检查您的Sping Boot application的main class或带有@SpringBootApplication annotation的class是否位于基础包中,或者是否具有必要的组件扫描配置来定位您的控制器。

6.公共访问和安全:

验证没有安全配置(如Spring Security)阻止端点被访问。
HTH

展开查看全部
z0qdvdin

z0qdvdin3#

enter image description here终于找到了答案。
我添加了一个类WebSecurityConfig.java,它运行了。它显示了另一个错误,比如错误请求400。
然后我解决了.这里是刚刚更新 Postman 头,标记内容类型,内容长度和主机和取消标记其他. I showed here what I did

相关问题