0

i'm trying to access the values of properties file in spring framework. now i have bean file and controller. so how to access properties file value in json formate using bean

  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Clijsters Apr 24 '18 at 08:39

2 Answers2

1

For accessing single value can be used Spring annotations "PropertySource" and "Value".

@PropertySource("classpath:application.properties")
public class SomeClass {
    @Value("${some.property}")
    private String someProperty;
    ... 
}

For accessing/looping all spring properties, check this solution looping-through-all-the-properties-in-a-file-with-spring-and-java

Controller sample code:

@RestController
public class PropertiesController {
    @Autowired
    Properties props;

    @RequestMapping(value = {"/properties"}, method = RequestMethod.GET, produces= MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Set<Map.Entry<Object, Object>> getProperties() {
        return props.entrySet();
    }
}
bpu
  • 61
  • 5
  • In case if providing all application properties via rest interface be careful not to share sensitive data: URL's, connection parameters, etc. It is better to filter necessary parameters only by creating custom List. – bpu Apr 24 '18 at 11:36
0

if you are Using spring-boot then add spring-actuator dependency which by defaults expose /env endpoint and spits out all the properties loaded in the spring container in json format.

Raghvendra Garg
  • 425
  • 1
  • 4
  • 11