Bharath Bharath - 1 year ago 387
Java Question

Spring boot externalize config properties/messages on Java annotations

Is there a way to read text from external properties file in spring without using the

annotation. Ex:"hello"

I can inject it in a spring bean using

@Value("${}") String value;

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


and you externalize this property value in file.

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

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

@ApiOperation(value = "returns user details", notes = "some notes")
public User getUser() {

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

and the model,

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

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

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 file and spring knows by default to pick it up (or can be configured).

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).

Answer Source

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, or support Spring's MessageSource.

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

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

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.

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 for more details.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download