PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/sdk/spring/azure-spring-boot-test-application/src/main/java/com/azure/test/Application.java

http://github.com/WindowsAzure/azure-sdk-for-java
Java | 91 lines | 73 code | 16 blank | 2 comment | 5 complexity | 5f48fe8f459c0be1d3a6d3b4b2311323 MD5 | raw file
Possible License(s): MIT
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. package com.azure.test;
  4. import com.alibaba.fastjson.JSON;
  5. import com.fasterxml.jackson.core.JsonProcessingException;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.boot.CommandLineRunner;
  10. import org.springframework.boot.SpringApplication;
  11. import org.springframework.boot.autoconfigure.SpringBootApplication;
  12. import org.springframework.core.env.ConfigurableEnvironment;
  13. import org.springframework.core.env.MutablePropertySources;
  14. import org.springframework.core.env.PropertySource;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.PathVariable;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. @SpringBootApplication
  22. @RestController
  23. public class Application implements CommandLineRunner {
  24. @Autowired
  25. private ConfigurableEnvironment environment;
  26. @Value("${spring.cosmos.db.key:local}")
  27. private String cosmosDBkey;
  28. private static ObjectMapper mapper = new ObjectMapper();
  29. public static void main(String[] args) {
  30. SpringApplication.run(Application.class, args);
  31. }
  32. @GetMapping("hello")
  33. public String hello() {
  34. try {
  35. return mapper.writeValueAsString(System.getenv());
  36. } catch (JsonProcessingException e) {
  37. e.printStackTrace();
  38. return "Some error happens";
  39. }
  40. }
  41. @GetMapping("get")
  42. public String get() {
  43. return cosmosDBkey;
  44. }
  45. @GetMapping("env/{key}")
  46. public String env(@PathVariable String key) {
  47. final String property = environment.getProperty(key);
  48. return property;
  49. }
  50. @GetMapping("list")
  51. public String list() {
  52. final List list = new ArrayList();
  53. final MutablePropertySources propertySources = this.environment.getPropertySources();
  54. final Iterator<PropertySource<?>> iterator = propertySources.iterator();
  55. while (iterator.hasNext()) {
  56. final PropertySource<?> next = iterator.next();
  57. list.add(next.getName());
  58. }
  59. return JSON.toJSONString(list);
  60. }
  61. @GetMapping("getSpecificProperty/{ps}/{key}")
  62. public String getSpecificProperty(@PathVariable String ps, @PathVariable String key) {
  63. final MutablePropertySources propertySources = this.environment.getPropertySources();
  64. final PropertySource<?> propertySource = propertySources.get(ps);
  65. if (propertySource != null) {
  66. final Object property = propertySource.getProperty(key);
  67. return property == null ? null : property.toString();
  68. } else {
  69. return null;
  70. }
  71. }
  72. public void run(String... varl) throws Exception {
  73. System.out.println("property your-property-name value is: " + cosmosDBkey);
  74. }
  75. }