use*_*011 3 java swagger swagger-ui springdoc springdoc-openapi-ui
有以下Controller方法:
@ApiResponses(value = {@ApiResponse(responseCode = "200")})
@GetMapping(value = API_URI_PREFIX + PRODUCTS_URI, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public Flux<Product> getProducts(@Valid @NotNull PagingAndSorting pagingAndSorting) {
}
Run Code Online (Sandbox Code Playgroud)
我需要找到一种如何在对象Swagger示例中显示的方法PagingAndSorting。
我正在使用springdoc-apiv1.4.3。
您可以使用@ExampleObject 批注。
请注意,如果您想引用示例现有对象,您也可以在示例中使用 ref @ExampleObject(ref="...")。或者理想情况下,从外部配置文件中获取示例并使用 OpenApiCustomiser 添加它们,就像在此测试中所做的那样:
这是使用@ExampleObject 的示例代码:
@PostMapping("/test/{id}")
public void testme(@PathVariable("id") String id, @RequestBody(content = @Content(examples = {
@ExampleObject(
name = "Person sample",
summary = "person example",
value =
"{\"email\": test@gmail.Com,"
+ "\"firstName\": \"josh\","
+ "\"lastName\": \"spring...\""
+ "}")
})) PersonDTO personDTO) { }
Run Code Online (Sandbox Code Playgroud)
如果您@RequestBody在控制器中使用Spring 注释,则需要区分两者,例如使用@io.swagger.v3.oas.annotations.parameters.RequestBodySwagger 注释。这可以防止下面评论中提到的空参数问题。
小智 -4
如果我们使用 Spring Boot,要将其添加到我们的 Maven 项目中,我们需要在 pom.xml 文件中添加依赖项。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
@Configuration
@EnableSwagger2
public class SpringConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
不使用 Spring Boot 的配置
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
Run Code Online (Sandbox Code Playgroud)
验证: 要验证 Springfox 是否正常工作,您可以在浏览器中访问以下 URL:http://localhost:8080/our-app-root/api/v2/api-docs
要使用 Swagger UI,需要一个额外的 Maven 依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
您可以通过访问 http://localhost:8080/our-app-root/swagger-ui.html 在浏览器中测试它