1

I am using spring boot with an access to keycloak. I need to leave the keycloak configuration in the keycloak.json file I would like to get teh values from the json using the PropertySource annotation...

My application.yml

keycloak:
   configurationFile: "classpath:keycloak.json"

My keycloak.json

{
  "realm": "MyRealm",
  "auth-server-url": "https://keycloakURL",
....
}

My class

@Controller
@Configuration
@PropertySource("classpath:keycloak.json")
public class MyClass{

@Autowired
private Environment env;

@RequestMapping(value = "/mycontroller", method = RequestMethod.GET)
    public String method(Model model) {
    env.getProperty("auth-server-url")
}

The problem is that i am getting null for this environment variable

testeurFou
  • 71
  • 3
  • 11
  • Run in debug, put a breakpoint on env.getProperty, then look at the property sources on env and see if you have anything in the source for keycloak.json as a starting point. – PaulNUK Oct 20 '17 at 11:29
  • Possible duplicate of [Load spring-boot properties from json file](https://stackoverflow.com/questions/44564166/load-spring-boot-properties-from-json-file) – araknoid Oct 20 '17 at 12:18
  • I have the environment variables in the application.yml which is classpath:keycloak.json but not the content of keycloak.json file – testeurFou Oct 20 '17 at 12:43

3 Answers3

0

If env is null, maybe the issue is with the import.

org.springframework.core.env.Environment

Else if the value queried from the environment is empty. In spring boot application, configuration of JSON file as property is quite different please refer the Example 1, Example 2

0

I know using @Value is somewhat discouraged in favor of env.getProperty(), but I found the former much more reliable and straight forward. The proper way to use @Value from a PropertySource is described in mkyong's great tutorial

To resolve ${} in @Values, you must register a static PropertySourcesPlaceholderConfigurer in either XML or annotation configuration file.

maaw
  • 1,276
  • 13
  • 13
  • I have tried what you said about @value. @Value("${keycloak.configurationFile}") private String authServerUrl; Returns "classpath:keycloak.json" @Value("${auth-server-url}") private String authServerUrl; Spring wont start.. – testeurFou Oct 20 '17 at 12:32
  • @testeurFou It should be `@PropertySource("classpath:keycloak.json") public class MyClass{ @Value("${auth-server-url}") private String authServerUrl; }` – maaw Oct 20 '17 at 15:01
  • @testeurFou also I don't know about `@Controller` and `@Configuration` in the same file. At the very least (and assuming it works) it is recommendable to keep them separated. – maaw Oct 20 '17 at 15:10
0

It's not possible for now but we have a ticket for it https://issues.jboss.org/browse/KEYCLOAK-4942

Sébastien Blanc
  • 2,929
  • 1
  • 12
  • 11