You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot API无法被检测,Postman返回404错误求助

SpringBoot API 404 问题排查求助

我正在完成一个学校项目,遇到SpringBoot API无法被HTML页面或Postman检测到的问题,使用Postman请求时会返回404错误。启动API时控制台无报错信息,但调用时无法被识别。相关代码如下:

TicketsRest 控制器代码

package zm.rest;

import java.util.List;
import java.util.Optional;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import zm.dao.TicketDAO;
import zm.model.Tickets;

@RestController
@RequestMapping("tickets")
public class TicketsRest {
    
    @Autowired
    private TicketDAO ticketDAO;
    
    /*guardar*/
    @PostMapping("/create") //localhost:8080/users/create
    public void create(@RequestBody Tickets tickets) {
        /*int id = userDAO.findAll().get(userDAO.findAll().size()-1).getId()+1;*/
        int id = 0;
        if(ticketDAO.findAll().size() == 0) {
            id = 1;
        }
        else {
            id = ticketDAO.findAll().get(ticketDAO.findAll().size()-1).getId()+1;
        }
        tickets.setId(id);
        ticketDAO.save(tickets);
    }
    
    @GetMapping("/list") //localhost:8080/users/create
    public List<Tickets>list(){
        return ticketDAO.findAll();
    }
    
    @PutMapping("/update/{id}")
    public void update(@PathVariable int id, @RequestBody Tickets updateTickets) {
        
        Optional<Tickets> ticketsOptional = ticketDAO.findById(id);
        Tickets tickets = ticketsOptional.orElse(null);
        if(tickets != null)
        {
            tickets.setMotive(updateTickets.getMotive());
            tickets.setDepartment(updateTickets.getDepartment());
            tickets.setPriority(updateTickets.getPriority());
            tickets.setDepartment(updateTickets.getDepartment());
            tickets.setDescription(updateTickets.getDescription());

            ticketDAO.save(tickets);
        }
    }
    
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable int id) {
        
        Optional<Tickets> ticketsOptional = ticketDAO.findById(id);
        Tickets tickets = ticketsOptional.orElse(null);
        if(tickets != null)
        {
            ticketDAO.delete(tickets);
            return true;
        }
        return false;
    }
}

TicketDAO 数据访问接口

package zm.dao;

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

import zm.model.Tickets;


public interface TicketDAO extends JpaRepository<Tickets, Integer> {
}

Tickets 实体类

package zm.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Tickets {
    @Id
    private Integer id;
    
    @Column
    private String motive;
    
    @Column
    private String priority;
    
    @Column
    private String department;
    
    @Column
    private String description;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMotive() {
        return motive;
    }

    public void setMotive(String motive) {
        this.motive = motive;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        this.priority = priority;
    }
}

TicketsApplication 启动类

package zm.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class TicketsApplication {

    public static void main(String[] args) {
        SpringApplication.run(TicketsApplication.class, args);
    }
    
    @Bean
    WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping( "/**").allowedOrigins( "http://localhost:4200").allowedMethods("*").allowedHeaders("*");
    }
    };
    }
}

排查方向建议

  • 核对请求路径:控制器类的@RequestMapping是"tickets",完整接口路径应为localhost:8080/tickets/create、localhost:8080/tickets/list等,代码注释里的/users/create是错误路径
  • 确认组件扫描范围:启动类在zm.application包下,默认会扫描该包及其子包,但可显式添加@ComponentScan(basePackages = "zm")确保所有子包(rest/dao/model)被扫描
  • 查看启动日志:检查控制台是否输出接口映射日志,比如Mapped "{[/tickets/create],methods=[POST]}",无此日志说明控制器未被加载
  • 确认端口一致性:Postman请求的端口需和SpringBoot启动端口一致(默认8080)
  • 检查JPA实体识别:查看启动时是否有Hibernate创建表的日志,确认Tickets实体被正确识别

内容的提问来源于stack exchange,提问作者Cremita Xb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 16:55:23