www說(shuō)
2022-05-20 18:37:51
我想使用本地化來(lái)本地化 Swagger 文檔。但我只能為 Annotations 提供編譯時(shí)常量。所以我很困惑如何提供來(lái)自 messages_**.properties 的讀取消息并將其提供給注釋。消息來(lái)源:@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(); }我想閱讀這些注釋中的消息。任何指導(dǎo)或建議將不勝感激。
2 回答

收到一只叮咚
TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
最好將文檔注釋與代碼分離(從外部屬性文件中讀取文本而不是作為純文本插入)
像這樣使用占位符,而不是
@ApiOperation(value = "Add Netmap mode " ,...)
采用
@ApiOperation(value = ${message.addNetMode} ,...)
在“messages_**.properties”文件里面應(yīng)該有鍵值對(duì)
message.addNetMode=Add Netmap mode
還要在類級(jí)別的配置中注冊(cè)屬性文件
@PropertySource("classpath:messages_**.properties")
**請(qǐng)注意,可能不支持某些注釋的值。請(qǐng)參閱文檔http://springfox.github.io/springfox/docs/current/#support-for-documentation-from-property-file-lookup

12345678_0001
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
application.properties
您可以使用SPEL
ie獲取值${}
:
@Annotation(value="${request.reminder.mails.cron.expression}")
注意:- 道具名稱應(yīng)該是完整的名稱application.properties
。
添加回答
舉報(bào)
0/150
提交
取消