java SpringFox Swagger - 模型中的可选和必填字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43771283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SpringFox Swagger - Optional and Mandatory fields in model
提问by Punter Vicky
I used SpringFox library for rest documentation of my spring boot app. When I click on model , all the elements are being returned as optional. Is there a way to display required elements as mandatory? Is there any additional configuration that needs to be added?
我使用 SpringFox 库作为我的 Spring Boot 应用程序的其余文档。当我点击 model 时,所有元素都作为可选元素返回。有没有办法将所需元素显示为强制性?是否有任何额外的配置需要添加?
回答by Ganesh
Yes by default All the fields will be optional. To mark a field as required you can use following annotation.
默认情况下是所有字段都是可选的。要将字段标记为必填字段,您可以使用以下注释。
@ApiModelProperty(required = true)
On the getter method of the field which should be required. This won't show the field as "mandatory". But the optional tag will be removed for this field in the documentation.
关于应该需要的字段的getter方法。这不会将该字段显示为“强制性”。但是文档中此字段的可选标记将被删除。
Hope this helps.
希望这可以帮助。
回答by Hasson
Try the a similar code in Swagger Configuration:
在 Swagger 配置中尝试类似的代码:
@Bean
public Docket api() {
List<ResponseMessage> list = new java.util.ArrayList<>();
list.add(new ResponseMessageBuilder().code(500).message("500 message")
.responseModel(new ModelRef("JSONResult?string?")).build());
list.add(new ResponseMessageBuilder().code(401).message("Unauthorized")
.responseModel(new ModelRef("JSONResult?string?")).build());
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().securitySchemes(Collections.singletonList(securitySchema()))
.securityContexts(Collections.singletonList(securityContext())).pathMapping("/")
.directModelSubstitute(LocalDate.class, String.class).genericModelSubstitutes(ResponseEntity.class)
.alternateTypeRules(newRule(
typeResolver.resolve(DeferredResult.class,
typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
typeResolver.resolve(WildcardType.class)))
.useDefaultResponseMessages(false).apiInfo(apiInfo()).globalResponseMessage(RequestMethod.GET, list)
.globalResponseMessage(RequestMethod.POST, list);
}
And in the controller mapping add @RequestBody @Valid MyRequestClass req
for example if you are passing objects in the request body, and if you are passing parameters add something like @RequestParam(value = "email", required = true, defaultValue = "") String email
并且在控制器映射中添加@RequestBody @Valid MyRequestClass req
例如,如果您在请求正文中传递对象,并且如果您正在传递参数,则添加类似@RequestParam(value = "email", required = true, defaultValue = "") String email
Also, see how in the config code how to reference a class with generic type, i.e "JSONResult?string?"
which is referencing JSONResult<String>
另外,请参阅如何在配置代码中如何引用具有泛型类型的类,即"JSONResult?string?"
正在引用的类JSONResult<String>
回答by etech
Support for bean validation annotations was added, specifically for @NotNull, @Min, @Max, and @Size in Springfox v2.3.2.
添加了对 bean 验证注释的支持,特别是 Springfox v2.3.2 中的 @NotNull、@Min、@Max 和 @Size。
You can place those annotations on any of your API models.
您可以将这些注释放在任何 API 模型上。
In order to use it add the springfox-bean-validators dependency:
为了使用它,添加 springfox-bean-validators 依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
</dependency>
Add to your application's configuration class:
添加到您的应用程序的配置类:
@Import({springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class})
See: https://springfox.github.io/springfox/docs/current/#springfox-support-for-jsr-303
参见:https: //springfox.github.io/springfox/docs/current/#springfox-support-for-jsr-303
回答by marisa79
I was with the same problem but with @etech tips I was able to see the required fields marked in swagger. All I did was upgrading springfox-swagger.version to 2.9.2 (from 2.4.0) and guava.version to 20.0 (from 15) plus the import at the application configuration class. Thank you.
我遇到了同样的问题,但是通过@etech 提示,我能够看到用 swagger 标记的必填字段。我所做的只是将 springfox-swagger.version 升级到 2.9.2(从 2.4.0)和 guava.version 到 20.0(从 15)以及应用程序配置类的导入。谢谢你。