首页 > 解决方案 > 使用 Spring Boot 配置 Swagger2

问题描述

我正在尝试将 swagger2 与我的 Spring Boot 应用程序集成,但是当我尝试在浏览器中打开 swagger-ui 页面时,它在控制台上出现以下错误:

处理程序执行导致的已解决异常:org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: 无法将类型“java.lang.String”的值转换为所需类型“int”;嵌套异常是 java.lang.NumberFormatException:对于输入字符串:“swagger-ui.html”

这是我的 SwaggerConfig 类:

@EnableSwagger2
@PropertySource("classpath:swagger.properties")
@ComponentScan(basePackageClasses = TestController.class)
@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()
               .build();
   }
}

这是我的控制器类:

@RestController
@RequestMapping("/test")
@Api(value="MerchantControllerAPI",produces = MediaType.APPLICATION_JSON_VALUE)
public class TestController {

    @RequestMapping(path="{/id}", method = RequestMethod.GET)
    @GetMapping("/")
    @ApiOperation("Testing")
    @ApiResponses(value={@ApiResponse(code=200, message="Ok",response=String.class )})
    public String getSomething(@PathVariable("id") String id){
        return "HelloWorld";
    }
}

我按照本教程进行操作。这里有遇到类似问题的人吗?请帮帮我。

块引用

标签: javaspring-bootswaggerswagger-ui

解决方案


请删除@PropertySource("classpath:swagger.properties")

也改变你的 TestController 如下所示

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/test")
@Api(value = "MerchantControllerAPI", produces = MediaType.APPLICATION_JSON_VALUE)
public class TestController {

    @RequestMapping(path = "/{id}", method = RequestMethod.GET)
    @GetMapping("/")
    @ApiOperation("Testing")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "Ok", response = String.class)})
    public String getSomething(@PathVariable("id") String id) {
        return "HelloWorld";
    }
}

推荐阅读