Spring docs-Open API 3-如何将默认值设置为body?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring docs-Open API 3-如何将默认值设置为body?相关的知识,希望对你有一定的参考价值。

我正在使用Spring Boot + Spring Data Mongo + Spring REST + Spring HATEOAS来实现REST端点。

由于我们传递了5个以上的查询参数(组织的专有设置,应该不传递),所以我尽管创建了EmployeeDto类并在Controller处传递了该类

@GetMapping(value = "/employees/geographies", produces = {MediaType.APPLICATION_JSON })
public ResponseEntity<PagedModel<EmployeeModel>> findEmployees(
        EmployeeDto dto,
        @Parameter(hidden=true) String sort,
        @Parameter(hidden=true) String order,
        @Parameter(hidden=true) Pageable pageRequest) {

    // Add needed logic 
    ......
    ......
    ......
    PagedModel<EmployeeModel> model = countryOutPagedAssembler.toModel(response, employeeAssembler);

    return new ResponseEntity<>(model, HttpStatus.OK);
}

Swagger UI它显示为-

{
  "firstName": "string",
  "lastName": "string",
  "age": 0,
  "languageCd": "string",
  "isActive": "string",
  "email": "string",
  "regionCd": "string"
}

CURL命令:

curl -X GET“ http://localhost:8080/employee-data/employees/geographies?firstName=string&lastName=string&age=0&languageCd=string&isActive=string&email=string&regionCd=string&page=0&size=25&sort=firstName&order=ASC” -H“ accept:application / json”

EmployeeDto.java

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@Schema
public class EmployeeDto {
    @Schema(description = AppConstants.FIRSTNAME, defaultValue="")
    private String firstName; 

    @Schema(description = AppConstants.LASTNAME, defaultValue="")
    private String lastName; 

    @Schema(description = AppConstants.AGE, defaultValue="")
    private Integer age; 

    @Schema(description = AppConstants.LANG_CD_DESC, defaultValue="0")
    private String languageCd;

    @Schema(description = AppConstants.ISACTIVE, defaultValue="")
    private String isActive; 

    @Schema(description = AppConstants.EMAIL, defaultValue="")
    private String email; 

    @Schema(description = AppConstants.REGION_CD_DESC, defaultValue="")
    private String regionCd;
}

我正在寻找-

1)如何为每个字段设置默认值,而不是似乎默认的“字符串”?

2)如何简单地允许在OAS3 UI中查看实际查询参数?货币,看起来像身体。

答案

我能够自己解决此问题。 example - Provides an example of the schema. When associated with a specific media type, the example string shall be parsed by the consumer to be treated as an object or an array.

代码-

@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@Schema
public class EmployeeDto {
    @Schema(description = AppConstants.FIRSTNAME, type = "string", example = " ")
    private String firstName; 

    @Schema(description = AppConstants.LASTNAME, type = "string", example = " ")
    private String lastName; 

    @Schema(description = AppConstants.AGE, type = "string", example = "null")
    private Integer age; 

    @Schema(description = AppConstants.LANG_CD_DESC, type = "string", example = " ")
    private String languageCd;

    @Schema(description = AppConstants.ISACTIVE, type = "string", example = " ")
    private String isActive; 

    @Schema(description = AppConstants.EMAIL, type = "string", example = " ")
    private String email; 

    @Schema(description = AppConstants.REGION_CD_DESC, type = "string", example = " ")
    private String regionCd;
}

并且在控制器上,简单地添加以下方法,如果发送了"null"值,它将重置;如果是,它将重置为"",对于Integernull同样。

public static Object getDefaulter(Object obj) {
    Arrays.stream(obj.getClass().getDeclaredFields()).map(f -> {
        f.setAccessible(true);
        try {
            // set null values only if found String value as "null"
            if (Objects.equals(f.get(obj), "null")) {
                f.set(obj, "");
            }else if(Objects.equals(f.get(obj), 0)) {
                f.set(obj, null);
            }
        } catch (IllegalArgumentException | IllegalAccessException e) {
            //
        }
        return f;
    }).collect(Collectors.toList());
    return obj;
}

以上是关于Spring docs-Open API 3-如何将默认值设置为body?的主要内容,如果未能解决你的问题,请参考以下文章

Spring 4.x/3.x (Web MVC) REST API 和 JSON2 Post 请求,如何一劳永逸?

如何集成 AngularJS 和 Spring-OAuth2?

Spring cloud微服务安全实战-3-6API安全机制之数据校验_batch

如何在 Spring Boot 中使用 MySql 数据库查询?

Spring框架文档与API(4.3.6版本)

如何覆盖 Google API 请求权限页面中的 grant_heading