SpringDocs-OpenAPI3-如何设置body的默认值?
我使用Spring Boot+Spring Data Mongo+Spring REST+Spring HATEOAS实现REST端点 由于我们要传递5个以上的查询参数(一个组织的专有设置,应该不会被传递),所以我想创建一个EmployeeDto类并在Controller上传递该类SpringDocs-OpenAPI3-如何设置body的默认值?,spring,springdoc,springdoc-openui,Spring,Springdoc,Springdoc Openui,我使用Spring Boot+Spring Data Mongo+Spring REST+Spring HATEOAS实现REST端点 由于我们要传递5个以上的查询参数(一个组织的专有设置,应该不会被传递),所以我想创建一个EmployeeDto类并在Controller上传递该类 @GetMapping(value = "/employees", produces = {MediaType.APPLICATION_JSON }) public ResponseEntity<PagedMo
@GetMapping(value = "/employees", 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 = employeePagedAssembler.toModel(response, employeeAssembler);
return new ResponseEntity<>(model, HttpStatus.OK);
}
CURL命令:
curl-X GET“--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) 如何简单地允许在OAS3UI中查看实际的查询参数?货币,它看起来像身体。我自己能够解决这个问题<代码>示例-提供架构的示例。当与特定媒体类型关联时,消费者应将示例字符串解析为对象或数组。 代码-
@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”
将重置,如果是,则将重置为”
,对于整数
将重置为null
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;
}
不起作用。带空格的字符串将被忽略,就像空字符串一样。
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;
}