我正在使用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
從揚鞭,代碼生成看起來產生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);
}
[...]
}
我想這些改變,但我仍然獲得了不必要的錯誤響應.... – Volker
我已經對我的帖子進行了更新。如果你想嘗試。我希望它有幫助。 –