vic
vic

Reputation: 2816

@Value Won't Pick Up Data

In a controller, I have something like

@Value("${api.host.baseurl}")
private String baseurl;

and in a configration file application.yml, I have

api:
  host:   
    baseurl: localhost:3000/api/v1

The @Value doesn't pick the data up in the controller, however.

Update:

I didn't make my question clear in my original post. The data is picked in one environment (dev), but not in the other environment (prod). Why? Those configuration files are set up by JHipster, BTW.

Upvotes: 0

Views: 93

Answers (2)

ray
ray

Reputation: 1680

This can be because of these reason,

First reason can be application.yml (or application.property) file. Since loading property file can be changed when active profile is changed, make sure this property is available in both dev and prod property files. Since you have mentioned that "data is picked in one environment (dev), but not in the other environment (prod)", this is can be the reason.

@value can inject values to you class only if that is a spring bean. If it is a simple POJO, then value will not be injected. Since you have mentioned that you have used this inside a controller (controller means this class is annotated by @RestController or @Controller) and worked in dev environment, this cannot be the reason.

Update

Its quite difficult to assume what is the issue without knowing all the facts. This is what I think. If your whole application-prod.yml is ignored in prod environment, issue should be passing active profile. For debug that you can check the application log inside your container. If profile is detected their should be a log like this,

ray.ApplicationMainClass       : The following profiles are active: prod

If active profile is not correctly set, log will be,


ray.ApplicationMainClass       : No active profile set, falling back to default profiles: default

It will be helpful to know how you set active profile in prod env.

If issue only there for api.host.baseurl: localhost:3000/api/v1 propety, then issue should be the property itself.

Upvotes: 0

vaibhavsahu
vaibhavsahu

Reputation: 682

Annotating your controller class with @Configuration should fix the issue

or

@Value("${api.host.baseurl:localhost:3000/api/v1}")
private String baseurl;

Upvotes: 0

Related Questions