From db6811406c1c8474d06798b1947729d7b45443e0 Mon Sep 17 00:00:00 2001 From: juan Date: Sun, 8 Oct 2017 23:59:55 -0300 Subject: [PATCH 01/73] Minor format changes. --- .../java/com/baeldung/jsonb/JsonbTest.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/spring-5/src/test/java/com/baeldung/jsonb/JsonbTest.java b/spring-5/src/test/java/com/baeldung/jsonb/JsonbTest.java index 4caab86f7d..a65e171e6e 100644 --- a/spring-5/src/test/java/com/baeldung/jsonb/JsonbTest.java +++ b/spring-5/src/test/java/com/baeldung/jsonb/JsonbTest.java @@ -24,13 +24,28 @@ public class JsonbTest { public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() { Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000)); String jsonPerson = jsonb.toJson(person); - assertTrue("{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}".equals(jsonPerson)); + //// @formatter:off + String jsonExpected = + "{\"email\":\"jhon@test.com\"," + + "\"id\":1," + + "\"person-name\":\"Jhon\"," + + "\"registeredDate\":\"07-09-2019\"," + + "\"salary\":\"1000.0\"}"; + // @formatter:on + assertTrue(jsonExpected.equals(jsonPerson)); } @Test public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() { Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0)); - String jsonPerson = "{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}"; + // @formatter:off + String jsonPerson = + "{\"email\":\"jhon@test.com\"," + + "\"id\":1," + + "\"person-name\":\"Jhon\"," + + "\"registeredDate\":\"07-09-2019\"," + + "\"salary\":\"1000.0\"}"; + // @formatter:on assertTrue(jsonb.fromJson(jsonPerson, Person.class) .equals(person)); } From d9368899219b8d67c9cb3cc68057eb4be469253e Mon Sep 17 00:00:00 2001 From: hgomez Date: Sat, 4 Nov 2017 17:43:04 -0400 Subject: [PATCH 02/73] first version --- libraries/pom.xml | 15 ++++ .../DailyMotionExample.java | 74 +++++++++++++++++++ .../googlehttpclientguide/DailyMotionUrl.java | 22 ++++++ .../baeldung/googlehttpclientguide/Video.java | 23 ++++++ .../googlehttpclientguide/VideoFeed.java | 20 +++++ .../googlehttpclientguide/logging.properties | 10 +++ 6 files changed, 164 insertions(+) create mode 100644 libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionExample.java create mode 100644 libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionUrl.java create mode 100644 libraries/src/main/java/com/baeldung/googlehttpclientguide/Video.java create mode 100644 libraries/src/main/java/com/baeldung/googlehttpclientguide/VideoFeed.java create mode 100644 libraries/src/main/java/com/baeldung/googlehttpclientguide/logging.properties diff --git a/libraries/pom.xml b/libraries/pom.xml index 25c1113fea..9384f58493 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -622,6 +622,21 @@ bcpkix-jdk15on 1.58 + + com.google.http-client + google-http-client + 1.23.0 + + + com.google.http-client + google-http-client-jackson2 + 1.23.0 + + + com.google.http-client + google-http-client-gson + 1.23.0 + diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionExample.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionExample.java new file mode 100644 index 0000000000..dd559f3aa2 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionExample.java @@ -0,0 +1,74 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.baeldung.googlehttpclientguide; + +import com.google.api.client.http.HttpRequest; +import com.google.api.client.http.HttpRequestFactory; +import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.http.HttpResponseException; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.JsonObjectParser; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; + +/** + * + * @author Hugo + */ +public class DailyMotionExample { + + static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); + static final JsonFactory JSON_FACTORY = new JacksonFactory(); + //static final JsonFactory JSON_FACTORY = new GsonFactory(); + + private static void run() throws Exception { + HttpRequestFactory requestFactory + = HTTP_TRANSPORT.createRequestFactory( + new HttpRequestInitializer() { + + public void initialize(HttpRequest request) { + request.setParser(new JsonObjectParser(JSON_FACTORY)); + } + }); + DailyMotionUrl url = new DailyMotionUrl("https://api.dailymotion.com/videos/"); + url.fields = "id,tags,title,url"; + HttpRequest request = requestFactory.buildGetRequest(url); + VideoFeed videoFeed = request.execute().parseAs(VideoFeed.class); + if (videoFeed.list.isEmpty()) { + System.out.println("No videos found."); + } else { + if (videoFeed.hasMore) { + System.out.print("First "); + } + System.out.println(videoFeed.list.size() + " videos found:"); + for (Video video : videoFeed.list) { + System.out.println(); + System.out.println("-----------------------------------------------"); + System.out.println("ID: " + video.id); + System.out.println("Title: " + video.title); + System.out.println("Tags: " + video.tags); + System.out.println("URL: " + video.url); + } + } + } + + public static void main(String[] args) { + try { + try { + run(); + return; + } catch (HttpResponseException e) { + System.err.println(e.getMessage()); + } + } catch (Throwable t) { + t.printStackTrace(); + } + System.exit(1); + } + +} diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionUrl.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionUrl.java new file mode 100644 index 0000000000..0f77cdd2f6 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/DailyMotionUrl.java @@ -0,0 +1,22 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.baeldung.googlehttpclientguide; + +import com.google.api.client.http.GenericUrl; +import com.google.api.client.util.Key; + +/** + * + * @author Hugo + */ + public class DailyMotionUrl extends GenericUrl { + + public DailyMotionUrl(String encodedUrl) { + super(encodedUrl); + } + + @Key public String fields; + } diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/Video.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/Video.java new file mode 100644 index 0000000000..62622deaf5 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/Video.java @@ -0,0 +1,23 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.baeldung.googlehttpclientguide; + +import com.google.api.client.util.Key; +import java.util.List; + +/** + * + * @author Hugo + */ + public class Video { + @Key public String id; + + @Key public List tags; + + @Key public String title; + + @Key public String url; + } \ No newline at end of file diff --git a/libraries/src/main/java/com/baeldung/googlehttpclientguide/VideoFeed.java b/libraries/src/main/java/com/baeldung/googlehttpclientguide/VideoFeed.java new file mode 100644 index 0000000000..6b62f61924 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/googlehttpclientguide/VideoFeed.java @@ -0,0 +1,20 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.baeldung.googlehttpclientguide; + +import com.google.api.client.util.Key; +import java.util.List; + +/** + * + * @author Hugo + */ + public class VideoFeed { + @Key public List