Springfox - 如果 POJO 未在控制器中使用,是否可以通过注释记录它 [英] Springfox - Is it possible to document a POJO via annotation if it's not used in a controller

查看:29
本文介绍了Springfox - 如果 POJO 未在控制器中使用,是否可以通过注释记录它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,如果控制器方法中没有使用 POJO,是否可以在 swagger 文档中包含它?

As the title says, is it possible to include a POJO in a swagger doc if it's not used in a controller method?

我尝试在 POJO 类上使用 @ApiModel 注释,即:

I've tried using the @ApiModel annotation on the POJO class, i.e.:

@ApiModel("POJO")
public class Pojo {
  ...
}

但是,除非控制器返回 POJO,否则我无法让它出现在生成的 swagger 文档中.有没有办法做到这一点?

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?

顺便说一下,我使用的是 springfox 2.9.2 版.

I'm using springfox version 2.9.2, by the way.

推荐答案

Springfox 可以实现.你只需要修改你的Docket.将 additionalModels 方法添加到您的 Docket 实现中:

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

这篇关于Springfox - 如果 POJO 未在控制器中使用,是否可以通过注释记录它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆