useDefaultResponseMessages(false) 不工作
useDefaultResponseMessages(false) not working
我正在使用 swagger-codegen 创建一个 spring-server。
我还使用了.useDefaultResponseMessages(false)
-属性
如
中所述
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());}
相关API部分: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)
不工作。
如何禁用这些默认错误响应?
@John Duskin
我更改了 Docketinitialization,将 @Controller
更改为 @Restcontroller
但我仍然收到 Get
的 404 消息
different looking 404-Message
从 Swagger-Codegen 生成的 Serverstubs 看起来像:
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);
}
[...]
}
尝试将对 useDefaultResponseMessages 的调用放在 Docket 中的构建方法之后。我已经更新了您的代码以说明我的意思。
希望对您有所帮助。
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 与控制器放在一起[=13=]
@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
}
}
解决了工作之外的问题...
这是一个 Spring 注释问题。
在我的 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());
}
}
我正在使用 swagger-codegen 创建一个 spring-server。
我还使用了.useDefaultResponseMessages(false)
-属性
如
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());}
相关API部分: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)
不工作。
如何禁用这些默认错误响应?
@John Duskin
我更改了 Docketinitialization,将 @Controller
更改为 @Restcontroller
但我仍然收到 Get
different looking 404-Message
从 Swagger-Codegen 生成的 Serverstubs 看起来像:
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);
}
[...]
}
尝试将对 useDefaultResponseMessages 的调用放在 Docket 中的构建方法之后。我已经更新了您的代码以说明我的意思。
希望对您有所帮助。
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 与控制器放在一起[=13=]
@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
}
}
解决了工作之外的问题...
这是一个 Spring 注释问题。
在我的 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());
}
}