Swagger UI + Spring Boot中不需要的参数

问题描述 投票:0回答:1

我看到一个spring boot app的swagger ui中显示了很多不需要的字段。

我的swagger依赖关系是

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>

见附件截图enter image description here

我的休息控制器代码是这样的

    @ApiOperation(value = "Get Data")
    @GetMapping(value = "/applicant", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> getApplicant(@Valid  ApplicantRequestBean 
    applicantRequestBean, Errors errors) throws APIValidationException {
     if (errors.hasErrors()) {
     //check for constraintsviolation here and throw an error if found
     throw new APIValidationException(HttpStatus.BAD_REQUEST, "Bad Request", errorWrapper);
     }

}

我的ApplicationRequestBean有

    @NotBlank(message = "Id is mandatory")
    @Size(min = 9, max = 9, message = "Id should be 9 characters long")
    @ApiModelProperty(example = "@12345678", value = "Applicant Id", position = 3)
    private String id=null;
    @NotBlank(message = "Date of Birth is mandatory")
    @ApiModelProperty(example = "30-12-2000", value = "Applicant Date of Birth in DD-MM-YYYY format",  position = 2)
    private String dateOfBirth=null;
    @Email
    @ApiModelProperty(example = "[email protected]", value = "Applicant Email Id", position = 1)
    private String emailId=null;
    @ApiModelProperty(example = "971505504XXX", value = "Applicant Contact Number", position = 0)
    private String contactNumber=null;

我的APIValidationException类扩展了RuntimeException,有三个字段。

private HttpStatus status;
private String message;
private Object errors;

我的CustomExceptionHandler是这样的,它扩展了ResponseEntityExceptionHandler。

@RestControllerAdvicepublic class CustomExceptionHandler extends ResponseEntityExceptionHandler{。

 @ExceptionHandler(APIValidationException.class)
    public final ResponseEntity<Object> handleAPIValidationExceptions(APIValidationException ex, WebRequest request) {
        ErrorResponse error = new ErrorResponse("error", ex.getMessage(), ex.getErrors());
        return new ResponseEntity(error, HttpStatus.BAD_REQUEST);
    }

最后,MySwaager的配置是这样的

public Docket swaggerConfiguration() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(swaggerGroupName)
                .select()
                .paths(PathSelectors.ant("/api/**"))
                .apis(RequestHandlerSelectors.basePackage("a.b.c.d"))
                .build()
                .apiInfo(apiInfo())
                .tags(new Tag("Applicant", "Applicant APIs"));
    }
spring-boot swagger swagger-ui
1个回答
1
投票

我需要错误,但解决的方法是添加以下内容

.ignoredParameterTypes(Errors.class)到Swagger配置中。

在RestController的错误参数前加上@ApiParam(hidden = true)

© www.soinside.com 2019 - 2024. All rights reserved.