我想使用本地化对Swagger文档进行本地化。但是我只能为“注释”提供编译时间常数。所以我很困惑如何提供来自messages _ **。properties的已读消息并将其提供给注释。
消息源:
@Configuration
public class CustomMessageSourceConfig {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Bean
public LocalValidatorFactoryBean getValidator() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource());
return bean;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.ENGLISH);
return slr;
}
}
从messages _ **。properties中读取消息:
@Component
public class MessagesByLocaleServiceImpl implements MessagesByLocaleService {
@Autowired
private MessageSource messageSource;
@Override
public String getMessage(String id) {
Locale locale = LocaleContextHolder.getLocale();
return StringEscapeUtils.unescapeJava(messageSource.getMessage(id, null, locale));
}
}
这是我在Java代码中读取消息的方式:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot"))).build()
.apiInfo(apiInfo())
.tags(new Tag("Netmap Mode Service", messageSource.getMessage(MessageCodes.SWAGGER_WINDOWS_ONLY)));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title(messageSource.getMessage(MessageCodes.SWAGGER_TITLE))
.description(messageSource.getMessage(MessageCodes.SWAGGER_DESCRIPTION))
.contact(messageSource.getMessage(MessageCodes.SWAGGER_CONTACT)).build();
}
但是如何将这些消息提供给Swagger注释。
@ApiOperation(value = "Add Netmap mode ", notes = "**I want to read properties here**")
@ApiImplicitParams({
@ApiImplicitParam(value = SwaggerSinglePoint.DESC_MODE_NAME, dataType = CDSwaggerPrimitives.STRING, name = SwaggerSinglePoint.MODE_NAME, paramType = CDSwaggerPrimitives.PARAMA_TYPE_QUERY),
@ApiImplicitParam(value = SwaggerSinglePoint.DESC_MODE_BUFFER_SIZE, dataType = CDSwaggerPrimitives.INETEGER, name = SwaggerSinglePoint.BUFFER, paramType = CDSwaggerPrimitives.PARAMA_TYPE_QUERY)})
@RequestMapping(method = RequestMethod.POST, produces = CDConstants.JSON_RESPONSE_DATA_FORMAT, consumes = CDConstants.JSON_REQUEST_DATA_FORMAT)
@SuppressWarnings({ "squid:S3776", "squid:S1319", "unused" })
public String testController(@RequestBody(required = false) HashMap requestParamMap, HttpServletResponse response,
HttpServletRequest request) {
我想阅读这些注释中的消息。任何指导或建议将不胜感激。
答案 0 :(得分:0)
您可以使用application.properties
,即SPEL
从${}
获取值:
@Annotation(value =“ $ {request.reminder.mails.cron.expression}”)
注意:-道具名称应为application.properties
的全名。
答案 1 :(得分:0)
始终最好将文档注释与代码分离(从外部属性文件中读取文本,而不是将其插入为纯文本)
像这样使用占位符,而不是
@ApiOperation(value = "Add Netmap mode " ,...)
使用
@ApiOperation(value = ${message.addNetMode} ,...)
在“ messages _ **。properties”文件中,应该有键值对
message.addNetMode=Add Netmap mode
还在类级别上在配置中注册属性文件
@PropertySource("classpath:messages_**.properties")
**请注意,某些注释的值可能不受支持。请参阅文档http://springfox.github.io/springfox/docs/current/#support-for-documentation-from-property-file-lookup