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

Spring Boot接口404问题排查:Postman提示无操作定义且无法连接

Spring Boot接口404排查求助

我从YouTube获取了一份Spring Boot练习代码,自认为代码编写正确,但用Postman调用接口时出现404错误,提示“No operations defined in spec!!”,同时无法建立连接。Spring Boot控制台没有任何报错输出,不确定是应用本身还是代码问题,附上相关代码,请求帮忙排查接口映射相关问题。


Controller代码

@Slf4j
@RestController
@RequestMapping("/api/v1")
public class RestuController {
    
    @Autowired
    private RestuService restuService;
    
    // POST MAPPING
    @PostMapping("/POST/save")
    Restaurant addRestaurant(@RequestBody Restaurant restaurant) {
        return restuService.saveRestu(restaurant);
    }
    
    @PostMapping("/POST/saves")
    List<Restaurant> addRestaurants(@RequestBody List<Restaurant> restaurants) {
        return restuService.saveRestaurants(restaurants);
    }

    @GetMapping("/GET/resturants")
    List<Restaurant> findAllRestaurants(){
        log.debug("reached /GET/resturants");
        return restuService.getRestaurants();
    }
    
    @GetMapping("/GET/{id}")
    Restaurant findRestaurantById(@PathVariable int id) {
        log.debug("reached /GET/{id} :" + id);
        return restuService.getRestaurantById(id);
    }
    
    @GetMapping("/GET/{name}")
    Restaurant findRestaurantByName(@PathVariable String name) {
        log.debug("reached /GET/{name}:" + name);
        return restuService.getRestaurantByname(name);
    }
    
    @GetMapping("/GET/{dist}")
    List<Restaurant> findRestaurantByDist(@PathVariable int distance) {  
        log.debug("reached /GET/{dist} :" + distance);
        return restuService.getRestaurantbyDist(distance);
    }
    
    @GetMapping("/GET/{loc}")
    List<Restaurant> findRestaurantByLocation(@PathVariable String location) {
        log.debug("reached /GET/{loc} : " + location);
        return restuService.getRestaurantbyLocation(location);
    }
    
    @GetMapping("/GET/{cuisine}")
    List<Restaurant> findRestaurantByCuisine(@PathVariable String cuisine) {
        log.debug("reached /GET/{cuisine} : " + cuisine);
        return restuService.getRestaurantbyCuisine(cuisine);
    }
    
    @GetMapping("/GET/{budget}")
    List<Restaurant> findRestaurantByBudget(@PathVariable int budget) {
        log.debug("reached /GET/{Budget} : " + budget);
        return restuService.getRestaurantbyBudget(budget);
    }
}

Service代码

package SERVICE;

import java.util.List;

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

import ENTITY.Restaurant;
import REPOSITORY.RestuRepository;

@Service
public class RestuService {
    
    @Autowired
    RestuRepository restuRepository;
    
    // POST方法
    public Restaurant saveRestu(Restaurant restaurant) {
        return restuRepository.save(restaurant);
    }
    
    public List<Restaurant> saveRestaurants(List<Restaurant> restaurants) {
        return restuRepository.saveAll(restaurants);
    }
    
    // GET方法
    public List<Restaurant> getRestaurants(){
        return restuRepository.findAll();
    }

    public Restaurant getRestaurantById(int id){
        return restuRepository.findById(id).orElse(null);
    }
    
    public Restaurant getRestaurantByname(String name){
        return restuRepository.findByName(name);
    }

    public List<Restaurant> getRestaurantbyDist(int distance){
        return restuRepository.findByDist(distance);
    }

    public List<Restaurant> getRestaurantbyLocation(String location){
        return restuRepository.findByLocation(location);
    }

    public List<Restaurant> getRestaurantbyCuisine(String cuisine){
        return restuRepository.findBycuisine(cuisine);
    }
    
    public List<Restaurant> getRestaurantbyBudget(int budget){
        return restuRepository.findBybudget(budget);
    }
}

Repository代码

package REPOSITORY;

import java.util.List;

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

import ENTITY.Restaurant;

@Repository
public interface RestuRepository extends JpaRepository<Restaurant, Integer> {

    Restaurant findByName(String name);

    List<Restaurant> findByDist(int distance);

    List<Restaurant> findByLocation(String location);

    List<Restaurant> findBycuisine(String cuisine);

    List<Restaurant> findBybudget(int budget);
}

Entity实体类代码

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

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "restaurant_tbl")
public class Restaurant {
    
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private String cuisine;
    private String location;
    private int distance;
    private int budget;
}

application.properties配置

server.port = 6282

spring.datasource.dbcp2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url =jdbc:mysql://localhost:3306/restaurants
spring.datasource.username = root
spring.datasource.password = ****
spring.jpa.show-sql =true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
springdoc.api-docs.path=/api-docs
springdoc.swagger-ui.path=/swagger-ui.html

pom.xml依赖配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sangamam</groupId>
    <artifactId>RESTAURANT-SERVICE</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>RESTAURANT-SERVICE</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

问题排查及解决方案

1. 核心问题:URL路径模板冲突

你的Controller中多个GetMapping使用了完全相同的URL模板(如/GET/{id}、/GET/{name}等),Spring无法区分请求应该映射到哪个方法,直接导致接口映射失效,返回404。

修复方法:为每个接口设置唯一的路径标识,示例修改如下:

@GetMapping("/GET/by-id/{id}")
Restaurant findRestaurantById(@PathVariable int id) {
    log.debug("reached /GET/by-id/{id} :" + id);
    return restuService.getRestaurantById(id);
}

@GetMapping("/GET/by-name/{name}")
Restaurant findRestaurantByName(@PathVariable String name) {
    log.debug("reached /GET/by-name/{name}:" + name);
    return restuService.getRestaurantByname(name);
}

@GetMapping("/GET/by-distance/{dist}")
List<Restaurant> findRestaurantByDist(@PathVariable int distance) {  
    log.debug("reached /GET/by-distance/{dist} :" + distance);
    return restuService.getRestaurantbyDist(distance);
}
// 其余接口同理修改路径前缀

2. 包扫描验证

确认Spring Boot启动类所在包是否能覆盖RestuController等组件的包路径。如果启动类在com.sangamam,而Controller在com.sangamam.controller,Spring默认会自动扫描;如果Controller在非子包下,需在启动类添加@ComponentScan(basePackages = {"你的包路径"})指定扫描范围。

3. 接口调用路径校验

确保Postman调用的完整URL严格匹配:

  • 完整URL示例:http://localhost:6282/api/v1/GET/resturants
  • 注意路径中的大小写(如GET、POST均为大写,需严格对应)

4. Swagger接口验证

启动应用后访问http://localhost:6282/swagger-ui.html,查看接口是否被正确识别。如果Swagger中无接口列表,说明Controller未被Spring正确加载。

5. 数据库连接确认

检查MySQL服务是否正常运行,restaurants数据库是否存在,用户名密码是否正确。虽然控制台无报错,但数据库连接异常可能间接影响应用初始化。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 18:35:27