Spring docs - Open API 3 - How to set default values to body?

前提是你 提交于 2020-05-23 07:41:52

问题


I am using Spring Boot + Spring Data Mongo + Spring REST + Spring HATEOAS to implement REST endpoints.

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 = countryOutPagedAssembler.toModel(response, employeeAssembler);

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

Swagger UI it shows like -

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

CURL Command:

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;
}

I am looking for -

1) How to set the default value for each field instead of "string" which seem coming default?

2) How to simply allow to see actual query parameters in OAS3 UI ? Currency, it look like body.


回答1:


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.

Code -

@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;
}

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;
}


来源:https://stackoverflow.com/questions/60853683/spring-docs-open-api-3-how-to-set-default-values-to-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!