java - 使用 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";
}
}
我按照本教程进行操作。这里有遇到类似问题的人吗?请帮帮我。
块引用
解决方案
请删除@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";
}
}
推荐阅读
- php - 使用数组填充表格
- python - 向 GeoJsonToolTip 添加另一个值时出现 TypeError
- c# - 如何将字符串数组传递给属性构造函数?
- java - 递归类型序列化期间的 Jackson RuntimePermission 异常
- reactjs - TypeError:无效尝试传播具有使用效果的不可迭代实例
- android - drawableStart 不显示可绘制对象
- json - 对令牌的快速 POST 请求有问题
- visualization - How to install LLNL VisIt on another distribution
- neural-network - Pytorch modifying intermediate values during forward
- ruby-on-rails - Trying to compare results in one array with results in another array