有没有办法在ResponsesRequest body部分中隐藏Schema?我们只需要显示Example Value.我们使用OpenAPI 3.

Dependency:

<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-ui</artifactId>
   <version>1.6.9</version>
</dependency>

我们可以在应用程序中使用springdoc.swagger-ui.defaultModelsExpandDepth=-1隐藏listed schema个部分.属性文件.

enter image description here

但我们想从Request BodyResponses中删除API模式部分.

enter image description here

我try 了content= @Content(schema = @Schema(hidden = true ))次,但它隐藏了整个请求正文/响应.

enter image description here

Code for Response:

@ApiResponses({
            @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(name = "Success response", example = "JsonResponse..."),
                    mediaType = MediaType.APPLICATION_JSON_VALUE)),
            @ApiResponse(responseCode = "400", description = "BAD REQUEST", content = @Content(schema = @Schema(hidden = true))) 
    })

Code for Request Body:

@io.swagger.v3.oas.annotations.parameters.RequestBody(
            content= @Content(schema = @Schema(example="JsonRequestBody...")))

有谁能建议我们怎么做?

UPDATE:

我们可以对下面的响应隐藏Schema部分.

@ApiResponse(responseCode = IConstants.R_str_200, content = @Content(examples=
@ExampleObject(name="SUCCESS RESPONSE",value="Json response..."),
                mediaType = IConstants.MEDIA_JSONVALUE))

enter image description here

但仍然无法从Request Body中隐藏Schema部分.

推荐答案

我认为这不能用注释来解决.

您可以预定义招摇过市css来隐藏所需的元素.

要实现这一点,首先判断您使用的是哪个版本的swagger ui.

enter image description here

然后,编写如下控制器类:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

@RestController
@RequestMapping(path = "/swagger-ui")
public class SwaggerController {
    @GetMapping(path = "/swagger-ui.css", produces = "text/css")
    public String getCss() {
        String orig = toText(getClass().getResourceAsStream("/META-INF/resources/webjars/swagger-ui/3.25.0/swagger-ui.css"));
        String customCss = "li.tabitem.active {\n" +
                "    display:block !important;\n" +
                "}\n" +
                "li.tabitem {\n" +
                "    display:none !important;\n" +
                "}}";
        return  orig+customCss;
    }


    static String toText(InputStream in) {
        return new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))
                .lines().collect(Collectors.joining("\n"));
    }
}

加载css时将调用此控制器的端点.

通过此更改,当您启动应用程序并转到端点以查看swagger文档时,您应该会看到如下图所示的UI:

enter image description here

Java相关问答推荐

如何用Java表示C++类以通过FFI使用?

Java 8 RDX-如何设置单个选项卡标题文本的 colored颜色

收听RDX中用户数据的变化

伪类focus-in不适用于PFA中的选项卡

Saxon 9:如何从Java扩展函数中的net.sf.saxon.expr. XPathContent中获取声明的变量

如何使用jooq generator将表名和列名映射为人类可读的?

Java函数式编程中的双值单值映射

将数组整体转换为链接表

XPages-在第二次点击按钮之前延迟

对运行在GraalVM-21上的JavaFX应用程序使用分代ZGC会警告不支持JVMCI,为什么?

对Java中的通配符参数的混淆

Com.example.service.QuestionService中的构造函数的参数0需要找不到的类型为';com.example.Dao.QuestionDao;的Bean

用OSQL创建索引

如何在列表(链表)中插入一个新 node (作为prelast)

何时调用密封层次 struct 的switch 中的默认情况

使IntelliJ在导入时优先 Select 一个类或将另一个标记为错误

Java KeyListener不工作或被添加

为什么没有加载java.se模块?

HBox内部的左对齐按钮(如果重要的话,在页码内)

Springboot应用程序无法识别任何@RestController或@Service,我认为@Repository也无法识别