我有Springboot Application for Rest Service。休息服务使用Swagger / springfox注释进行了记录,我想在我的App中使用i18n文件属性来实现国际化功能。...
就像我在控制器中所做的那样,我动态地获取了@ApiResponses批注中“消息”的属性?
@ApiResponses(value = {
@ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Successful login to platform"),
@ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "Unauthorized request to platform")
})
谢谢
答案 0 :(得分:0)
似乎目前不支持(2018-08-08)您可以在这里查看问题:link
答案 1 :(得分:0)
有类似的问题。
首先,如果您查看当前的Springfox(版本2.9.2)文档,它们立即支持以下注释:
@ApiParam#value()
@ApiImplicitParam#value()
@ApiModelProperty#value()
@ApiOperation#value()
@ApiOperation#notes()
@RequestParam#defaultValue()
@RequestHeader#defaultValue()
我花了一些时间来获得动态@ApiResponse
消息,因此我将发布回复以帮助他人。
解决方案基于https://github.com/springfox/springfox/issues/1180
首先,我将以下内容添加到了大张旗鼓的配置文件中:
@Bean
public TranslationOperationBuilderPlugin translationPlugin() {
return new TranslationOperationBuilderPlugin();
}
//important to keep this LOWEST_PRECEDENCE!!!
@Order(Ordered.LOWEST_PRECEDENCE)
public static class TranslationOperationBuilderPlugin implements OperationBuilderPlugin {
@Autowired
protected Environment env;
@Override
public boolean supports(DocumentationType delimiter) {
return true;
}
@Override
public void apply(OperationContext context) {
Set<ResponseMessage> messages = context.operationBuilder().build().getResponseMessages();
Set<ResponseMessage> translated = new HashSet<>();
for (ResponseMessage untranslated : messages) {
String translation = env.getProperty(untranslated.getMessage());
translated.add(new ResponseMessage(untranslated.getCode(),
translation,
untranslated.getResponseModel(),
untranslated.getHeaders(),
untranslated.getVendorExtensions()
));
}
context.operationBuilder().responseMessages(translated);
}
}
在控制器类上,我添加@PropertySource(value= "classpath:swagger.properties", encoding="UTF-8")
该文件位于常规资源目录中,并且包含code_400=my message
然后是控制器中的方法
@ApiResponses(value = {
@ApiResponse(code = 400, message = ResponseKeys.MESSAGE_400)
})
最后ResponseKeys包含:
public class ResponseKeys {
/* 4xx messages */
public static final String MESSAGE_400 = "code_400";
}