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 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– Clijsters2018-04-24 08:39:24 +00:00Commented Apr 24, 2018 at 8:39
Add a comment
|
2 Answers
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();
}
}
1 Comment
bpu
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.