如何阅读Annotations中的messages.properties

问题描述 投票:-1回答:2

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

我想阅读这些注释中的消息。任何指导或建议将受到高度赞赏。

java spring-boot java-8
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.