From 9c7debefa4fdcdb9daa6cd8e32c4123c5b2d780a Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Tue, 19 Jul 2016 14:25:36 +0700 Subject: [PATCH 01/16] Changes configuration from XML to Java for the cxf-spring module --- apache-cxf/cxf-spring/.gitignore | 1 + apache-cxf/cxf-spring/pom.xml | 9 ++++++-- .../baeldung/cxf/spring/AppInitializer.java | 21 +++++++++++++++++ .../cxf/spring/ClientConfiguration.java | 21 +++++++++++++++++ .../cxf/spring/ServiceConfiguration.java | 23 +++++++++++++++++++ .../src/main/resources/client-beans.xml | 10 -------- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 7 ------ .../src/main/webapp/WEB-INF/web.xml | 14 ----------- .../com/baeldung/cxf/spring/StudentTest.java | 11 ++++----- 9 files changed, 77 insertions(+), 40 deletions(-) create mode 100644 apache-cxf/cxf-spring/.gitignore create mode 100644 apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/AppInitializer.java create mode 100644 apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ClientConfiguration.java create mode 100644 apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java delete mode 100644 apache-cxf/cxf-spring/src/main/resources/client-beans.xml delete mode 100644 apache-cxf/cxf-spring/src/main/webapp/WEB-INF/cxf-servlet.xml delete mode 100644 apache-cxf/cxf-spring/src/main/webapp/WEB-INF/web.xml diff --git a/apache-cxf/cxf-spring/.gitignore b/apache-cxf/cxf-spring/.gitignore new file mode 100644 index 0000000000..2f7896d1d1 --- /dev/null +++ b/apache-cxf/cxf-spring/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/apache-cxf/cxf-spring/pom.xml b/apache-cxf/cxf-spring/pom.xml index 431c35c51d..aa72edb739 100644 --- a/apache-cxf/cxf-spring/pom.xml +++ b/apache-cxf/cxf-spring/pom.xml @@ -19,7 +19,7 @@ maven-war-plugin 2.6 - src/main/webapp/WEB-INF/web.xml + false @@ -110,8 +110,13 @@ org.springframework - spring-web + spring-webmvc ${spring.version} + + javax.servlet + javax.servlet-api + 3.1.0 + diff --git a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/AppInitializer.java b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/AppInitializer.java new file mode 100644 index 0000000000..036bf66a52 --- /dev/null +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/AppInitializer.java @@ -0,0 +1,21 @@ +package com.baeldung.cxf.spring; + +import javax.servlet.ServletContext; +import javax.servlet.ServletRegistration; + +import org.apache.cxf.transport.servlet.CXFServlet; +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.ContextLoaderListener; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + +public class AppInitializer implements WebApplicationInitializer { + @Override + public void onStartup(ServletContext container) { + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.register(ServiceConfiguration.class); + container.addListener(new ContextLoaderListener(context)); + + ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new CXFServlet()); + dispatcher.addMapping("/services"); + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ClientConfiguration.java b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ClientConfiguration.java new file mode 100644 index 0000000000..a2f124705b --- /dev/null +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ClientConfiguration.java @@ -0,0 +1,21 @@ +package com.baeldung.cxf.spring; + +import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ClientConfiguration { + @Bean(name = "client") + public Object generateProxy() { + return proxyFactoryBean().create(); + } + + @Bean + public JaxWsProxyFactoryBean proxyFactoryBean() { + JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean(); + proxyFactory.setServiceClass(Baeldung.class); + proxyFactory.setAddress("http://localhost:8080/services/baeldung"); + return proxyFactory; + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java new file mode 100644 index 0000000000..0d150532bf --- /dev/null +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java @@ -0,0 +1,23 @@ +package com.baeldung.cxf.spring; + +import javax.xml.ws.Endpoint; + +import org.apache.cxf.bus.spring.SpringBus; +import org.apache.cxf.jaxws.EndpointImpl; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ServiceConfiguration { + @Bean + public SpringBus springBus() { + return new SpringBus(); + } + + @Bean + public Endpoint endpoint() { + EndpointImpl endpoint = new EndpointImpl(springBus(), new BaeldungImpl()); + endpoint.publish("http://localhost:8080/services/baeldung"); + return endpoint; + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-spring/src/main/resources/client-beans.xml b/apache-cxf/cxf-spring/src/main/resources/client-beans.xml deleted file mode 100644 index 626252b565..0000000000 --- a/apache-cxf/cxf-spring/src/main/resources/client-beans.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - diff --git a/apache-cxf/cxf-spring/src/main/webapp/WEB-INF/cxf-servlet.xml b/apache-cxf/cxf-spring/src/main/webapp/WEB-INF/cxf-servlet.xml deleted file mode 100644 index 9fe87ba9ee..0000000000 --- a/apache-cxf/cxf-spring/src/main/webapp/WEB-INF/cxf-servlet.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - diff --git a/apache-cxf/cxf-spring/src/main/webapp/WEB-INF/web.xml b/apache-cxf/cxf-spring/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 43a03b1d8b..0000000000 --- a/apache-cxf/cxf-spring/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - cxf - org.apache.cxf.transport.servlet.CXFServlet - 1 - - - cxf - /services/* - - diff --git a/apache-cxf/cxf-spring/src/test/java/com/baeldung/cxf/spring/StudentTest.java b/apache-cxf/cxf-spring/src/test/java/com/baeldung/cxf/spring/StudentTest.java index 44a9d11687..794275b4d6 100644 --- a/apache-cxf/cxf-spring/src/test/java/com/baeldung/cxf/spring/StudentTest.java +++ b/apache-cxf/cxf-spring/src/test/java/com/baeldung/cxf/spring/StudentTest.java @@ -3,15 +3,12 @@ package com.baeldung.cxf.spring; import static org.junit.Assert.assertEquals; import org.junit.Test; -import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class StudentTest { - private ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "client-beans.xml" }); - private Baeldung baeldungProxy; - - { - baeldungProxy = (Baeldung) context.getBean("client"); - } + private ApplicationContext context = new AnnotationConfigApplicationContext(ClientConfiguration.class); + private Baeldung baeldungProxy = (Baeldung) context.getBean("client"); @Test public void whenUsingHelloMethod_thenCorrect() { From 864839a8415e03fd02d6f35792c2e3646090fbd3 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Thu, 21 Jul 2016 09:20:48 +0700 Subject: [PATCH 02/16] Changes the name of the SpringBus bean --- .../java/com/baeldung/cxf/spring/ServiceConfiguration.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java index 0d150532bf..f1fac579ab 100644 --- a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java @@ -2,6 +2,7 @@ package com.baeldung.cxf.spring; import javax.xml.ws.Endpoint; +import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.context.annotation.Bean; @@ -9,10 +10,10 @@ import org.springframework.context.annotation.Configuration; @Configuration public class ServiceConfiguration { - @Bean + @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); - } + } @Bean public Endpoint endpoint() { From 551ee2017c5db5ebf8243a8833352a9bf8fc6d97 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 24 Jul 2016 12:53:50 +0300 Subject: [PATCH 03/16] fixes for the build --- dependency-injection/pom.xml | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/dependency-injection/pom.xml b/dependency-injection/pom.xml index 667ea87402..9e78a66ad6 100644 --- a/dependency-injection/pom.xml +++ b/dependency-injection/pom.xml @@ -1,6 +1,5 @@ - + 4.0.0 @@ -9,7 +8,7 @@ 0.0.1-SNAPSHOT war - Resource vs Inject vs Autowired + dependency-injection Accompanying the demonstration of the use of the annotations related to injection mechanisms, namely Resource, Inject, and Autowired @@ -54,6 +53,7 @@ + maven-compiler-plugin @@ -71,13 +71,29 @@ + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + + + 2.6 + + java.net https://maven.java.net/content/repositories/releases/ + From f79715e6b21213f82d694744c0d17de47dd36c38 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 24 Jul 2016 13:01:24 +0300 Subject: [PATCH 04/16] minor cleanup --- .../.settings/org.eclipse.wst.common.project.facet.core.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml b/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml index 9ca0d1c1b7..b1c99d7726 100644 --- a/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -3,4 +3,5 @@ + From dd99e54f3be61e5ef81d2288cfa03518149bbd62 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 24 Jul 2016 17:28:41 +0300 Subject: [PATCH 05/16] renaming a module --- {rest-assured-tutorial => rest-assured}/.gitignore | 0 {rest-assured-tutorial => rest-assured}/README.md | 0 {rest-assured-tutorial => rest-assured}/pom.xml | 0 .../src/test/java/com/baeldung/restassured/RestAssured2Test.java | 0 .../src/test/java/com/baeldung/restassured/RestAssuredTest.java | 0 .../test/java/com/baeldung/restassured/RestAssuredXML2Test.java | 0 .../test/java/com/baeldung/restassured/RestAssuredXMLTest.java | 0 .../src/test/java/com/baeldung/restassured/Util.java | 0 .../src/test/resources/employees.xml | 0 .../src/test/resources/event_0.json | 0 .../src/test/resources/log4j.properties | 0 .../src/test/resources/odds.json | 0 .../src/test/resources/teachers.xml | 0 13 files changed, 0 insertions(+), 0 deletions(-) rename {rest-assured-tutorial => rest-assured}/.gitignore (100%) rename {rest-assured-tutorial => rest-assured}/README.md (100%) rename {rest-assured-tutorial => rest-assured}/pom.xml (100%) rename {rest-assured-tutorial => rest-assured}/src/test/java/com/baeldung/restassured/RestAssured2Test.java (100%) rename {rest-assured-tutorial => rest-assured}/src/test/java/com/baeldung/restassured/RestAssuredTest.java (100%) rename {rest-assured-tutorial => rest-assured}/src/test/java/com/baeldung/restassured/RestAssuredXML2Test.java (100%) rename {rest-assured-tutorial => rest-assured}/src/test/java/com/baeldung/restassured/RestAssuredXMLTest.java (100%) rename {rest-assured-tutorial => rest-assured}/src/test/java/com/baeldung/restassured/Util.java (100%) rename {rest-assured-tutorial => rest-assured}/src/test/resources/employees.xml (100%) rename {rest-assured-tutorial => rest-assured}/src/test/resources/event_0.json (100%) rename {rest-assured-tutorial => rest-assured}/src/test/resources/log4j.properties (100%) rename {rest-assured-tutorial => rest-assured}/src/test/resources/odds.json (100%) rename {rest-assured-tutorial => rest-assured}/src/test/resources/teachers.xml (100%) diff --git a/rest-assured-tutorial/.gitignore b/rest-assured/.gitignore similarity index 100% rename from rest-assured-tutorial/.gitignore rename to rest-assured/.gitignore diff --git a/rest-assured-tutorial/README.md b/rest-assured/README.md similarity index 100% rename from rest-assured-tutorial/README.md rename to rest-assured/README.md diff --git a/rest-assured-tutorial/pom.xml b/rest-assured/pom.xml similarity index 100% rename from rest-assured-tutorial/pom.xml rename to rest-assured/pom.xml diff --git a/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssured2Test.java b/rest-assured/src/test/java/com/baeldung/restassured/RestAssured2Test.java similarity index 100% rename from rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssured2Test.java rename to rest-assured/src/test/java/com/baeldung/restassured/RestAssured2Test.java diff --git a/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredTest.java b/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredTest.java similarity index 100% rename from rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredTest.java rename to rest-assured/src/test/java/com/baeldung/restassured/RestAssuredTest.java diff --git a/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredXML2Test.java b/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXML2Test.java similarity index 100% rename from rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredXML2Test.java rename to rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXML2Test.java diff --git a/rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredXMLTest.java b/rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXMLTest.java similarity index 100% rename from rest-assured-tutorial/src/test/java/com/baeldung/restassured/RestAssuredXMLTest.java rename to rest-assured/src/test/java/com/baeldung/restassured/RestAssuredXMLTest.java diff --git a/rest-assured-tutorial/src/test/java/com/baeldung/restassured/Util.java b/rest-assured/src/test/java/com/baeldung/restassured/Util.java similarity index 100% rename from rest-assured-tutorial/src/test/java/com/baeldung/restassured/Util.java rename to rest-assured/src/test/java/com/baeldung/restassured/Util.java diff --git a/rest-assured-tutorial/src/test/resources/employees.xml b/rest-assured/src/test/resources/employees.xml similarity index 100% rename from rest-assured-tutorial/src/test/resources/employees.xml rename to rest-assured/src/test/resources/employees.xml diff --git a/rest-assured-tutorial/src/test/resources/event_0.json b/rest-assured/src/test/resources/event_0.json similarity index 100% rename from rest-assured-tutorial/src/test/resources/event_0.json rename to rest-assured/src/test/resources/event_0.json diff --git a/rest-assured-tutorial/src/test/resources/log4j.properties b/rest-assured/src/test/resources/log4j.properties similarity index 100% rename from rest-assured-tutorial/src/test/resources/log4j.properties rename to rest-assured/src/test/resources/log4j.properties diff --git a/rest-assured-tutorial/src/test/resources/odds.json b/rest-assured/src/test/resources/odds.json similarity index 100% rename from rest-assured-tutorial/src/test/resources/odds.json rename to rest-assured/src/test/resources/odds.json diff --git a/rest-assured-tutorial/src/test/resources/teachers.xml b/rest-assured/src/test/resources/teachers.xml similarity index 100% rename from rest-assured-tutorial/src/test/resources/teachers.xml rename to rest-assured/src/test/resources/teachers.xml From 0a2e672be1d584e4dfab06b49590801484012e6a Mon Sep 17 00:00:00 2001 From: GuenHamza Date: Sun, 24 Jul 2016 15:47:04 +0100 Subject: [PATCH 06/16] Add fast-json module --- fast-json/pom.xml | 30 ++++++ .../java/com/baeldung/fast_json/Person.java | 70 ++++++++++++ .../com/baeldung/fast_json/FastJsonTests.java | 100 ++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 fast-json/pom.xml create mode 100644 fast-json/src/main/java/com/baeldung/fast_json/Person.java create mode 100644 fast-json/src/test/java/com/baeldung/fast_json/FastJsonTests.java diff --git a/fast-json/pom.xml b/fast-json/pom.xml new file mode 100644 index 0000000000..b7627e4135 --- /dev/null +++ b/fast-json/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + + com.baeldung + fast-json + 0.0.1-SNAPSHOT + jar + + fast-json + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.12 + test + + + com.alibaba + fastjson + 1.2.13 + + + diff --git a/fast-json/src/main/java/com/baeldung/fast_json/Person.java b/fast-json/src/main/java/com/baeldung/fast_json/Person.java new file mode 100644 index 0000000000..51dc690355 --- /dev/null +++ b/fast-json/src/main/java/com/baeldung/fast_json/Person.java @@ -0,0 +1,70 @@ +package com.baeldung.fast_json; + +import java.util.Date; + +import com.alibaba.fastjson.annotation.JSONField; + +public class Person { + + @JSONField(name = "AGE", serialize = false, deserialize = false) + private int age; + + @JSONField(name = "LAST NAME", ordinal = 2) + private String lastName; + + @JSONField(name = "FIRST NAME", ordinal = 1) + private String firstName; + + @JSONField(name = "DATE OF BIRTH", format = "dd/MM/yyyy", ordinal = 3) + private Date dateOfBirth; + + public Person() { + + } + + public Person(int age, String lastName, String firstName, Date dateOfBirth) { + super(); + this.age = age; + this.lastName = lastName; + this.firstName = firstName; + this.dateOfBirth = dateOfBirth; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public Date getDateOfBirth() { + return dateOfBirth; + } + + public void setDateOfBirth(Date dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } + + @Override + public String toString() { + return "Person [age=" + age + ", lastName=" + lastName + ", firstName=" + + firstName + ", dateOfBirth=" + dateOfBirth + "]"; + } +} diff --git a/fast-json/src/test/java/com/baeldung/fast_json/FastJsonTests.java b/fast-json/src/test/java/com/baeldung/fast_json/FastJsonTests.java new file mode 100644 index 0000000000..9e6771e98b --- /dev/null +++ b/fast-json/src/test/java/com/baeldung/fast_json/FastJsonTests.java @@ -0,0 +1,100 @@ +package com.baeldung.fast_json; + +import static org.junit.Assert.*; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.serializer.BeanContext; +import com.alibaba.fastjson.serializer.ContextValueFilter; +import com.alibaba.fastjson.serializer.NameFilter; +import com.alibaba.fastjson.serializer.SerializeConfig; + +public class FastJsonTests { + private List listOfPersons = new ArrayList(); + + @Before + public void setUp() { + listOfPersons.add(new Person(15, "John", "Doe", new Date())); + listOfPersons.add(new Person(20, "Janette", "Doe", new Date())); + } + + @Test + public void convertObjectToJsonTest() { + String personJsonFormat = JSON.toJSONString(listOfPersons); + assertEquals( + personJsonFormat, + "[{\"FIRST NAME\":\"Doe\",\"LAST NAME\":\"John\",\"DATE OF BIRTH\":" + + "\"24/07/2016\"},{\"FIRST NAME\":\"Doe\",\"LAST NAME\":\"Janette\",\"DATE OF BIRTH\":" + + "\"24/07/2016\"}]"); + } + + @Test + public void convertJsonFormatToObject() { + String personJsonFormat = JSON.toJSONString(listOfPersons.get(0)); + Person newPerson = JSON.parseObject(personJsonFormat, Person.class); + assertEquals(newPerson.getAge(), 0); // serialize is set to false for age attribute + assertEquals(newPerson.getFirstName(), listOfPersons.get(0).getFirstName()); + assertEquals(newPerson.getLastName(), listOfPersons.get(0).getLastName()); + } + + @Test + public void createJsonObjectTest() throws ParseException { + JSONArray jsonArray = new JSONArray(); + for (int i = 0; i < 2; i++) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("FIRST NAME", "John" + i); + jsonObject.put("LAST NAME", "Doe" + i); + jsonObject.put("DATE OF BIRTH", "2016/12/12 12:12:12"); + jsonArray.add(jsonObject); + } + assertEquals( + jsonArray.toString(), + "[{\"LAST NAME\":\"Doe0\",\"DATE OF BIRTH\":" + + "\"2016/12/12 12:12:12\",\"FIRST NAME\":\"John0\"},{\"LAST NAME\":\"Doe1\"," + + "\"DATE OF BIRTH\":\"2016/12/12 12:12:12\",\"FIRST NAME\":\"John1\"}]"); + } + + @Test + public void convertObjectToJsonUsingContextFilterTest() { + ContextValueFilter valueFilter = new ContextValueFilter() { + public Object process(BeanContext context, Object object, + String name, Object value) { + if (name.equals("DATE OF BIRTH")) { + return "NOT TO DISCLOSE"; + } + if (value.equals("John") || value.equals("Doe")) { + return ((String) value).toUpperCase(); + } else { + return null; + } + } + }; + JSON.toJSONString(listOfPersons, valueFilter); + } + + @Test + public void convertObjectToJsonUsingSerializeConfig() { + NameFilter formatName = new NameFilter() { + public String process(Object object, String name, Object value) { + return name.toLowerCase().replace(" ", "_"); + } + }; + SerializeConfig.getGlobalInstance().addFilter(Person.class, formatName); + String jsonOutput = JSON.toJSONStringWithDateFormat(listOfPersons, + "yyyy-MM-dd"); + assertEquals( + jsonOutput, + "[{\"first_name\":\"Doe\",\"last_name\":\"John\"," + + "\"date_of_birth\":\"2016-07-24\"},{\"first_name\":\"Doe\",\"last_name\":" + + "\"Janette\",\"date_of_birth\":\"2016-07-24\"}]"); + } +} From b4d98aa751cc2dd8b125754e32dd6584179e93ba Mon Sep 17 00:00:00 2001 From: GuenHamza Date: Sun, 24 Jul 2016 15:48:07 +0100 Subject: [PATCH 07/16] add fast-json module to parent pom.xml --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 562aaf896b..68841212d4 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ javaxval jooq-spring json-path + fast-json mockito mocks jee7schedule From bacb7be6c2aa047da9dd2818372ee9f5a4fa3a78 Mon Sep 17 00:00:00 2001 From: GuenHamza Date: Sun, 24 Jul 2016 15:50:01 +0100 Subject: [PATCH 08/16] Create README.md --- fast-json/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 fast-json/README.md diff --git a/fast-json/README.md b/fast-json/README.md new file mode 100644 index 0000000000..3bffe81f61 --- /dev/null +++ b/fast-json/README.md @@ -0,0 +1,6 @@ +========= + +## Fast-Json + +### Relevant Articles: +- [A Guide to FastJson](http://www.baeldung.com/????????) From 2c6f98ff02c499979689ff489e0fd0b0785ef193 Mon Sep 17 00:00:00 2001 From: GuenHamza Date: Sun, 24 Jul 2016 15:51:24 +0100 Subject: [PATCH 09/16] Update Person.java Change methods order --- .../src/main/java/com/baeldung/fast_json/Person.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fast-json/src/main/java/com/baeldung/fast_json/Person.java b/fast-json/src/main/java/com/baeldung/fast_json/Person.java index 51dc690355..4600eaa712 100644 --- a/fast-json/src/main/java/com/baeldung/fast_json/Person.java +++ b/fast-json/src/main/java/com/baeldung/fast_json/Person.java @@ -30,6 +30,12 @@ public class Person { this.dateOfBirth = dateOfBirth; } + @Override + public String toString() { + return "Person [age=" + age + ", lastName=" + lastName + ", firstName=" + + firstName + ", dateOfBirth=" + dateOfBirth + "]"; + } + public int getAge() { return age; } @@ -61,10 +67,4 @@ public class Person { public void setDateOfBirth(Date dateOfBirth) { this.dateOfBirth = dateOfBirth; } - - @Override - public String toString() { - return "Person [age=" + age + ", lastName=" + lastName + ", firstName=" - + firstName + ", dateOfBirth=" + dateOfBirth + "]"; - } } From 32f0e2ed5d417ce6fe2c664737aacd9d1e067909 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 24 Jul 2016 18:37:52 +0300 Subject: [PATCH 10/16] minor cleanup work --- apache-cxf/cxf-spring/pom.xml | 175 +++++++++--------- .../com/baeldung/cxf/spring/Baeldung.java | 1 + .../cxf/spring/ClientConfiguration.java | 2 +- .../cxf/spring/ServiceConfiguration.java | 2 +- .../com/baeldung/cxf/spring/StudentTest.java | 2 +- 5 files changed, 95 insertions(+), 87 deletions(-) diff --git a/apache-cxf/cxf-spring/pom.xml b/apache-cxf/cxf-spring/pom.xml index aa72edb739..b9dbda7c11 100644 --- a/apache-cxf/cxf-spring/pom.xml +++ b/apache-cxf/cxf-spring/pom.xml @@ -8,90 +8,7 @@ apache-cxf 0.0.1-SNAPSHOT - - 3.1.6 - 4.3.1.RELEASE - 2.19.1 - - - - - maven-war-plugin - 2.6 - - false - - - - maven-surefire-plugin - ${surefire.version} - - - StudentTest.java - - - - - - - - integration - - - - org.codehaus.cargo - cargo-maven2-plugin - 1.5.0 - - - jetty9x - embedded - - - - localhost - 8080 - - - - - - start-server - pre-integration-test - - start - - - - stop-server - post-integration-test - - stop - - - - - - maven-surefire-plugin - ${surefire.version} - - - integration-test - - test - - - - none - - - - - - - - - + org.apache.cxf @@ -119,4 +36,94 @@ 3.1.0 + + + + + maven-war-plugin + 2.6 + + false + + + + maven-surefire-plugin + ${surefire.version} + + + StudentTest.java + + + + + + + + + integration + + + + org.codehaus.cargo + cargo-maven2-plugin + 1.5.0 + + + jetty9x + embedded + + + + localhost + 8081 + + + + + + start-server + pre-integration-test + + start + + + + stop-server + post-integration-test + + stop + + + + + + + maven-surefire-plugin + ${surefire.version} + + + integration-test + + test + + + + none + + + + + + + + + + + + + 3.1.6 + 4.3.1.RELEASE + 2.19.1 + + diff --git a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/Baeldung.java b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/Baeldung.java index 4f880e6ada..b66e4c2fbf 100644 --- a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/Baeldung.java +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/Baeldung.java @@ -5,5 +5,6 @@ import javax.jws.WebService; @WebService public interface Baeldung { String hello(String name); + String register(Student student); } \ No newline at end of file diff --git a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ClientConfiguration.java b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ClientConfiguration.java index a2f124705b..021bcc6f66 100644 --- a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ClientConfiguration.java +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ClientConfiguration.java @@ -15,7 +15,7 @@ public class ClientConfiguration { public JaxWsProxyFactoryBean proxyFactoryBean() { JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean(); proxyFactory.setServiceClass(Baeldung.class); - proxyFactory.setAddress("http://localhost:8080/services/baeldung"); + proxyFactory.setAddress("http://localhost:8081/services/baeldung"); return proxyFactory; } } \ No newline at end of file diff --git a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java index f1fac579ab..0bc60fb790 100644 --- a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/ServiceConfiguration.java @@ -18,7 +18,7 @@ public class ServiceConfiguration { @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), new BaeldungImpl()); - endpoint.publish("http://localhost:8080/services/baeldung"); + endpoint.publish("http://localhost:8081/services/baeldung"); return endpoint; } } \ No newline at end of file diff --git a/apache-cxf/cxf-spring/src/test/java/com/baeldung/cxf/spring/StudentTest.java b/apache-cxf/cxf-spring/src/test/java/com/baeldung/cxf/spring/StudentTest.java index 794275b4d6..7466944e04 100644 --- a/apache-cxf/cxf-spring/src/test/java/com/baeldung/cxf/spring/StudentTest.java +++ b/apache-cxf/cxf-spring/src/test/java/com/baeldung/cxf/spring/StudentTest.java @@ -22,7 +22,7 @@ public class StudentTest { Student student2 = new Student("Eve"); String student1Response = baeldungProxy.register(student1); String student2Response = baeldungProxy.register(student2); - + assertEquals("Adam is registered student number 1", student1Response); assertEquals("Eve is registered student number 2", student2Response); } From c551e4344406967b09d7dcad00eb008fb9eb6545 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 24 Jul 2016 18:42:08 +0300 Subject: [PATCH 11/16] renaming module --- pom.xml | 2 +- rest-assured/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 8d87555eb7..074f330bcf 100644 --- a/pom.xml +++ b/pom.xml @@ -45,7 +45,7 @@ querydsl - rest-assured-tutorial + rest-assured rest-testing resteasy log4j diff --git a/rest-assured/pom.xml b/rest-assured/pom.xml index 0d5a73d075..47241b18bd 100644 --- a/rest-assured/pom.xml +++ b/rest-assured/pom.xml @@ -2,7 +2,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 com.baeldung - rest-assured-tutorial + rest-assured 1.0 rest-assured From e4f8fc5c8ae62f7749caa1dc7bb792f69e3456a0 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sun, 24 Jul 2016 21:14:08 +0300 Subject: [PATCH 12/16] Refactor ITutorialsService --- .../baeldung/mvc/velocity/service/ITutorialsService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/service/ITutorialsService.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/service/ITutorialsService.java index 42d6149151..24059f2662 100644 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/service/ITutorialsService.java +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/service/ITutorialsService.java @@ -1,10 +1,10 @@ package com.baeldung.mvc.velocity.service; -import java.util.List; - import com.baeldung.mvc.velocity.domain.Tutorial; +import java.util.List; + public interface ITutorialsService { - public List listTutorials(); + List listTutorials(); } From 9da4eba141e45858d8702b870334b5862bd231a7 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Mon, 25 Jul 2016 15:52:54 +0700 Subject: [PATCH 13/16] Downgrade cargo and change jetty to tomcat --- apache-cxf/cxf-spring/pom.xml | 4 ++-- .../src/main/java/com/baeldung/cxf/spring/AppInitializer.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apache-cxf/cxf-spring/pom.xml b/apache-cxf/cxf-spring/pom.xml index aa72edb739..f3f8e916b5 100644 --- a/apache-cxf/cxf-spring/pom.xml +++ b/apache-cxf/cxf-spring/pom.xml @@ -41,10 +41,10 @@ org.codehaus.cargo cargo-maven2-plugin - 1.5.0 + 1.4.19 - jetty9x + tomcat8x embedded diff --git a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/AppInitializer.java b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/AppInitializer.java index 036bf66a52..a4faa0f287 100644 --- a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/AppInitializer.java +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/AppInitializer.java @@ -16,6 +16,6 @@ public class AppInitializer implements WebApplicationInitializer { container.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new CXFServlet()); - dispatcher.addMapping("/services"); + dispatcher.addMapping("/services/*"); } } \ No newline at end of file From d848933f207e9c602783e5fde23766e05dd5635d Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 25 Jul 2016 13:54:02 +0200 Subject: [PATCH 14/16] using StdSerializer --- .../jackson/bidirection/CustomListSerializer.java | 13 +++++++++++-- .../jackson/date/CustomDateSerializer.java | 13 +++++++++++-- .../jackson/date/CustomDateTimeSerializer.java | 14 ++++++++++++-- .../date/CustomLocalDateTimeSerializer.java | 14 ++++++++++++-- .../jackson/dtos/withEnum/TypeSerializer.java | 14 ++++++++++++-- .../jackson/serialization/ItemSerializer.java | 15 ++++++++++++--- .../serialization/ItemSerializerOnClass.java | 15 ++++++++++++--- .../serialization/MyDtoNullKeySerializer.java | 14 ++++++++++++-- 8 files changed, 94 insertions(+), 18 deletions(-) diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java index 1d8ca011ea..75e0a4ecb7 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListSerializer.java @@ -6,11 +6,20 @@ import java.util.List; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class CustomListSerializer extends JsonSerializer> { +public class CustomListSerializer extends StdSerializer> { + private static final long serialVersionUID = 3698763098000900856L; + + public CustomListSerializer() { + this(null); + } + + public CustomListSerializer(final Class> t) { + super(t); + } @Override public void serialize(final List items, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException { final List ids = new ArrayList(); diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java index 8d435b7b69..d840e1940f 100644 --- a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateSerializer.java @@ -6,13 +6,22 @@ import java.util.Date; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class CustomDateSerializer extends JsonSerializer { +public class CustomDateSerializer extends StdSerializer { + private static final long serialVersionUID = -2894356342227378312L; private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); + public CustomDateSerializer() { + this(null); + } + + public CustomDateSerializer(final Class t) { + super(t); + } + @Override public void serialize(final Date value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException { gen.writeString(formatter.format(value)); diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java index 88c069419b..ab4f4bbec7 100644 --- a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateTimeSerializer.java @@ -8,10 +8,20 @@ import org.joda.time.format.DateTimeFormatter; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class CustomDateTimeSerializer extends JsonSerializer { +public class CustomDateTimeSerializer extends StdSerializer { + + private static final long serialVersionUID = -3927232057990121460L; + + public CustomDateTimeSerializer() { + this(null); + } + + public CustomDateTimeSerializer(final Class t) { + super(t); + } private static DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm"); diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java index 3f8f5e098e..dbcf42488e 100644 --- a/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomLocalDateTimeSerializer.java @@ -6,13 +6,23 @@ import java.time.format.DateTimeFormatter; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class CustomLocalDateTimeSerializer extends JsonSerializer { +public class CustomLocalDateTimeSerializer extends StdSerializer { + + private static final long serialVersionUID = -7449444168934819290L; private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + public CustomLocalDateTimeSerializer() { + this(null); + } + + public CustomLocalDateTimeSerializer(final Class t) { + super(t); + } + @Override public void serialize(final LocalDateTime value, final JsonGenerator gen, final SerializerProvider arg2) throws IOException, JsonProcessingException { gen.writeString(formatter.format(value)); diff --git a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java index c5d5d7e0a8..fc5011137c 100644 --- a/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/dtos/withEnum/TypeSerializer.java @@ -4,10 +4,20 @@ import java.io.IOException; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class TypeSerializer extends JsonSerializer { +public class TypeSerializer extends StdSerializer { + + private static final long serialVersionUID = -7650668914169390772L; + + public TypeSerializer() { + this(null); + } + + public TypeSerializer(final Class t) { + super(t); + } @Override public void serialize(final TypeEnumWithCustomSerializer value, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException { diff --git a/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java index cb93f9cb03..b5624c566a 100644 --- a/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializer.java @@ -3,13 +3,22 @@ package com.baeldung.jackson.serialization; import java.io.IOException; import com.baeldung.jackson.dtos.Item; - import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class ItemSerializer extends JsonSerializer { +public class ItemSerializer extends StdSerializer { + + private static final long serialVersionUID = 6739170890621978901L; + + public ItemSerializer() { + this(null); + } + + public ItemSerializer(final Class t) { + super(t); + } @Override public final void serialize(final Item value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { diff --git a/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java index 79b450d7f1..1fdf44e17c 100644 --- a/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java +++ b/jackson/src/test/java/com/baeldung/jackson/serialization/ItemSerializerOnClass.java @@ -3,13 +3,22 @@ package com.baeldung.jackson.serialization; import java.io.IOException; import com.baeldung.jackson.dtos.ItemWithSerializer; - import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class ItemSerializerOnClass extends JsonSerializer { +public class ItemSerializerOnClass extends StdSerializer { + + private static final long serialVersionUID = -1760959597313610409L; + + public ItemSerializerOnClass() { + this(null); + } + + public ItemSerializerOnClass(final Class t) { + super(t); + } @Override public final void serialize(final ItemWithSerializer value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { diff --git a/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java b/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java index e915378498..d0b2d7f5e9 100644 --- a/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/serialization/MyDtoNullKeySerializer.java @@ -4,10 +4,20 @@ import java.io.IOException; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; -public class MyDtoNullKeySerializer extends JsonSerializer { +public class MyDtoNullKeySerializer extends StdSerializer { + + private static final long serialVersionUID = -4478531309177369056L; + + public MyDtoNullKeySerializer() { + this(null); + } + + public MyDtoNullKeySerializer(final Class t) { + super(t); + } @Override public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException { From cc8632120ca24d8cc837b21b5dccafa5009dbab9 Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 25 Jul 2016 13:54:18 +0200 Subject: [PATCH 15/16] using StdDeserializer --- .../bidirection/CustomListDeserializer.java | 14 ++++++++++++-- .../jackson/date/CustomDateDeserializer.java | 13 +++++++++++-- .../deserialization/ItemDeserializer.java | 17 +++++++++++++---- .../ItemDeserializerOnClass.java | 15 ++++++++++++--- .../try1/RestLoaderRequestDeserializer.java | 13 +++++++++++-- 5 files changed, 59 insertions(+), 13 deletions(-) diff --git a/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java b/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java index 5f1f1edf2b..c00316e365 100644 --- a/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/bidirection/CustomListDeserializer.java @@ -7,9 +7,19 @@ import java.util.List; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; -public class CustomListDeserializer extends JsonDeserializer> { +public class CustomListDeserializer extends StdDeserializer> { + + private static final long serialVersionUID = 1095767961632979804L; + + public CustomListDeserializer() { + this(null); + } + + public CustomListDeserializer(final Class vc) { + super(vc); + } @Override public List deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException { diff --git a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java index a63190c8f5..90c7d9fbac 100644 --- a/jackson/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/date/CustomDateDeserializer.java @@ -8,12 +8,21 @@ import java.util.Date; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; -public class CustomDateDeserializer extends JsonDeserializer { +public class CustomDateDeserializer extends StdDeserializer { + private static final long serialVersionUID = -5451717385630622729L; private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); + public CustomDateDeserializer() { + this(null); + } + + public CustomDateDeserializer(final Class vc) { + super(vc); + } + @Override public Date deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException { final String date = jsonparser.getText(); diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializer.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializer.java index 3be6685103..e9c89b8c78 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializer.java @@ -2,17 +2,26 @@ package com.baeldung.jackson.deserialization; import java.io.IOException; -import com.baeldung.jackson.dtos.User; import com.baeldung.jackson.dtos.Item; - +import com.baeldung.jackson.dtos.User; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import com.fasterxml.jackson.databind.node.IntNode; -public class ItemDeserializer extends JsonDeserializer { +public class ItemDeserializer extends StdDeserializer { + + private static final long serialVersionUID = 1883547683050039861L; + + public ItemDeserializer() { + this(null); + } + + public ItemDeserializer(final Class vc) { + super(vc); + } /** * {"id":1,"itemNr":"theItem","owner":2} diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java index 169a5c1c50..2036780e99 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/ItemDeserializerOnClass.java @@ -4,15 +4,24 @@ import java.io.IOException; import com.baeldung.jackson.dtos.ItemWithSerializer; import com.baeldung.jackson.dtos.User; - import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import com.fasterxml.jackson.databind.node.IntNode; -public class ItemDeserializerOnClass extends JsonDeserializer { +public class ItemDeserializerOnClass extends StdDeserializer { + + private static final long serialVersionUID = 5579141241817332594L; + + public ItemDeserializerOnClass() { + this(null); + } + + public ItemDeserializerOnClass(final Class vc) { + super(vc); + } /** * {"id":1,"itemNr":"theItem","owner":2} diff --git a/jackson/src/test/java/com/baeldung/jackson/try1/RestLoaderRequestDeserializer.java b/jackson/src/test/java/com/baeldung/jackson/try1/RestLoaderRequestDeserializer.java index 849607586d..529c05ddcc 100644 --- a/jackson/src/test/java/com/baeldung/jackson/try1/RestLoaderRequestDeserializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/try1/RestLoaderRequestDeserializer.java @@ -6,10 +6,19 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; -public class RestLoaderRequestDeserializer extends JsonDeserializer> { +public class RestLoaderRequestDeserializer extends StdDeserializer> { + private static final long serialVersionUID = -4245207329377196889L; + + public RestLoaderRequestDeserializer() { + this(null); + } + + public RestLoaderRequestDeserializer(final Class vc) { + super(vc); + } @Override public RestLoaderRequest deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { From daa3a5d944e1426e5f006ce4d83ab84cfe4f835a Mon Sep 17 00:00:00 2001 From: Slavisa Baeldung Date: Mon, 25 Jul 2016 14:31:25 +0200 Subject: [PATCH 16/16] BAEL-14 - renaming test, merging to json module, fixing failing tests inside the json module --- fast-json/README.md | 6 -- fast-json/pom.xml | 30 --------- json/README.md | 7 +++ json/pom.xml | 6 ++ .../baeldung/json/schema/JSONSchemaTest.java | 63 ++++++++++--------- .../test/java}/fast_json/FastJsonTests.java | 48 +++++++------- .../src/test/java}/fast_json/Person.java | 6 +- pom.xml | 2 +- 8 files changed, 75 insertions(+), 93 deletions(-) delete mode 100644 fast-json/README.md delete mode 100644 fast-json/pom.xml create mode 100644 json/README.md rename json/src/test/java/{org => com}/baeldung/json/schema/JSONSchemaTest.java (88%) rename {fast-json/src/test/java/com/baeldung => json/src/test/java}/fast_json/FastJsonTests.java (77%) rename {fast-json/src/main/java/com/baeldung => json/src/test/java}/fast_json/Person.java (97%) diff --git a/fast-json/README.md b/fast-json/README.md deleted file mode 100644 index 3bffe81f61..0000000000 --- a/fast-json/README.md +++ /dev/null @@ -1,6 +0,0 @@ -========= - -## Fast-Json - -### Relevant Articles: -- [A Guide to FastJson](http://www.baeldung.com/????????) diff --git a/fast-json/pom.xml b/fast-json/pom.xml deleted file mode 100644 index b7627e4135..0000000000 --- a/fast-json/pom.xml +++ /dev/null @@ -1,30 +0,0 @@ - - 4.0.0 - - com.baeldung - fast-json - 0.0.1-SNAPSHOT - jar - - fast-json - http://maven.apache.org - - - UTF-8 - - - - - junit - junit - 4.12 - test - - - com.alibaba - fastjson - 1.2.13 - - - diff --git a/json/README.md b/json/README.md new file mode 100644 index 0000000000..c47eca3e84 --- /dev/null +++ b/json/README.md @@ -0,0 +1,7 @@ +========= + +## Fast-Json + +### Relevant Articles: +- [Introduction to JSON Schema in Java](http://www.baeldung.com/introduction-to-json-schema-in-java) +- [A Guide to FastJson](http://www.baeldung.com/????????) diff --git a/json/pom.xml b/json/pom.xml index 47f1f25aa2..4d9efe56e4 100644 --- a/json/pom.xml +++ b/json/pom.xml @@ -13,6 +13,12 @@ 1.3.0 + + com.alibaba + fastjson + 1.2.13 + + junit junit diff --git a/json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java b/json/src/test/java/com/baeldung/json/schema/JSONSchemaTest.java similarity index 88% rename from json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java rename to json/src/test/java/com/baeldung/json/schema/JSONSchemaTest.java index 273fd48bac..bd4aaee682 100644 --- a/json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java +++ b/json/src/test/java/com/baeldung/json/schema/JSONSchemaTest.java @@ -1,31 +1,32 @@ -package org.baeldung.json.schema; - -import org.everit.json.schema.Schema; -import org.everit.json.schema.loader.SchemaLoader; - -import org.json.JSONObject; -import org.json.JSONTokener; -import org.junit.Test; - -public class JSONSchemaTest { - - @Test - public void givenInvalidInput_whenValidating_thenInvalid() { - - JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json"))); - JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_invalid.json"))); - - Schema schema = SchemaLoader.load(jsonSchema); - schema.validate(jsonSubject); - } - - @Test - public void givenValidInput_whenValidating_thenValid() { - - JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json"))); - JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_valid.json"))); - - Schema schema = SchemaLoader.load(jsonSchema); - schema.validate(jsonSubject); - } -} +package com.baeldung.json.schema; + +import org.everit.json.schema.Schema; +import org.everit.json.schema.ValidationException; +import org.everit.json.schema.loader.SchemaLoader; + +import org.json.JSONObject; +import org.json.JSONTokener; +import org.junit.Test; + +public class JSONSchemaTest { + + @Test(expected = ValidationException.class) + public void givenInvalidInput_whenValidating_thenInvalid() { + + JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json"))); + JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_invalid.json"))); + + Schema schema = SchemaLoader.load(jsonSchema); + schema.validate(jsonSubject); + } + + @Test + public void givenValidInput_whenValidating_thenValid() { + + JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json"))); + JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_valid.json"))); + + Schema schema = SchemaLoader.load(jsonSchema); + schema.validate(jsonSubject); + } +} diff --git a/fast-json/src/test/java/com/baeldung/fast_json/FastJsonTests.java b/json/src/test/java/fast_json/FastJsonTests.java similarity index 77% rename from fast-json/src/test/java/com/baeldung/fast_json/FastJsonTests.java rename to json/src/test/java/fast_json/FastJsonTests.java index 9e6771e98b..7b7e3c0434 100644 --- a/fast-json/src/test/java/com/baeldung/fast_json/FastJsonTests.java +++ b/json/src/test/java/fast_json/FastJsonTests.java @@ -1,14 +1,4 @@ -package com.baeldung.fast_json; - -import static org.junit.Assert.*; - -import java.text.ParseException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -import org.junit.Before; -import org.junit.Test; +package fast_json; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; @@ -17,18 +7,30 @@ import com.alibaba.fastjson.serializer.BeanContext; import com.alibaba.fastjson.serializer.ContextValueFilter; import com.alibaba.fastjson.serializer.NameFilter; import com.alibaba.fastjson.serializer.SerializeConfig; +import org.junit.Before; +import org.junit.Test; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import static org.junit.Assert.assertEquals; public class FastJsonTests { - private List listOfPersons = new ArrayList(); + private List listOfPersons; @Before public void setUp() { - listOfPersons.add(new Person(15, "John", "Doe", new Date())); - listOfPersons.add(new Person(20, "Janette", "Doe", new Date())); + listOfPersons = new ArrayList(); + Calendar calendar = Calendar.getInstance(); + calendar.set(2016, 6, 24); + listOfPersons.add(new Person(15, "John", "Doe", calendar.getTime())); + listOfPersons.add(new Person(20, "Janette", "Doe", calendar.getTime())); } @Test - public void convertObjectToJsonTest() { + public void whenJavaList_thanConvertToJsonCorrect() { String personJsonFormat = JSON.toJSONString(listOfPersons); assertEquals( personJsonFormat, @@ -38,16 +40,16 @@ public class FastJsonTests { } @Test - public void convertJsonFormatToObject() { + public void whenJson_thanConvertToObjectCorrect() { String personJsonFormat = JSON.toJSONString(listOfPersons.get(0)); Person newPerson = JSON.parseObject(personJsonFormat, Person.class); assertEquals(newPerson.getAge(), 0); // serialize is set to false for age attribute assertEquals(newPerson.getFirstName(), listOfPersons.get(0).getFirstName()); - assertEquals(newPerson.getLastName(), listOfPersons.get(0).getLastName()); + assertEquals(newPerson.getLastName(), listOfPersons.get(0).getLastName()); } - + @Test - public void createJsonObjectTest() throws ParseException { + public void whenGenerateJson_thanGenerationCorrect() throws ParseException { JSONArray jsonArray = new JSONArray(); for (int i = 0; i < 2; i++) { JSONObject jsonObject = new JSONObject(); @@ -64,10 +66,10 @@ public class FastJsonTests { } @Test - public void convertObjectToJsonUsingContextFilterTest() { + public void givenContextFilter_whenJavaObject_thanJsonCorrect() { ContextValueFilter valueFilter = new ContextValueFilter() { public Object process(BeanContext context, Object object, - String name, Object value) { + String name, Object value) { if (name.equals("DATE OF BIRTH")) { return "NOT TO DISCLOSE"; } @@ -82,7 +84,7 @@ public class FastJsonTests { } @Test - public void convertObjectToJsonUsingSerializeConfig() { + public void givenSerializeConfig_whenJavaObject_thanJsonCorrect() { NameFilter formatName = new NameFilter() { public String process(Object object, String name, Object value) { return name.toLowerCase().replace(" ", "_"); @@ -96,5 +98,7 @@ public class FastJsonTests { "[{\"first_name\":\"Doe\",\"last_name\":\"John\"," + "\"date_of_birth\":\"2016-07-24\"},{\"first_name\":\"Doe\",\"last_name\":" + "\"Janette\",\"date_of_birth\":\"2016-07-24\"}]"); + // resetting custom serializer + SerializeConfig.getGlobalInstance().put(Person.class, null); } } diff --git a/fast-json/src/main/java/com/baeldung/fast_json/Person.java b/json/src/test/java/fast_json/Person.java similarity index 97% rename from fast-json/src/main/java/com/baeldung/fast_json/Person.java rename to json/src/test/java/fast_json/Person.java index 4600eaa712..0eac58cdfd 100644 --- a/fast-json/src/main/java/com/baeldung/fast_json/Person.java +++ b/json/src/test/java/fast_json/Person.java @@ -1,9 +1,9 @@ -package com.baeldung.fast_json; - -import java.util.Date; +package fast_json; import com.alibaba.fastjson.annotation.JSONField; +import java.util.Date; + public class Person { @JSONField(name = "AGE", serialize = false, deserialize = false) diff --git a/pom.xml b/pom.xml index 68841212d4..483dfa4a0f 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ javaxval jooq-spring json-path - fast-json + json mockito mocks jee7schedule