From 800da1bbb47903600853d88da1a690ad6b8c2987 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Fri, 8 Jul 2016 06:40:10 +0700 Subject: [PATCH 1/5] Introduction to Apache-CXF --- apache-cxf/cxf-introduction/pom.xml | 49 ++++++++++++++ .../baeldung/cxf/introduction/Baeldung.java | 16 +++++ .../cxf/introduction/BaeldungImpl.java | 24 +++++++ .../com/baeldung/cxf/introduction/Server.java | 19 ++++++ .../baeldung/cxf/introduction/Student.java | 8 +++ .../cxf/introduction/StudentAdapter.java | 16 +++++ .../cxf/introduction/StudentImpl.java | 23 +++++++ .../baeldung/cxf/introduction/StudentMap.java | 39 +++++++++++ .../cxf/introduction/StudentMapAdapter.java | 27 ++++++++ .../cxf/introduction/StudentTest.java | 65 +++++++++++++++++++ apache-cxf/pom.xml | 40 ++++++++++++ pom.xml | 2 +- 12 files changed, 327 insertions(+), 1 deletion(-) create mode 100644 apache-cxf/cxf-introduction/pom.xml create mode 100644 apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Baeldung.java create mode 100644 apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/BaeldungImpl.java create mode 100644 apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Server.java create mode 100644 apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Student.java create mode 100644 apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentAdapter.java create mode 100644 apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentImpl.java create mode 100644 apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMap.java create mode 100644 apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMapAdapter.java create mode 100644 apache-cxf/cxf-introduction/src/test/java/com/baeldung/cxf/introduction/StudentTest.java create mode 100644 apache-cxf/pom.xml diff --git a/apache-cxf/cxf-introduction/pom.xml b/apache-cxf/cxf-introduction/pom.xml new file mode 100644 index 0000000000..bc4f0a1225 --- /dev/null +++ b/apache-cxf/cxf-introduction/pom.xml @@ -0,0 +1,49 @@ + + + + 4.0.0 + cxf-introduction + + com.baeldung + apache-cxf + 0.0.1-SNAPSHOT + + + 3.1.6 + + + + org.apache.cxf + cxf-rt-frontend-jaxws + ${cxf.version} + + + org.apache.cxf + cxf-rt-transports-http-jetty + ${cxf.version} + + + + + + maven-compiler-plugin + + + org.codehaus.mojo + exec-maven-plugin + + + process-test-classes + + java + + + com.baeldung.cxf.introduction.Server + + + + + + + \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Baeldung.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Baeldung.java new file mode 100644 index 0000000000..6a0b52d504 --- /dev/null +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Baeldung.java @@ -0,0 +1,16 @@ +package com.baeldung.cxf.introduction; + +import java.util.Map; + +import javax.jws.WebService; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +@WebService +public interface Baeldung { + String hello(String name); + + String helloStudent(Student student); + + @XmlJavaTypeAdapter(StudentMapAdapter.class) + Map getStudents(); +} \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/BaeldungImpl.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/BaeldungImpl.java new file mode 100644 index 0000000000..32fd95dc02 --- /dev/null +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/BaeldungImpl.java @@ -0,0 +1,24 @@ +package com.baeldung.cxf.introduction; + +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.jws.WebService; + +@WebService(endpointInterface = "com.baeldung.cxf.introduction.Baeldung") +public class BaeldungImpl implements Baeldung { + Map students = new LinkedHashMap(); + + public String hello(String name) { + return "Hello " + name; + } + + public String helloStudent(Student student) { + students.put(students.size() + 1, student); + return "Hello " + student.getName(); + } + + public Map getStudents() { + return students; + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Server.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Server.java new file mode 100644 index 0000000000..58caa9087e --- /dev/null +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Server.java @@ -0,0 +1,19 @@ +package com.baeldung.cxf.introduction; + +import javax.xml.ws.Endpoint; + +public class Server { + private Server() { + BaeldungImpl implementor = new BaeldungImpl(); + String address = "http://localhost:8080/baeldung"; + Endpoint.publish(address, implementor); + } + + public static void main(String args[]) throws InterruptedException { + new Server(); + System.out.println("Server ready"); + Thread.sleep(60 * 1000); + System.out.println("Server exiting"); + System.exit(0); + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Student.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Student.java new file mode 100644 index 0000000000..68bbb151df --- /dev/null +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Student.java @@ -0,0 +1,8 @@ +package com.baeldung.cxf.introduction; + +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +@XmlJavaTypeAdapter(StudentAdapter.class) +public interface Student { + String getName(); +} \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentAdapter.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentAdapter.java new file mode 100644 index 0000000000..29b829d808 --- /dev/null +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentAdapter.java @@ -0,0 +1,16 @@ +package com.baeldung.cxf.introduction; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + +public class StudentAdapter extends XmlAdapter { + public StudentImpl marshal(Student student) throws Exception { + if (student instanceof StudentImpl) { + return (StudentImpl) student; + } + return new StudentImpl(student.getName()); + } + + public Student unmarshal(StudentImpl student) throws Exception { + return student; + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentImpl.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentImpl.java new file mode 100644 index 0000000000..3f57143909 --- /dev/null +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentImpl.java @@ -0,0 +1,23 @@ +package com.baeldung.cxf.introduction; + +import javax.xml.bind.annotation.XmlType; + +@XmlType(name = "Student") +public class StudentImpl implements Student { + String name; + + StudentImpl() { + } + + public StudentImpl(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMap.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMap.java new file mode 100644 index 0000000000..8593dcaf86 --- /dev/null +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMap.java @@ -0,0 +1,39 @@ +package com.baeldung.cxf.introduction; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlType; + +@XmlType(name = "StudentMap") +public class StudentMap { + private List entries = new ArrayList(); + + @XmlElement(nillable = false, name = "entry") + public List getEntries() { + return entries; + } + + @XmlType(name = "StudentEntry") + static class StudentEntry { + private Integer id; + private Student student; + + public void setId(Integer id) { + this.id = id; + } + + public Integer getId() { + return id; + } + + public void setStudent(Student student) { + this.student = student; + } + + public Student getStudent() { + return student; + } + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMapAdapter.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMapAdapter.java new file mode 100644 index 0000000000..f156676a5f --- /dev/null +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMapAdapter.java @@ -0,0 +1,27 @@ +package com.baeldung.cxf.introduction; + +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + +public class StudentMapAdapter extends XmlAdapter> { + public StudentMap marshal(Map boundMap) throws Exception { + StudentMap valueMap = new StudentMap(); + for (Map.Entry boundEntry : boundMap.entrySet()) { + StudentMap.StudentEntry valueEntry = new StudentMap.StudentEntry(); + valueEntry.setStudent(boundEntry.getValue()); + valueEntry.setId(boundEntry.getKey()); + valueMap.getEntries().add(valueEntry); + } + return valueMap; + } + + public Map unmarshal(StudentMap valueMap) throws Exception { + Map boundMap = new LinkedHashMap(); + for (StudentMap.StudentEntry studentEntry : valueMap.getEntries()) { + boundMap.put(studentEntry.getId(), studentEntry.getStudent()); + } + return boundMap; + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/test/java/com/baeldung/cxf/introduction/StudentTest.java b/apache-cxf/cxf-introduction/src/test/java/com/baeldung/cxf/introduction/StudentTest.java new file mode 100644 index 0000000000..120f3ac5e5 --- /dev/null +++ b/apache-cxf/cxf-introduction/src/test/java/com/baeldung/cxf/introduction/StudentTest.java @@ -0,0 +1,65 @@ +package com.baeldung.cxf.introduction; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +import javax.xml.namespace.QName; +import javax.xml.ws.Service; +import javax.xml.ws.soap.SOAPBinding; + +import com.baeldung.cxf.introduction.Baeldung; +import com.baeldung.cxf.introduction.Student; +import com.baeldung.cxf.introduction.StudentImpl; + +public class StudentTest { + private static QName SERVICE_NAME = new QName("http://introduction.cxf.baeldung.com/", "Baeldung"); + private static QName PORT_NAME = new QName("http://introduction.cxf.baeldung.com/", "BaeldungPort"); + + Service service; + Baeldung baeldungProxy; + Baeldung baeldungImpl; + + { + service = Service.create(SERVICE_NAME); + String endpointAddress = "http://localhost:8080/baeldung"; + service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress); + } + + @Before + public void reinstantiateBaeldungInstances() { + baeldungImpl = new BaeldungImpl(); + baeldungProxy = service.getPort(PORT_NAME, Baeldung.class); + } + + @Test + public void whenUsingHelloMethod_thenCorrect() { + String endpointResponse = baeldungProxy.hello("Baeldung"); + String localResponse = baeldungImpl.hello("Baeldung"); + assertEquals(localResponse, endpointResponse); + } + + @Test + public void whenUsingHelloStudentMethod_thenCorrect() { + Student student = new StudentImpl("John Doe"); + String endpointResponse = baeldungProxy.helloStudent(student); + String localResponse = baeldungImpl.helloStudent(student); + assertEquals(localResponse, endpointResponse); + } + + @Test + public void usingGetStudentsMethod_thenCorrect() { + Student student1 = new StudentImpl("Adam"); + baeldungProxy.helloStudent(student1); + + Student student2 = new StudentImpl("Eve"); + baeldungProxy.helloStudent(student2); + + Map students = baeldungProxy.getStudents(); + assertEquals("Adam", students.get(1).getName()); + assertEquals("Eve", students.get(2).getName()); + } +} \ No newline at end of file diff --git a/apache-cxf/pom.xml b/apache-cxf/pom.xml new file mode 100644 index 0000000000..7732490576 --- /dev/null +++ b/apache-cxf/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + com.baeldung + apache-cxf + 0.0.1-SNAPSHOT + pom + + cxf-introduction + + + + junit + junit + 4.12 + test + + + + install + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.codehaus.mojo + exec-maven-plugin + 1.5.0 + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index f6dda4efd1..42d1704b11 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ assertj - + apache-cxf core-java core-java-8 gson From 6befb0a5a61e50b0699367f59755086001d283a6 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Fri, 8 Jul 2016 23:54:16 +0700 Subject: [PATCH 2/5] Minor fixes for Introduction to Apache CXF --- apache-cxf/cxf-introduction/pom.xml | 3 --- .../main/java/com/baeldung/cxf/introduction/Baeldung.java | 6 +++--- .../java/com/baeldung/cxf/introduction/BaeldungImpl.java | 2 +- .../main/java/com/baeldung/cxf/introduction/Student.java | 2 +- .../java/com/baeldung/cxf/introduction/StudentImpl.java | 2 +- .../main/java/com/baeldung/cxf/introduction/StudentMap.java | 2 +- .../java/com/baeldung/cxf/introduction/StudentTest.java | 6 +++--- 7 files changed, 10 insertions(+), 13 deletions(-) diff --git a/apache-cxf/cxf-introduction/pom.xml b/apache-cxf/cxf-introduction/pom.xml index bc4f0a1225..0a6f387b06 100644 --- a/apache-cxf/cxf-introduction/pom.xml +++ b/apache-cxf/cxf-introduction/pom.xml @@ -26,9 +26,6 @@ - - maven-compiler-plugin - org.codehaus.mojo exec-maven-plugin diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Baeldung.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Baeldung.java index 6a0b52d504..472d38b8e1 100644 --- a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Baeldung.java +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Baeldung.java @@ -7,10 +7,10 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @WebService public interface Baeldung { - String hello(String name); + public String hello(String name); - String helloStudent(Student student); + public String helloStudent(Student student); @XmlJavaTypeAdapter(StudentMapAdapter.class) - Map getStudents(); + public Map getStudents(); } \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/BaeldungImpl.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/BaeldungImpl.java index 32fd95dc02..240f6bb1da 100644 --- a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/BaeldungImpl.java +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/BaeldungImpl.java @@ -7,7 +7,7 @@ import javax.jws.WebService; @WebService(endpointInterface = "com.baeldung.cxf.introduction.Baeldung") public class BaeldungImpl implements Baeldung { - Map students = new LinkedHashMap(); + private Map students = new LinkedHashMap(); public String hello(String name) { return "Hello " + name; diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Student.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Student.java index 68bbb151df..cad8f94d97 100644 --- a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Student.java +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/Student.java @@ -4,5 +4,5 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlJavaTypeAdapter(StudentAdapter.class) public interface Student { - String getName(); + public String getName(); } \ No newline at end of file diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentImpl.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentImpl.java index 3f57143909..bc9dd27afe 100644 --- a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentImpl.java +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentImpl.java @@ -4,7 +4,7 @@ import javax.xml.bind.annotation.XmlType; @XmlType(name = "Student") public class StudentImpl implements Student { - String name; + private String name; StudentImpl() { } diff --git a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMap.java b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMap.java index 8593dcaf86..4c40886c42 100644 --- a/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMap.java +++ b/apache-cxf/cxf-introduction/src/main/java/com/baeldung/cxf/introduction/StudentMap.java @@ -16,7 +16,7 @@ public class StudentMap { } @XmlType(name = "StudentEntry") - static class StudentEntry { + public static class StudentEntry { private Integer id; private Student student; diff --git a/apache-cxf/cxf-introduction/src/test/java/com/baeldung/cxf/introduction/StudentTest.java b/apache-cxf/cxf-introduction/src/test/java/com/baeldung/cxf/introduction/StudentTest.java index 120f3ac5e5..68bc4aa5e8 100644 --- a/apache-cxf/cxf-introduction/src/test/java/com/baeldung/cxf/introduction/StudentTest.java +++ b/apache-cxf/cxf-introduction/src/test/java/com/baeldung/cxf/introduction/StudentTest.java @@ -19,9 +19,9 @@ public class StudentTest { private static QName SERVICE_NAME = new QName("http://introduction.cxf.baeldung.com/", "Baeldung"); private static QName PORT_NAME = new QName("http://introduction.cxf.baeldung.com/", "BaeldungPort"); - Service service; - Baeldung baeldungProxy; - Baeldung baeldungImpl; + private Service service; + private Baeldung baeldungProxy; + private Baeldung baeldungImpl; { service = Service.create(SERVICE_NAME); From afff1c57a01874b578bc8b27c39fded4ae46ab66 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Sat, 9 Jul 2016 15:29:29 +0700 Subject: [PATCH 3/5] Add server profile --- apache-cxf/cxf-introduction/pom.xml | 43 ++++++++++++++++------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/apache-cxf/cxf-introduction/pom.xml b/apache-cxf/cxf-introduction/pom.xml index 0a6f387b06..5534ba34f7 100644 --- a/apache-cxf/cxf-introduction/pom.xml +++ b/apache-cxf/cxf-introduction/pom.xml @@ -24,23 +24,28 @@ ${cxf.version} - - - - org.codehaus.mojo - exec-maven-plugin - - - process-test-classes - - java - - - com.baeldung.cxf.introduction.Server - - - - - - + + + server + + + + org.codehaus.mojo + exec-maven-plugin + + + process-test-classes + + java + + + com.baeldung.cxf.introduction.Server + + + + + + + + \ No newline at end of file From 1f48db7994826bb05c6d110489a39cb78b950746 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Sat, 9 Jul 2016 15:32:08 +0700 Subject: [PATCH 4/5] Adds skipTests attribute --- apache-cxf/cxf-introduction/pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apache-cxf/cxf-introduction/pom.xml b/apache-cxf/cxf-introduction/pom.xml index 5534ba34f7..e43d4a8ee1 100644 --- a/apache-cxf/cxf-introduction/pom.xml +++ b/apache-cxf/cxf-introduction/pom.xml @@ -10,6 +10,7 @@ 0.0.1-SNAPSHOT + true 3.1.6 @@ -34,7 +35,7 @@ exec-maven-plugin - process-test-classes + test java From 2673f28612c38ce53ecf6262445f3d609efb4394 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Sat, 9 Jul 2016 21:30:19 +0700 Subject: [PATCH 5/5] Adds default goal to exec plugin --- apache-cxf/cxf-introduction/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/apache-cxf/cxf-introduction/pom.xml b/apache-cxf/cxf-introduction/pom.xml index e43d4a8ee1..e28039c1af 100644 --- a/apache-cxf/cxf-introduction/pom.xml +++ b/apache-cxf/cxf-introduction/pom.xml @@ -29,6 +29,7 @@ server + test org.codehaus.mojo