在Spring Boot中配置的Swagger仅显示带POST和GET映射的方法
在Spring Boot中配置的Swagger仅显示一种使用POST映射的方法,以及每种控制器使用GET映射的一种方法。 Swagger忽略使用GET和POST映射的另一种方法,并忽略所有使用PUT和DELETE映射的方法。 我的配置:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("my.project.controllers"))
.paths(PathSelectors.ant("/api/*"))
.build();
}
}
pom.xml中的依赖关系:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
<scope>compile</scope>
</dependency>
我的控制器代码:
@RestController @RequestMapping(value =“/ api / users”,produce =“application / json; charset = UTF-8”)public class UserController {
@Autowired
private UserService userService;
protected UserService getService() {
return userService;
}
@RequestMapping(method = GET)
public Page<User> query(@RequestParam Map<String, Object> parameters, Pageable pageable) {
return getService().query(parameters, pageable);
}
@ResponseStatus(CREATED)
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<User> create(@RequestBody User entity) {
return ResponseEntity.status(HttpStatus.CREATED).body(getService().create(entity));
}
@RequestMapping(value = "/{id:[0-9]+}", method = RequestMethod.PUT)
public ResponseEntity<User> update(@PathVariable Long id, @RequestBody User entity) {
return ResponseEntity.ok(getService().update(id, entity));
}
@RequestMapping("/current")
public ResponseEntity current() {
return ResponseEntity.ok(userService.getUser());
}
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/{id:[0-9]+}/enable", method = RequestMethod.POST)
public void enable(@PathVariable("id") final long id) {
userService.enable(id);
}
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/{id:[0-9]+}/disable", method = RequestMethod.POST)
public void disable(@PathVariable("id") final long id) {
userService.disable(id);
}
@RequestMapping(value = "/histories", method = RequestMethod.GET)
public List<UserHistory> histories() {
return userService.histories();
}
}
可能是我需要添加一些更多的配置或添加其他东西?
基于你的控制器,我认为你应该在你的swagger配置中在路径匹配器中再添加一颗星星:
.paths(PathSelectors.ant("/api/**"))
例如/ api / users / current不会被/ api / *而是被/ api / **匹配,这就是为什么您只能获得记录的基本路径端点。
链接地址: http://www.djcxy.com/p/48707.html上一篇: Swagger configured in Spring Boot shows only methods with POST and GET mapping