使用spring boot配置Swagger2

时间:2018-05-21 06:46:35

标签: java spring-boot swagger swagger-ui

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

  

由Handler执行引起的已解决异常: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";
    }
}

我关注此tutorial。这里有谁遇到类似的问题?请帮帮我。

  

块引用

1 个答案:

答案 0 :(得分:0)

请删除@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";
    }
}