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

Spring Boot迁移后REST接口与Swagger均404问题求助

Alright, let's figure out why you're hitting those 404s and fix them step by step:

1. Why the /test/echo/{in} endpoint returns 404

The main issue here is Spring isn't scanning your TestController class at all. Look at your WebserviceApplication code: the scanBasePackages only includes com.company.subpackage.location.project1, but your TestController lives in com.company.project.webservice.controllers—a completely separate package tree that Spring doesn't know about.

Fix: Expand the package scanning range

Update your WebserviceApplication to include the root package of your controllers (and any other components you need loaded):

package com.company.project.webservice;

@SpringBootApplication(
  scanBasePackages = { 
    "com.company.subpackage.location.project1",
    "com.company.project.webservice" // Adds your controller's root package
  })
public class WebserviceApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebserviceApplication.class, args);
    }
}

Bonus fix: Add @PathVariable to your endpoint parameter

Even if Spring scans the controller, your echo method will fail to bind the path variable without this annotation. Update your TestController:

package com.company.project.webservice.controllers;

@RestController
@RequestMapping("/test")
@Api(value = "test", description = "Actions to test")
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping("/echo/{in}")
    String echo(@PathVariable String in){ // Bind the path variable here
        return testService.echo(in);
    }
}

2. Why Swagger UI returns 404 & shows null path errors

You've added the springfox dependencies, but you haven't enabled or configured Swagger in your app. Spring Boot doesn't auto-configure Swagger out of the box—you need a configuration class to set it up properly. The null path error happens because Swagger can't find any API endpoints to document (since your controller wasn't scanned) and has no base path configured.

Fix: Add a Swagger configuration class

Create a SwaggerConfig class in a scanned package (like com.company.project.webservice.config):

package com.company.project.webservice.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2 // Turns on Swagger 2 support
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                // Tell Swagger to scan your controller package for endpoints
                .apis(RequestHandlerSelectors.basePackage("com.company.project.webservice.controllers"))
                .paths(PathSelectors.any())
                .build();
    }
}

Final Verification

After making these changes:

  1. Restart your app
  2. Test the echo endpoint: http://localhost:30001/test/echo/hello should return hello (assuming your TestService.echo() just returns the input)
  3. Access Swagger UI: http://localhost:30001/swagger-ui.html should load the interface, and you'll see your /test/echo/{in} endpoint listed there.

Note: The root path response ({"_links": {"profile": ...}}) is normal—it's from Spring Data REST, which you have enabled via spring-boot-starter-data-rest. This doesn't conflict with your custom REST controllers once they're properly scanned.

内容的提问来源于stack exchange,提问作者Burst of Ice

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:39:36