【问题标题】:Springfox Swagger generates fields not in the method signatureSpringfox Swagger 生成不在方法签名中的字段
【发布时间】:2019-07-02 13:37:47
【问题描述】:

我为我的 Spring Boot 应用程序启用了 springfox swagger。这是代码设置

SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig
{
    @Bean
    public Docket api()
    {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.ant("/api/**")).build().apiInfo(metaData());
    }

    private ApiInfo metaData()
    {
        ApiInfo apiInfo = new ApiInfo("myApp REST API", "REST APIs for myApp", "1.0.0", "", new Contact("", "", ""), "Proprietary", "", Collections
                .emptyList());
        return apiInfo;
    }
}

这是我的控制器

@RequestMapping("/api/user")
@RestController
public class UserApiController
{
    @Autowired
    UserService userService;

    @Secured(value = { "ROLE_ADMIN", "PERMISSION_LIST_USERS" })
    @GetMapping(value = "/list", produces = "application/json")
    List<UserBO> listUsers(HttpSession session)
    {
        Long companyId = (Long) session.getAttribute("companyId");
        List<UserBO> users = userService.listUsers(companyId);
        return users;
    }

    @Secured(value = { "ROLE_ADMIN", "PERMISSION_USER_CREATE" })
    @PostMapping("/create")
    ResponseEntity<String> createUser(@RequestParam String login, @RequestParam String password, @RequestParam String firstName, @RequestParam String lastName, @RequestParam Long usergroupId, HttpSession session)
    {
        Long companyId = (Long) session.getAttribute("companyId");
        ResponseEntity<String> response = userService
                .createUser(login, password, firstName, lastName, usergroupId, companyId);
        return response;
    }

    ...
    ...
    ...
}

这是我看到的招摇用户界面

还有很多其他参数不在方法签名中(creationTime、lastAccessedTime 等)。

为什么 swagger 会生成这些,我该如何防止它并且只生成方法签名中的参数?

【问题讨论】:

  • 因为你有 HttpSession 作为方法参数。
  • @AlanHay 你是对的,当我删除 HttpSession 参数时条目消失了,我该如何隐藏或使它不出现在 api 文档中
  • 想通了,为HttpSession参数添加@ApiIgnore注解

标签: spring-boot swagger-ui springfox


【解决方案1】:

我终于弄明白了,正如@AlanHay 所指出的那样,显示其他参数是因为HttpSession 对象在方法签名中,为了忽略我不得不为@ 使用@ApiIgnore 注释987654323@方法签名中的参数。

所以方法是

@Secured(value = { "ROLE_ADMIN", "PERMISSION_LIST_USERS" })
@GetMapping(value = "/list", produces = "application/json")
List<UserBO> listUsers(@ApiIgnore HttpSession session)
{
    Long companyId = (Long) session.getAttribute("companyId");
    List<UserBO> users = userService.listUsers(companyId);
    return users;
}

【讨论】:

    【解决方案2】:

    尝试添加 Docket bean 配置:

        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.springbackend.controller"))
                .paths(PathSelectors.regex(".*"))
                .build()
                .apiInfo(metaData());
         }
    

    【讨论】:

    • 我已经加了,你的意思是改成RequestHandlerSelectors.basePackage("com.example.springbackend.controller"),我也试过了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 2016-12-23
    相关资源
    最近更新 更多