Springfox Swagger 生成不在方法签名中的字段
Springfox Swagger generates fields not in the method signature
我为我的 spring 启动应用程序启用了 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;
}
...
...
...
}
这是招摇ui我明白了
还有很多其他参数不在方法签名中(creationTime、lastAccessedTime 等)。
为什么 swagger 会生成这些参数?我该如何防止它并只生成方法签名中的参数?
尝试添加 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());
}
我终于弄明白了,正如@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;
}
我为我的 spring 启动应用程序启用了 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;
}
...
...
...
}
这是招摇ui我明白了
还有很多其他参数不在方法签名中(creationTime、lastAccessedTime 等)。
为什么 swagger 会生成这些参数?我该如何防止它并只生成方法签名中的参数?
尝试添加 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());
}
我终于弄明白了,正如@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;
}