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

1
  • While this code snippet may solve the question, including an explanation 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. Commented Apr 24, 2018 at 8:39

2 Answers 2

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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.