我想使用本地化来本地化 Swagger 文档。但我只能为 Annotations 提供编译时常量。所以我很困惑如何提供来自 messages_**.properties 的读取消息并将其提供给注释。消息来源:@Configurationpublic 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 中读取消息:@Componentpublic 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(); }我想阅读这些注释中的消息。任何指导或建议将不胜感激。
2 回答
收到一只叮咚
TA贡献1821条经验 获得超5个赞
最好将文档注释与代码分离(从外部属性文件中读取文本而不是作为纯文本插入)
像这样使用占位符,而不是
@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
12345678_0001
TA贡献1802条经验 获得超5个赞
application.properties您可以使用SPELie获取值${}:
@Annotation(value="${request.reminder.mails.cron.expression}")
注意:- 道具名称应该是完整的名称application.properties。
添加回答
举报
0/150
提交
取消
