BAEL-5657 - Reading JSON from a URL in Java (#12584)
* moving SpringBootPersistenceApplication class to its own package * from com.baeldung to com.baeldung.logging to prevent it from loading contexts from other applications. * moving SpringBootPersistenceApplication class to its own package * from com.baeldung to com.baeldung.logging to prevent it from loading contexts from other applications. * Spring Data MongoDB - Configure Connection Ready for revision. * fixed tests to reflect article changes * BAEL-5657 * reverting BAEL-5657 * reverting BAEL-5657 * done * reverting bael-5366 * revert bael-5366 * revert bael-3366 * including section 3 3. Using commons-io and org.json * replacing tabs with spaces
This commit is contained in:
parent
7050d0a1e8
commit
941d6701f3
|
@ -32,6 +32,18 @@
|
|||
<artifactId>jackson-dataformat-csv</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mock-server</groupId>
|
||||
<artifactId>mockserver-netty</artifactId>
|
||||
<version>${mockserver-netty.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>${json.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -44,4 +56,9 @@
|
|||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<mockserver-netty.version>5.13.2</mockserver-netty.version>
|
||||
<json.version>20220320</json.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.jackson.jsonurlreader;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.fasterxml.jackson.core.exc.StreamReadException;
|
||||
import com.fasterxml.jackson.databind.DatabindException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class JsonUrlReader {
|
||||
|
||||
public static void main(String[] args) throws StreamReadException, DatabindException, MalformedURLException, IOException {
|
||||
String url = args[0];
|
||||
|
||||
JsonNode node = JsonUrlReader.get(url);
|
||||
System.out.println(node.toPrettyString());
|
||||
}
|
||||
|
||||
public static String stream(String url) throws IOException {
|
||||
try (InputStream input = new URL(url).openStream()) {
|
||||
InputStreamReader isr = new InputStreamReader(input, Charset.forName("UTF-8"));
|
||||
BufferedReader reader = new BufferedReader(isr);
|
||||
StringBuilder json = new StringBuilder();
|
||||
int c;
|
||||
while ((c = reader.read()) != -1) {
|
||||
json.append((char) c);
|
||||
}
|
||||
return json.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonNode get(String url) throws StreamReadException, DatabindException, MalformedURLException, IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode json = mapper.readTree(new URL(url));
|
||||
return json;
|
||||
}
|
||||
|
||||
public static <T> T get(String url, Class<T> type) throws StreamReadException, DatabindException, MalformedURLException, IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
T entity = mapper.readValue(new URL(url), type);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public static String getString(String url) throws StreamReadException, DatabindException, MalformedURLException, IOException {
|
||||
return get(url).toPrettyString();
|
||||
}
|
||||
|
||||
public static JSONObject getJson(String url) throws MalformedURLException, IOException {
|
||||
String json = IOUtils.toString(new URL(url), Charset.forName("UTF-8"));
|
||||
JSONObject object = new JSONObject(json);
|
||||
return object;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.jackson.jsonurlreader.data;
|
||||
|
||||
public class Example {
|
||||
private String name;
|
||||
private Integer n;
|
||||
private Boolean real;
|
||||
|
||||
public Example() {
|
||||
}
|
||||
|
||||
public Example(String name, Integer n, Boolean real) {
|
||||
this.name = name;
|
||||
this.n = n;
|
||||
this.real = real;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getN() {
|
||||
return n;
|
||||
}
|
||||
|
||||
public void setN(Integer n) {
|
||||
this.n = n;
|
||||
}
|
||||
|
||||
public Boolean getReal() {
|
||||
return real;
|
||||
}
|
||||
|
||||
public void setReal(Boolean real) {
|
||||
this.real = real;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.jackson.jsonurlreader;
|
||||
|
||||
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
|
||||
import static org.mockserver.model.HttpRequest.request;
|
||||
import static org.mockserver.model.HttpResponse.response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.mockserver.client.MockServerClient;
|
||||
import org.mockserver.integration.ClientAndServer;
|
||||
import org.mockserver.model.HttpStatusCode;
|
||||
|
||||
import com.baeldung.jackson.jsonurlreader.data.Example;
|
||||
|
||||
public class JsonMockServer {
|
||||
protected static final String JSON_RESPONSE = "{ \"name\": \"A\", \"n\": 1, \"real\": true }";
|
||||
protected static final Example OBJECT_RESPONSE = new Example("A", 1, true);
|
||||
|
||||
protected static String serviceUrl;
|
||||
|
||||
private static final String SERVER_ADDRESS = "127.0.0.1";
|
||||
private static final String PATH = "/sample-json";
|
||||
|
||||
private static ClientAndServer mockServer;
|
||||
private static int serverPort;
|
||||
|
||||
@BeforeAll
|
||||
static void startServer() throws IOException {
|
||||
serverPort = getFreePort();
|
||||
serviceUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH;
|
||||
|
||||
mockServer = startClientAndServer(serverPort);
|
||||
|
||||
mockJsonRequest();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void stopServer() {
|
||||
mockServer.stop();
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
private static void mockJsonRequest() {
|
||||
new MockServerClient(SERVER_ADDRESS, serverPort)
|
||||
.when(request()
|
||||
.withPath(PATH)
|
||||
.withMethod("GET"))
|
||||
.respond(response()
|
||||
.withStatusCode(HttpStatusCode.OK_200.code())
|
||||
.withBody(JSON_RESPONSE)
|
||||
);
|
||||
}
|
||||
|
||||
private static int getFreePort() throws IOException {
|
||||
try (ServerSocket serverSocket = new ServerSocket(0)) {
|
||||
return serverSocket.getLocalPort();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.jackson.jsonurlreader;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.jackson.jsonurlreader.data.Example;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
public class JsonUrlReaderIntegrationTest extends JsonMockServer {
|
||||
|
||||
@Test
|
||||
public void whenStreamUrl_thenJsonStringReturned() throws Exception {
|
||||
String jsonNode = JsonUrlReader.stream(serviceUrl);
|
||||
|
||||
assertEquals(jsonNode, JSON_RESPONSE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetUrl_thenJsonNodeReturned() throws Exception {
|
||||
JsonNode jsonNode = JsonUrlReader.get(serviceUrl);
|
||||
|
||||
assertEquals(jsonNode.get("name")
|
||||
.textValue(), "A");
|
||||
assertEquals(jsonNode.get("n")
|
||||
.intValue(), 1);
|
||||
assertEquals(jsonNode.get("real")
|
||||
.booleanValue(), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenType_whenGetUrl_thenTypeReturned() throws Exception {
|
||||
Example object = JsonUrlReader.get(serviceUrl, Example.class);
|
||||
|
||||
Integer n = 1;
|
||||
assertEquals(object.getName(), "A");
|
||||
assertEquals(object.getN(), n);
|
||||
assertEquals(object.getReal(), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetJsonUrl_thenJsonObjectReturned() throws Exception {
|
||||
JSONObject jsonObject = JsonUrlReader.getJson(serviceUrl);
|
||||
|
||||
assertEquals(jsonObject.getString("name"), "A");
|
||||
assertEquals(jsonObject.getInt("n"), 1);
|
||||
assertEquals(jsonObject.getBoolean("real"), true);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue