问题
As the title says, is it possible to include a POJO in a swagger doc if it's not used in a controller method?
I've tried using the @ApiModel annotation on the POJO class, i.e.:
@ApiModel("POJO")
public class Pojo {
...
}
However, unless the POJO is returned by a controller, I haven't been able to have it appear in the generated swagger docs. Is there a way to accomplish this?
I'm using springfox version 2.9.2, by the way.
回答1:
It is possible with Springfox. You just have to modify your Docket
. Add additionalModels
method to your Docket
implementation:
@Autowired
private TypeResolver resolver;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
...
.additionalModels(resolver.resolve(Pojo.class));
}
来源:https://stackoverflow.com/questions/52841563/springfox-is-it-possible-to-document-a-pojo-via-annotation-if-its-not-used-in