Spring Boot 外部化 Java 注释上的配置属性/消息 [英] Spring boot externalize config properties/messages on Java annotations

查看:35
本文介绍了Spring Boot 外部化 Java 注释上的配置属性/消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不使用 @Value 注释的情况下在 spring 中从外部属性文件中读取文本.例如:application.properties

Is there a way to read text from external properties file in spring without using the @Value annotation. Ex: application.properties

var.foo="hello"

我可以使用

@Value("${var.foo}") String value;

作为类变量.有没有办法在不使用 @Value 注释的情况下包含此属性.类似于 JSR bean 验证的方式.

as a class variable. Is there a way to include this property without using the @Value annotation. Something like how JSR bean validation does.

@NotNull(message={custom.notnull})

并且您在 ValidationMessages.properties 文件中将此属性值具体化.

and you externalize this property value in ValidationMessages.properties file.

例如,如果我有一个资源(Web 组件)类,并且我必须使用 Swagger 注释来记录它们,

Example, if I have a resource(web component) class, and if I have to use Swagger annotations to document them,

@controller
@path("/")
@Api(description = "User Resource API")
public class UserResource {

    @Path("/users")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "returns user details", notes = "some notes")
    public User getUser() {

        return new User(123, "Joe", "Montana");
    }
}

和模型,

@ApiModel("user model")
public class User {

    @ApiModelProperty(value = "This represents user id")
    private String id;
    private String firstName;
    private String lastName;
    ...
}

您如何将此字符串/句子/消息外部化到外部属性文件.我认为这适用于一般的 Java 注释和 spring,而不是特定于 Swagger.我指定 Swagger 的原因是,就像 hibernate 验证 Java 库可以选择在外部 ValidationMessages.properties 文件中指定这些消息,并且 spring 知道默认情况下会选择它(或可以配置).

How do you externalize this string/sentences/messages to an external properties file. I think this applies to general Java annotations and spring and not specific to Swagger. The reason I specified Swagger is, if just like hibernate validation Java library has the option of specifying these messages in an external ValidationMessages.properties file and spring knows by default to pick it up (or can be configured).

Swagger 是否提供这样的选项?如果没有,我该如何设置?

Does Swagger provide such an option? If it does not, how do I setup one?

根本问题是,我不想让我的代码/逻辑与文档相关的数据(元数据)混淆.

Underlying problem is, I don't want to clutter my code/logic with documentation related data(meta data).

推荐答案

我不认为你能做到这一点,除非你正在使用的注释的底层实现要么带有他们自己的 i18n 标准(比如 ValidationMessages.properties),或者支持Spring的MessageSource.

I don't think you can achieve this unless the underlying implementation of the annotations you're using either come with their own standard for i18n (like ValidationMessages.properties), or support Spring's MessageSource.

在后一种选择的情况下,您应该做的就是将您的键/值对添加到 messages.properties 文件中,然后让框架完成剩下的工作:

In the case of the latter alternative, all you should have to do is to add your key/value pairs inside a messages.properties file and let the framework do the rest:

messages.properties

my.validation.error.message=Something went terribly wrong!

SomeClass.java

@MySpringCompatibleValidationAnnotation("my.validation.error.message")
private String someVariable;

最重要的是,根据您尝试使用的框架,它可能支持也可能不支持这种开箱即用的功能.

Bottom line is, depending on the framework you're trying to use, it may or may not support this out-of-the-box.

现在,就 Swagger 而言,i18n 对文档的支持被​​提议作为一项新功能,但在他们进一步考虑如何实施时,它被关闭或搁置.请参阅 https://github.com/OAI/OpenAPI-Specification/issues/274 了解更多详情.

Now, as far as Swagger goes, i18n support for documentation was proposed as a new feature, but it was shut down or put on hold while they put some more thought into how it should be implemented. See https://github.com/OAI/OpenAPI-Specification/issues/274 for more details.

这篇关于Spring Boot 外部化 Java 注释上的配置属性/消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆