2017-08-04 40 views
0

揚鞭在彈簧引導配置僅示出一個方法與POST映射和一個方法以獲得從每個控制器映射。揚鞭忽略與另一種方法GETPOST映射而忽略PUTDELETE映射的所有方法。我的配置:揚鞭在Spring引導配置示出了僅與POST方法及GET映射

@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(值= 「/ API /用戶」,產生=「應用/ 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(); 
} 

}

可能是我需要添加一些更多的配置或添加別的東西?

+0

你能不能請出示一些控制器代碼? –

+0

我添加了控制器代碼。其他控制器是一樣的。 Swagger只顯示查詢方法和創建方法。 –

回答

1

根據您的控制器上,我想你應該在路徑匹配您的招搖配置增加一個星:

.paths(PathSelectors.ant("/api/**"))

如/ API /用戶/電流將不會受到/ API匹配/ *而是通過/ api/**,這就是爲什麼你只能獲得記錄的基本路徑端點。

+0

當我添加一個星號時,應用程序拋出一個異常: 'org.springframework.context.ApplicationContextException:無法啓動bean'documentationPluginsBootstrapper';嵌套的異常是com.google.common.util.concurrent.UncheckedExecutionException:java.lang.ArrayIndexOutOfBoundsException:1' 你不知道這個異常是什麼?) –

+0

奇怪。你可以嘗試一個正則表達式:'.paths(regex(「/ api。*」))''或'.paths(PathSelectors.ant(「/ api **」))' –

+0

我解決了它。 謝謝,扎卡里亞。你的回答是正確的。 出現此異常是因爲在我的一個控制器中,我有參數「java.util.Map」,沒有任何泛型。當我添加「」到我的地圖 - 我解決了問題。 我的下一個任務 - 添加授權,就像swagger.io中的示例項目一樣。 –