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

ycggw6v2  于 7个月前  发布在  Spring
关注(0)|答案(3)|浏览(65)

我在spring Boot 中创建了一个普通的POST,GETMap。它成功地连接到了数据库(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>{

    
}


这是我在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。确保它们可以被公开访问以处理传入的请求。

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

字符串

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

相关问题