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

Spring Boot集成Swagger2访问swagger-ui.html报错求助

Hey, I can see exactly what's causing that Swagger error you're facing! Let's break this down step by step.

First, let's look at that error message again:

Resolved exception caused by Handler execution: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "swagger-ui.html"

The core issue here is that your TestController has incorrect routing annotations, which is making Spring accidentally route the swagger-ui.html request to your API endpoint. It's trying to turn the string "swagger-ui.html" into a parameter type (even though you defined id as String, the messed-up routing logic is triggering this conversion failure).

What's wrong with your Controller code?

Looking at your TestController, there are two clear mistakes in the routing setup:

@RestController
@RequestMapping("/test")
@Api(value="MerchantControllerAPI",produces = MediaType.APPLICATION_JSON_VALUE)
public class TestController {
    // Mistake 1: Incorrect path variable syntax - {/id} is invalid
    @RequestMapping(path="{/id}", method = RequestMethod.GET)
    // Mistake 2: Conflicting GET annotations - this clashes with the above mapping
    @GetMapping("/")
    @ApiOperation("Testing")
    @ApiResponses(value={@ApiResponse(code=200, message="Ok",response=String.class )})
    public String getSomething(@PathVariable("id") String id){
        return "HelloWorld";
    }
}
  1. The path variable syntax {/id} is wrong — valid path variables should be formatted as /{id}.
  2. Using both @RequestMapping(path="{/id}", method = RequestMethod.GET) and @GetMapping("/") creates a routing conflict, which confuses Spring's request matching logic.

Fixed Controller Code

Update your TestController to use clean, consistent routing annotations:

@RestController
@RequestMapping("/test")
@Api(value="MerchantControllerAPI",produces = MediaType.APPLICATION_JSON_VALUE)
public class TestController {
    // Single, correct GET mapping for the /test/{id} endpoint
    @GetMapping("/{id}")
    @ApiOperation("Testing")
    @ApiResponses(value={@ApiResponse(code=200, message="Ok",response=String.class )})
    public String getSomething(@PathVariable("id") String id){
        return "HelloWorld";
    }
}

Optional SwaggerConfig Improvement

You can also tweak your SwaggerConfig to be more explicit about which controllers to scan, which helps avoid unexpected behavior:

@EnableSwagger2
@PropertySource("classpath:swagger.properties")
@Configuration
public class SwaggerConfiguration {
    private static final String SWAGGER_API_VERSION="1.0";
    private static final String LICENSE_TEXT ="License";
    private static final String title ="Merchant API";
    private static final String description ="Restful APIs for merchant";

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .license(LICENSE_TEXT)
                .version(SWAGGER_API_VERSION)
                .build();
    }

    @Bean
    public Docket merchantApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                // Replace with your actual controller package path
                .apis(RequestHandlerSelectors.basePackage("com.yourproject.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

After making these changes, restart your Spring Boot app and try accessing swagger-ui.html again — it should load without that conversion error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:21:27