gpt4 book ai didi

java - 如何读取注释中的messages.properties

转载 作者:行者123 更新时间:2023-12-02 10:20:43 26 4
gpt4 key购买 nike

我想使用本地化来本地化 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 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) {

我想阅读这些注释中的消息。任何指导或建议将不胜感激。

最佳答案

最好将文档注释与代码分离(从外部属性文件读取文本而不是作为纯文本插入)

像这样使用占位符,而不是

@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

关于java - 如何读取注释中的messages.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54360130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com