问题描述
我正在使用 Spring Boot + Spring Data Mongo + Spring REST + Spring HATEOAS 来实现REST端点.
I am using Spring Boot + Spring Data Mongo + Spring REST + Spring HATEOAS to implement REST endpoints.
由于我们要传递5个以上的查询参数(组织的专有设置,应该不传递),因此我尽管创建了EmployeeDto类并在Controller处传递了该类
Since we're passing more than 5 Query Parameters (proprietary setting of an Org, supposed to be not passed), so I though to create a EmployeeDto Class and pass that class at Controller
@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);
}
Swagger用户界面显示为-
Swagger UI it shows like -
{
"firstName": "string",
"lastName": "string",
"age": 0,
"languageCd": "string",
"isActive": "string",
"email": "string",
"regionCd": "string"
}
CURL命令:
EmployeeDto.java
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)如何设置每个字段的默认值,而不是似乎默认的字符串"?
1) How to set the default value for each field instead of "string" which seem coming default?
2)如何简单地允许在OAS3 UI中查看实际查询参数?货币,看起来像身体.
2) How to simply allow to see actual query parameters in OAS3 UI ? Currency, it look like body.
推荐答案
我能够自己解决此问题. 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.
I was able to solve this issue myself. 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"
值,该方法将重置;如果是,它将重置为""
,对于Integer
至null
相同.
and at the controller simply add the below method that will reset if "null"
value sent, if yes it will reset it to ""
and same for Integer
to 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;
}
这篇关于Spring文档-Open API 3-如何将默认值设置为body?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!