gpt4 book ai didi

java - 使用 Spring Boot 配置 Swagger2

转载 作者:行者123 更新时间:2023-11-30 06:10:05 24 4
gpt4 key购买 nike

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

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"

这是我的 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();
}
}

这是我的 Controller 类:

@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 。这里有人遇到过类似的问题吗?请帮帮我。

Blockquote

最佳答案

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

关于java - 使用 Spring Boot 配置 Swagger2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50443385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com