gpt4 book ai didi

spring-boot - Springfox Java Bean 验证未显示在 Swagger 输出中

转载 作者:行者123 更新时间:2023-12-05 01:44:05 26 4
gpt4 key购买 nike

我正在尝试按照 Springfox Swagger 的文档使 Java Bean 验证正常工作 (http://springfox.github.io/springfox/docs/current/#springfox-support-for-jsr-303),但它们没有出现在 Swagger UI 中。

这是我的 Spring 配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Import({springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class})
@Configuration
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("My API")
.build();
}
}

这是我的请求映射:

@ApiOperation(value = "Use to get token for internal applications")
@PostMapping(value = AuthUris.TOKEN)
public AuthResponse token(@Valid @RequestBody AuthRequest authRequest) {
// implementation omitted
}

这是我的 POJO:

@ApiModel
public class AuthRequest {
@ApiModelProperty
@NotNull
@Size(min = 4, max = 50)
private String username;
@NotNull
private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

我希望在 Swagger UI 中捕获 NotNull 和 Size 注释,但事实并非如此。请帮助我理解这应该如何工作。谢谢。

enter image description here .

感谢https://stackoverflow.com/users/8012379/indra-basak我确实看到他们在工作。但是,我必须将鼠标悬停在该字段上才能像@Size 一样思考。请参阅下面的屏幕截图。 enter image description here

最佳答案

  • 如果你使用的是springfox版本2.7.0 , 两者 @NotNull@Size注释应该有效。

  • 你的 @NotNull注释已在 password field 中工作.

  • 如果 @ApiModelProperty字段存在注释,它优先于 @NotNull注解。 username 就是这种情况。 field 。它显示为 optional因为required @ApiModelProperty 的属性注释设置为 false默认情况下。

如果你使用springfox版本2.7.0并且不要使用 @ApiModelProperty注释,模型将显示为:

enter image description here

验证

例如,如果您输入的用户名小于最小长度 4,则会出现以下异常:

{
"timestamp": 1511550365198,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"errors": [
{
"codes": [
"Size.authRequest.username",
"Size.username",
"Size.java.lang.String",
"Size"
],
"arguments": [
{
"codes": [
"authRequest.username",
"username"
],
"arguments": null,
"defaultMessage": "username",
"code": "username"
},
50,
4
],
"defaultMessage": "size must be between 4 and 50",
"objectName": "authRequest",
"field": "username",
"rejectedValue": "s",
"bindingFailure": false,
"code": "Size"
}
],
"message": "Validation failed for object='authRequest'. Error count: 1",
"path": "/tokens"
}

关于spring-boot - Springfox Java Bean 验证未显示在 Swagger 输出中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47312572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com