Springfox 3:从 ResponseMessage 迁移到 Response 以覆盖错误消息
Springfox 3: migrate from ResponseMessage to Response to override error messages
因为 Springfox 3, ResponseMessage已弃用,我们应该改用 Response。
我如何正确地从 ResponseMessage 迁移到 Response 覆盖默认响应错误消息?
这是我要迁移的 springfox 2.9.2 兼容代码示例:
private static ResponseMessage responseMessageFor401() {
return new ResponseMessageBuilder()
.code( 401 )
.message( "ERROR_MESSAGE_401" )
.responseModel( new ModelRef( "StatusWrapper" ) )
.build();
}
终于找到答案了:
您必须将模型 class 添加到 Docket 配置中。
.additionalModels( typeResolver.resolve( StatusWrapper.class ) )
比:
private static Response responseMessageFor401() {
return new ResponseBuilder().code( "401" )
.description( "ERROR_MESSAGE_401" )
.representation( MediaType.APPLICATION_JSON )
.apply( DefaultResponse::buildDefaultModel )
.isDefault( true )
.build();
}
private static void buildDefaultModel( final RepresentationBuilder representationBuilder ) {
representationBuilder.model(
msBuilder ->
msBuilder.name( "StatusWrapper" )
.referenceModel( rmsBuilder ->
rmsBuilder.key(
mkBuilder ->
mkBuilder.isResponse( true )
.qualifiedModelName(
qmnBuilder -> qmnBuilder.name( "StatusWrapper" )
//package of your model class
.namespace( "com.example.swagger.configuration" )
.build() )
.build() )
.build() )
.build() );
}
因为 Springfox 3, ResponseMessage已弃用,我们应该改用 Response。
我如何正确地从 ResponseMessage 迁移到 Response 覆盖默认响应错误消息?
这是我要迁移的 springfox 2.9.2 兼容代码示例:
private static ResponseMessage responseMessageFor401() {
return new ResponseMessageBuilder()
.code( 401 )
.message( "ERROR_MESSAGE_401" )
.responseModel( new ModelRef( "StatusWrapper" ) )
.build();
}
终于找到答案了:
您必须将模型 class 添加到 Docket 配置中。
.additionalModels( typeResolver.resolve( StatusWrapper.class ) )
比:
private static Response responseMessageFor401() {
return new ResponseBuilder().code( "401" )
.description( "ERROR_MESSAGE_401" )
.representation( MediaType.APPLICATION_JSON )
.apply( DefaultResponse::buildDefaultModel )
.isDefault( true )
.build();
}
private static void buildDefaultModel( final RepresentationBuilder representationBuilder ) {
representationBuilder.model(
msBuilder ->
msBuilder.name( "StatusWrapper" )
.referenceModel( rmsBuilder ->
rmsBuilder.key(
mkBuilder ->
mkBuilder.isResponse( true )
.qualifiedModelName(
qmnBuilder -> qmnBuilder.name( "StatusWrapper" )
//package of your model class
.namespace( "com.example.swagger.configuration" )
.build() )
.build() )
.build() )
.build() );
}