如何阅读Annotations中的messages.properties
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何阅读Annotations中的messages.properties相关的知识,希望对你有一定的参考价值。
我想使用本地化来本地化Swagger文档。但我只能为Annotations提供编译时常量。所以我很困惑如何从消息_ **。属性提供读取消息并将其提供给注释。
消息来源:
@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;
}
}
从消息_ **。属性中读取消息:
@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 Annotations。
@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) {
我想阅读这些注释中的消息。任何指导或建议将受到高度赞赏。
以上是关于如何阅读Annotations中的messages.properties的主要内容,如果未能解决你的问题,请参考以下文章
目标检测 - 如何在图片中标记Annotations中的坐标信息?
目标检测 - 如何在图片中标记Annotations中的坐标信息?
javamail 阅读邮件时,有啥理由使用 Message 而不是 MimeMessage 等?