我有一个 API 的三个路径变量。我想用 ***** 屏蔽 Swagger UI 上的一个输入。
使用 Springdoc OpenAPI 时如何做到这一点?
我有一个 spring(启动)服务器,想要使用 springdoc 从注释生成 OpenAPI 规范。
我有一个请求,请求正文中有两个参数。我希望第一个是必需的,第二个是可选的。
@RequestBody(required = {true|false})似乎只将主体中的所有参数设置为(不需要)。@Parameter另一方面,Javadoc说要使用io.swagger.v3.oas.annotations.parameters.RequestBody
这是我的代码,我希望生成一个规范,其中第一个参数是必需的,第二个参数是可选的:
@GetMapping("/fstVector")
public ResponseEntity<Vector> fstV(@RequestBody final Vector v1, @RequestBody(required = false) final Vector v2) {
return new ResponseEntity<>(v1, HttpStatus.OK);
}
@PostMapping("/fstVector")
public ResponseEntity<Vector> fstVPost(@RequestBody(required = true) final Vector v1, @RequestBody(required = false) final Vector v2) {
return new ResponseEntity<>(v1, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
然而,生成的规范需要两个参数:
/pond/fstVector:
get:
tags:
- circle-escape-controller
operationId: fstV
parameters:
- name: v1
in: query
required: true
schema:
$ref: '#/components/schemas/Vector'
- name: v2
in: query
required: …Run Code Online (Sandbox Code Playgroud) 网络上没有关于如何使用 springdocs-openapi 库 (1.5.7) 获取以下输出的好示例。我希望获得以下输出:
[
"A", "B", "C"
]
Run Code Online (Sandbox Code Playgroud)
这是基于所提供示例的代码。
@Operation(summary = "")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK",
content = {@Content(mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = String.class)),
examples = {@ExampleObject("A"), @ExampleObject("B"), @ExampleObject("C")}
)})
Run Code Online (Sandbox Code Playgroud)
这会产生以下输出
[
"string"
]
Run Code Online (Sandbox Code Playgroud)
上面列出的输出 ["A","B","C"] 如何通过 springdocs-openapi 库实现?
spring-boot springdoc springdoc-ui springdoc-openui springdoc-openapi-ui
我正在尝试使用以下代码将 springfox 迁移到 springdoc:
import io.swagger.annotations.ApiModelProperty;
@ApiModelProperty(position = 30, required = true, value = "Group value")
Run Code Online (Sandbox Code Playgroud)
到
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(position = 20, required = false)
Run Code Online (Sandbox Code Playgroud)
但我找不到position和value。你知道在 springdoc 中替换它们的正确方法是什么吗?
我升级到 Spring boot 3.0.7 并尝试让我的 Open API (swagger) 再次工作,具有这些依赖项(根据springdoc):
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
...但是当我构建我的应用程序时,出现以下错误:
java.lang.IllegalStateException: Failed to introspect Class [org.springdoc.webmvc.api.OpenApiWebMvcResource] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@1de0aca6]
Run Code Online (Sandbox Code Playgroud)
...“原因”为:
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
Run Code Online (Sandbox Code Playgroud)
当我查看罐子OpenApiWebMvcResource中的内容时,它确实是从而不是org.springdoc:springdoc-openapi-webmvc-core:1.7.0导入的:javaxjakarta
package org.springdoc.webmvc.api;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.v3.oas.annotations.Operation;
import java.util.Locale;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
...
Run Code Online (Sandbox Code Playgroud)
那么这是一个问题openapi-webmvc-core,还是我接线有问题?
我正在使用springdoc-openapi-ui. 我已经配置了名为 的全局标头Authorization。当我执行 API 时,Authorization请求的 CURL 中没有显示。除了 之外,我的其他参数都显示在 CURL 中Authorization。
以前,我使用的是springfox-swagger-ui,当时它显示在 CURL 部分中。
我搜索了相同的内容并在https://swagger.io/docs/specification/describing-parameters/#header-parameters上找到了注释
Note: Header parameters named Accept, Content-Type and Authorization are not allowed.
但我的要求仅用Authorization作标题。我们有什么办法可以实现这一目标吗?
我SwaggerConfiguration的添加在下面。
@Configuration
public class SwaggerConfiguration {
@Value("${title:title}")
private String title;
@Value("${description:description.}")
private String description;
@Value("${version:0.0.1}")
private String version;
@Value("${schemeId}")
String schemeIdentifier;
@Bean
public OperationCustomizer customGlobalHeaders() {
return (Operation operation, HandlerMethod handlerMethod) -> {
Parameter authorization = new Parameter().in(ParameterIn.HEADER.toString()).name("Authorization")
.description("Authorization …Run Code Online (Sandbox Code Playgroud) java spring-security swagger-ui spring-boot springdoc-openapi-ui
在我的 spring boot 应用程序中,我有端点,这些端点由 springboot 应用程序中的标头参数验证。当前 swagger json 如下所示:
// part of current swagger.json
...
"paths": {
"/path1/{param1}": {
"get": {
"parameters": [
{
"name": "param1",
"in": "path",
"type": "string",
"required": true
}
]
}
}
}
...
Run Code Online (Sandbox Code Playgroud)
我想使用springdoc-openapi-ui配置添加缺少的参数,所以它看起来像这样:
// I want to achieve swagger.json which contains additional parameter
...
"paths": {
"/path1/{param1}": {
"get": {
"parameters": [
{
"name": "param1",
"in": "path",
"type": "string",
"required": true
},
{
"name": "missingParam",
"in": "header",
"type": "string",
"required": true
} …Run Code Online (Sandbox Code Playgroud) 这有点奇怪。springdoc-openapi-ui v1.2.32,生成的文档仅包含控制器内的一些映射。
例子:
@Operation(
summary = "Foo",
description = "Foo"
)
@PostMapping(path="/v1/foo")
public ResponseEntity<ResponseObject> postFoo(@RequestBody FooRequestObject searchRequest, HttpServletRequest request){ ... }
@Operation(
summary = "Bar",
description = "Bar"
)
@GetMapping(path="/v1")
public ResponseEntity<ResponseObject> getBar(@RequestBody BarRequestObject request, HttpServletRequest request){ ... }
@Operation(
summary = "Bar",
description = "Bar"
)
@PostMapping(path="/v1")
public ResponseEntity<ResponseObject> postBar(@RequestBody BarRequestObject request, HttpServletRequest request){ ... }
Run Code Online (Sandbox Code Playgroud)
postBar仅针对和服务生成文档getBar,其他路径将被忽略。
我尝试过的:
post. 我重命名是为了避免冲突。如果我向控制器添加另一个服务(带或不带注释标记),它也不会显示在生成的 Swagger 中。例如:
@GetMapping(path="/test")
public String getTest(){
return "test"; …Run Code Online (Sandbox Code Playgroud) 我有一个 Springboot Rest 应用程序,其中有自动转换 API 和参数的注释。
我有自定义注释,其中包含一些注释,如何将其生成到 OpenAPI 3 中的 swagger 页面?
Ex:
@RestController
Class Controller {
@GetMapping(/test/result/)
@CustomAnnotation(value = "This description should come in swagger")
void method() {
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个使用 Spring Boot(最新版本)并创建一个具有 RESTful api 的后端的应用程序。传统上,我创建了如下控制器:
@RestController
@RequestMapping("/contacts")
public class ContactController {
@Autowired
private ContactService service;
@RequestMapping(value = "/contactId/{contactId}",
method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody ContactEntity getContactById(@PathVariable("contactId") long contactId) {
ContactEntity contactEntity = service.getContactById(contactId);
return contactEntity;
}
Run Code Online (Sandbox Code Playgroud)
集成测试一直是这样的:
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = ServiceContextConfiguration.class)
@ComponentScan("com.tomholmes.springboot.phonebook.server")
@Transactional
@WebAppConfiguration
public class ContactControllerTest {
@Test
public void testGetContactById() throws Exception {
MockHttpServletRequestBuilder requestBuilder =
MockMvcRequestBuilders.get(BASE_URL + "/contactId/6");
this.mockMvc.perform(requestBuilder)
.andDo(print())
.andExpect(status().isOk());
}
}
Run Code Online (Sandbox Code Playgroud)
多年来,它一直作为“代码优先”API 正常工作。现在,我正在使用 OpenAPI 3 和 YAML 文件处理合约优先的 API。API 是在与以前相同的位置生成的,我希望测试能够像以前一样工作,但事实并非如此。
因此,一项资源:
[https://www.hascode.com/2018/08/testing-openapi-swagger-schema-compliance-with-java-junit-and-assertj-swagger/#API_Test]建议我使用assertj …
junit spring-boot junit-jupiter openapi-generator springdoc-openapi-ui
spring-boot ×8
openapi ×5
springdoc ×5
swagger ×3
swagger-ui ×3
java ×1
junit ×1
springdoc-ui ×1
springfox ×1