Spring Boot 将 Java 注释上的配置属性/消息外部化

有没有办法在 spring 中从外部属性文件中读取文本而不使用 @Value 注释.例如: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.

相关文章