2017-07-04 94 views
0

我正在使用swagger-codegen來創建一個spring-server。useDefaultResponseMessages(false)不工作

我還使用了.useDefaultResponseMessages(false) -attribute 中描述Swagger - Springfox always generates some response messages (401,403...) by default. How can I remove them?

SwaggerConfig.java:

public Docket customImplementation() { 
return new Docket(DocumentationType.SWAGGER_2) 
    .useDefaultResponseMessages(false) 
    .select() 
    .apis(RequestHandlerSelectors.basePackage("myrest.api")) 
    .build() 
    .directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class) 
    .directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class) 
    .apiInfo(apiInfo());} 

相關apipart:Api.java:

@ApiOperation(value = "", notes = "Returns all clouds from the system that the user has access to ", response = Cloud.class, responseContainer = "List", tags = { 
     "cloud",}) 
    @ApiResponses(value = { 
     @ApiResponse(code = 200, message = "All clouds ", response = Cloud.class), 
     /*@ApiResponse(code = 401, message = "Authorization for this action is missing", response = Error.class), 
     @ApiResponse(code = 403, message = "Forbidden action", response = Error.class), 
     @ApiResponse(code = 500, message = "An unexpected Error occured", response = Error.class),*/ 
     @ApiResponse(code = 504, message = "Server temporary not available", response = Error.class)}) 

    @RequestMapping(value = "/clouds", 
     produces = {"application/json"}, 
     method = RequestMethod.GET) 
    ResponseEntity<List<Cloud>> findClouds(); 

但招搖的UI仍然看起來像: swagger-ui: ResponseMessageTable

所以它似乎.useDefaultResponseMessages(false)不起作用。

如何禁用這些默認錯誤響應?


@約翰Duskin

我改變了Docketinitialization,改變了@Controller@Restcontroller但我仍然Get

different looking 404-Message

從揚鞭,代碼生成看起來產生Serverstubs獲得404消息如:

Api.java:

@Api(value = "clouds", description = "the clouds API") 
public interface CloudsApi { 

@ApiOperation(value = "", notes = "Returns all clouds from the system that the user has access to ", response = Cloud.class, responseContainer = "List", tags={ "cloud", }) 
@ApiResponses(value = { 
     @ApiResponse(code = 200, message = "All clouds ", response = Cloud.class), 
     @ApiResponse(code = 401, message = "Authorization for this action is missing", response = Cloud.class), 
     @ApiResponse(code = 403, message = "Forbidden action", response = Cloud.class), 
     @ApiResponse(code = 500, message = "An unexpected Error occured", response = Cloud.class), 
     @ApiResponse(code = 504, message = "Server temporary not available", response = Cloud.class) }) 

@RequestMapping(value = "/clouds", 
     produces = { "application/json" }, 
     method = RequestMethod.GET) 
ResponseEntity<List<Cloud>> findClouds(); 

和分離控制器:

@RestController 
public class CloudsApiController implements CloudsApi { 

@Autowired 
private UserService userService; 
@Autowired 
private CloudService cloudService; 

public ResponseEntity<List<Cloud>> findClouds() { 
//do some magic 
    return new ResponseEntity<List<Cloud>>(cloudList, HttpStatus.OK); 
} 
[...] 
} 

回答

0

解決了這一問題工作的旁邊......

這是一個春天的詮釋問題。 在我SwaggerDocumentationConfig.java我加入@EnableSwagger2,一切都會按想

@Configuration 
@EnableSwagger2 
public class SwaggerDocumentationConfig { 

@Bean 
public Docket customImplementation(){ 
    return new Docket(DocumentationType.SWAGGER_2) 
      .useDefaultResponseMessages(false) 
      .select() 
       .apis(RequestHandlerSelectors.basePackage("myrest.api")) 
       .build() 
      .directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class) 
      .directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class) 
      .apiInfo(apiInfo()); 
    } 
}  
0

嘗試把呼叫useDefaultResponseMessages在文案構建方法之後。我已更新您的代碼以顯示我的意思。

希望有所幫助。

public Docket customImplementation() { 
    return new Docket(DocumentationType.SWAGGER_2) 
     .select() 
     .apis(RequestHandlerSelectors.basePackage("myrest.api")) 
     .build() 
     .useDefaultResponseMessages(false) 
     .directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class) 
     .directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class) 
     .apiInfo(apiInfo()); 
} 

爲了讓我的機器上工作的電話我已經把ApiReponses與控制器

@RestController 
@RequestMapping("/my_model") 
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), 
    @ApiResponse(code = 500, message = "Rocks fell, everyone died.") }) 

更新

你把描述的方法?

這是我在我的項目上工作。請注意API響應附加到控制器。

@RestController 
@RequestMapping("/my_model/gogo") 
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), 
@ApiResponse(code = 500, message = "Rocks Fall") }) 
public class GoGoClass { 

@RequestMapping(method = RequestMethod.POST) 
@ApiOperation(value = "Description") 
public void run(
     @ApiParam(value = "Param1 description") @RequestParam(required = true) final String param1, 
     @ApiParam(value = "Param 2 description") final String param2) 
     throws ModelException { 
     // Do stuff 
     } 
} 
+0

我想這些改變,但我仍然獲得了不必要的錯誤響應.... – Volker

+0

我已經對我的帖子進行了更新。如果你想嘗試。我希望它有幫助。 –

相關問題