I am currently having trouble trying to parse this VCAP_SERVICES to java objects. I do not quite understand how to structure the POJO to allow it to map the values from the json string. Can someone please help me structure my pojo so that it is aligns with the json string?
I want to create objects for both of the credentials: accessToken… jdbcurl.
VCAP_SERVICES
"VCAP_SERVICES": { "user-provided": [ { "credentials": { "accessTokenUri": "tokenurl", "apiUrl": "apiurl", "clientId": "typeofID", "clientSecret": "secretAf", "scope": "none" }, "syslog_drain_url": "", "volume_mounts": [], "label": "user-provided", "name": "OAuth2", "tags": [] }, { "credentials": { "jdbcUrl": "jdbc:oracle:connection[host]:[port]/service", "spring.datasource.driver-class-name": "oracle.jdbc.OracleDriver", "spring.datasource.initialize": "false" }, "syslog_drain_url": "", "volume_mounts": [], "label": "user-provided", "name": "Database", "tags": [] } ]
Java Class
ObjectMapper mapper = new ObjectMapper(); //json String to Object CupsProperties properties = mapper.readValue(VCAP_Services, CupsProperties.class); System.out.println(properties.getJdbcUrl() + "!!!!!!!!!!!!!!!!!!!");
POJOS
public class UserProviderWrapper { @JsonProperty("user-provided") public List<CupsProperties> cupsProperties; @JsonProperty("syslog_drain_url") public String syslog_drain_url; @JsonProperty("volume_mounts") public List<String> volume_mounts; @JsonProperty("label") public String label; @JsonProperty("name") public String name; @JsonProperty("tags") public List<String> tags; //getters and setters public class CupsProperties { @JsonProperty("jdbcUrl") public String jdbcUrl; @JsonProperty("spring.datasource.driver-class-name") public String driver; @JsonProperty("spring.datasource.initialize") public String initialize; //getters and setters
Error
Unrecognized field “user-provided” (class rest.springframework.model.CupsProperties), not marked as ignorable (2 known properties: “jdbcUrl”, “dataSource”]) at [Source: {“user-provided”:[{ “credentials”: { “jdbcUrl”: “jdbc:oracle:thin:user/pass//host:port/service”, “spring.datasource.driver-class-name”: “oracle.jdbc.OracleDriver”, “spring.datasource.initialize”: “false” }, “syslog_drain_url”: “”, “volume_mounts”: [ ], “label”: “user-provided”, “name”: “Oracle”, “tags”: [ ] }]}; line: 1, column: 19] (through reference chain: rest.springframework.model.CupsProperties[“user-provided”])
Advertisement
Answer
Check below solution and see if it fulfills your need. You can build on to it if you need to parse more fields.
import java.util.Iterator; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class JsonParser { public static void main(String[] args) { String VCAP_Services = "{"userProvided": [{"credentials": {"accessTokenUri": "tokenurl","apiUrl": "apiurl","clientId": "typeofID","clientSecret": "secretAf","scope": "none"},"syslog_drain_url": "","volume_mounts": [],"label": "user-provided","name": "OAuth2","tags": []},{"credentials": {"jdbcUrl": "jdbc:oracle:connection[host]:[port]/service","spring.datasource.driver-class-name": "oracle.jdbc.OracleDriver","spring.datasource.initialize": "false"},"syslog_drain_url": "","volume_mounts": [],"label": "user-provided","name": "Database","tags": [] } ] } "; CupsProperties properties=null; try { JSONParser jsonParser = new JSONParser(); JSONObject vcapServiceJSONObject = (JSONObject) jsonParser.parse(VCAP_Services); for(Object key: vcapServiceJSONObject.keySet()){ String keyStr = (String) key; JSONArray userProvidedList = (JSONArray) vcapServiceJSONObject.get(keyStr); Iterator i = userProvidedList.iterator(); while (i.hasNext()) { JSONObject innerObj = (JSONObject) i.next(); JSONObject credentialsObject = (JSONObject) innerObj.get("credentials"); if(credentialsObject.containsKey("jdbcUrl")){ //set to your pojo objects System.out.println("JDBC url:" + credentialsObject.get("jdbcUrl")); } if(credentialsObject.containsKey("accessTokenUri")){ //set to your pojo objects System.out.println("Access token URI:" + credentialsObject.get("accessTokenUri")); } } } } catch (ParseException e) { e.printStackTrace(); } } }
Output
Access token URI:tokenurl JDBC url:jdbc:oracle:connection[host]:[port]/service