Skip to content

Commit 9360f1f

Browse files
committed
First commit
1 parent a43e5b5 commit 9360f1f

File tree

11 files changed

+207
-0
lines changed

11 files changed

+207
-0
lines changed

Diff for: .classpath

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="output" path="target/classes"/>
26+
</classpath>

Diff for: .project

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>CallAPI2</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>

Diff for: .settings/org.eclipse.core.resources.prefs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/test/java=UTF-8
4+
encoding/<project>=UTF-8

Diff for: .settings/org.eclipse.jdt.core.prefs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
3+
org.eclipse.jdt.core.compiler.compliance=1.5
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.5

Diff for: .settings/org.eclipse.m2e.core.prefs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Demo on how to call an API and read JSON in Java with JAX-RS (Jersey) and Jackson
2+
3+
This is a demo for ICSI 518 software engineering course offered in Fall 2017.
4+
5+
This is a plain Java object, it uses Maven to manage dependencies. Download and import it into Eclipse to run.
6+
7+
The main code is in CallAPI.java. It demonstrates how to use JAX-RS (Jersey) to call an RESTful API and get the JSON data. It then use Jackson to read properties from the JSON data. Feel free to copy code from here to your term project.

Diff for: pom.xml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>edu.albany.mingying</groupId>
6+
<artifactId>CallAPI2</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>CallAPI2</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.glassfish.jersey.core</groupId>
20+
<artifactId>jersey-client</artifactId>
21+
<version>2.26</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.glassfish.jersey.inject</groupId>
25+
<artifactId>jersey-hk2</artifactId>
26+
<version>2.26</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.core</groupId>
30+
<artifactId>jackson-databind</artifactId>
31+
<version>2.9.2</version>
32+
</dependency>
33+
</dependencies>
34+
</project>
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package edu.albany.mingying.CallAPI2;
2+
3+
import java.io.IOException;
4+
import java.util.Iterator;
5+
import javax.ws.rs.client.Client;
6+
import javax.ws.rs.client.ClientBuilder;
7+
import javax.ws.rs.client.WebTarget;
8+
import javax.ws.rs.core.MediaType;
9+
10+
import com.fasterxml.jackson.databind.JsonNode;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
13+
public class CallAPI {
14+
public static void main(String[] args) {
15+
System.out.println("Calling AlphaVantage API...");
16+
Client client= ClientBuilder.newClient();
17+
18+
// Core settings are here, put what ever API parameter you want to use
19+
WebTarget target= client.target("https://www.alphavantage.co/query")
20+
.queryParam("function", "TIME_SERIES_WEEKLY")
21+
.queryParam("symbol", "AAPL")
22+
.queryParam("apikey", "8LY5VJQYBZTYS262");
23+
// Actually calling API here, Use HTTP GET method
24+
// data is the response JSON string
25+
String data = target.request(MediaType.APPLICATION_JSON).get(String.class);
26+
27+
try {
28+
// Use Jackson to read the JSON into a tree like structure
29+
ObjectMapper mapper = new ObjectMapper();
30+
JsonNode root = mapper.readTree(data);
31+
32+
// Make sure the JSON is an object, as said in their documents
33+
assert root.isObject();
34+
// Read the "Meta Data" property of JSON object
35+
JsonNode metadata = root.get("Meta Data");
36+
assert metadata.isObject();
37+
// Read "2. Symbol" property of "Meta Data" property
38+
if (metadata.get("2. Symbol").isValueNode()) {
39+
System.out.println(metadata.get("2. Symbol").asText());
40+
}
41+
// Print "4. Time Zone" property of "Meta Data" property of root JSON object
42+
System.out.println(root.at("/Meta Data/4. Time Zone").asText());
43+
// Read "Weekly Time Series" property of root JSON object
44+
Iterator<String> dates = root.get("Weekly Time Series").fieldNames();
45+
while(dates.hasNext()) {
46+
// Read the first date's open price
47+
String n = root.at("/Weekly Time Series/" + dates.next() + "/1. open").asText();
48+
System.out.println(Double.parseDouble(n));
49+
// remove break if you wan't to print all the open prices.
50+
break;
51+
}
52+
} catch (IOException e) {
53+
// TODO Auto-generated catch block
54+
e.printStackTrace();
55+
}
56+
}
57+
58+
}

Diff for: target/classes/META-INF/MANIFEST.MF

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Manifest-Version: 1.0
2+
Built-By: james
3+
Build-Jdk: 1.8.0_102
4+
Created-By: Maven Integration for Eclipse
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#Generated by Maven Integration for Eclipse
2+
#Mon Nov 20 13:07:25 EST 2017
3+
version=0.0.1-SNAPSHOT
4+
groupId=edu.albany.mingying
5+
m2e.projectName=CallAPI2
6+
m2e.projectLocation=/Users/james/Library/Mobile Documents/com~apple~CloudDocs/Fall 2017/SoftwareEngineering/Projects/CallAPI2
7+
artifactId=CallAPI2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>edu.albany.mingying</groupId>
6+
<artifactId>CallAPI2</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>CallAPI2</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.glassfish.jersey.core</groupId>
20+
<artifactId>jersey-client</artifactId>
21+
<version>2.26</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.glassfish.jersey.inject</groupId>
25+
<artifactId>jersey-hk2</artifactId>
26+
<version>2.26</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.core</groupId>
30+
<artifactId>jackson-databind</artifactId>
31+
<version>2.9.2</version>
32+
</dependency>
33+
</dependencies>
34+
</project>

0 commit comments

Comments
 (0)