0

So I'm new to Spring and I'm basically trying to make a REST service for the first time. Some of the data I'd like to return is some data from a properties file.

This is my configuration bean:

@Configuration
@PropertySource("classpath:client.properties")
public class PropertyConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer 
    propertySourcesPlaceholderConfigurer() {
         return new PropertySourcesPlaceholderConfigurer();
    }
}

This is the class containing the info I want to return from the API. When I hover over the values, I can see that the property is being injected.

public class ProviderInfo {

    @Value("${op.iss}") private String issuer;
    @Value("${op.jwks_uri}") private String jwksURI;
    @Value("${op.authz_uri}") private String authzURI;
    @Value("${op.token_uri}") private String tokenURI;
    @Value("${op.userinfo_uri}") private String userInfoURI;

    // Getter methods
}

And this is the RestController

@RestController
public class ProviderInfoController {

    @RequestMapping(value = "/provider-info", method = RequestMethod.GET)
    public ProviderInfo providerInfo() {
        return new ProviderInfo();
    }
}

When I navigate to that endpoint, everything is null:

{"issuer":null,"jwksURI":null,"authzURI":null,"tokenURI":null,"userInfoURI":null}

Can anybody see what I'm doing wrong? Or if there is a better way to accomplish this in general?

Thanks!

Luay Abdulraheem
  • 751
  • 3
  • 11

2 Answers2

3

The processing of the @Value annotations is done by Spring, so you need to get the ProviderInfo instance from Spring for the values to actually be set.

@RestController
public class ProviderInfoController {

    @Autowired
    private ProviderInfo providerInfo;

    @RequestMapping(value = "/provider-info", method = RequestMethod.GET)
    public ProviderInfo providerInfo() {
        return providerInfo;
    }
}

This also requires that Spring picks up and processes the ProviderInfo class.

twinklehawk
  • 628
  • 5
  • 12
0

Also, you need to add the ProviderInfo class to the Spring Bean life cycle using either @Component or @Service as follows:

@Component
public class ProviderInfo {

    @Value("${op.iss}") private String issuer;
    @Value("${op.jwks_uri}") private String jwksURI;
    @Value("${op.authz_uri}") private String authzURI;
    @Value("${op.token_uri}") private String tokenURI;
    @Value("${op.userinfo_uri}") private String userInfoURI;

    // Getter methods
}

Only then, you can use @Autowired inside ProviderInfoController class.

KayV
  • 12,987
  • 11
  • 98
  • 148