首页 > 解决方案 > 我如何在 Springdoc-swagger-ui 中获取 RequestBody null 或 empty 中的属性?

问题描述

我想包含属性为空或空的 RequestBody。

我目前在 Swagger-UI 中得到的内容如下所示

{
   "name": "string",
   "imagen": "string"
}

而我想要的是下面的

{
   "name": "null", or "name": ""
   "imagen": "null" or "imagen": ""
}

我在 Maven POM 中使用它

        <!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.5.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-common -->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-common</artifactId>
            <version>1.5.11</version>
        </dependency>

我的控制器是这样实例化的

@RestControllerAdvice
@RequestMapping("gender")
@CrossOrigin("*")
public class GenderController {

    private GenderService genderService;

    @Autowired
    public GenderController(GenderService genderService) {
        this.genderService = genderService;
    }

    @PostMapping("create")
    @Operation(description = "Create a gender in the DB")
    public ResponseEntity<?> createGender(
                 GenderCompressDto dto) throws Exception {
        try {
            GenderCompressDto genderDto = dto;
        } catch (InvalidFileNameException e) {
            throw new InvalidFileNameException(e.getMessage(),
                    "Field empty or null");
        }
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }
}

我的 GenderCompressDto

@Data
@AllArgsConstructor
@NoArgsConstructor
public class GenderCompressDto {
    private String name = null;
    private String imagen = null;
}

标签: spring-bootswagger-uispringdocspringdoc-openapi-ui

解决方案


推荐阅读