Springfox Swagger生成不在方法签名中的字段

时间:2019-07-02 13:37:47

标签: spring-boot swagger-ui springfox

我为我的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;
    }

    ...
    ...
    ...
}

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

enter image description here

方法签名中还包含很多其他参数( creationTime,lastAccessedTime 等)。

为什么昂首阔步地生成了这些?如何防止它并且仅生成方法签名中的参数?

2 个答案:

答案 0 :(得分:0)

尝试添加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());
     }

答案 1 :(得分:0)

我终于弄清楚了,正如@AlanHay指出的,其他参数之所以显示是因为HttpSession对象在方法签名中,并且为了忽略我必须使用@ApiIgnore方法签名中HttpSession参数的注释。

所以方法应该是

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