diff --git a/apache-cxf/cxf-introduction/pom.xml b/apache-cxf/cxf-introduction/pom.xml new file mode 100644 index 0000000000..e28039c1af --- /dev/null +++ b/apache-cxf/cxf-introduction/pom.xml @@ -0,0 +1,53 @@ + + + + 4.0.0 + cxf-introduction + + com.baeldung + apache-cxf + 0.0.1-SNAPSHOT + + + true + 3.1.6 + + + + org.apache.cxf + cxf-rt-frontend-jaxws + ${cxf.version} + + + org.apache.cxf + cxf-rt-transports-http-jetty + ${cxf.version} + + + + + server + + test + + + org.codehaus.mojo + exec-maven-plugin + + + test + + 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..472d38b8e1 --- /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 { + public String hello(String name); + + public String helloStudent(Student student); + + @XmlJavaTypeAdapter(StudentMapAdapter.class) + 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 new file mode 100644 index 0000000000..240f6bb1da --- /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 { + private 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..cad8f94d97 --- /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 { + public 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..bc9dd27afe --- /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 { + private 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..4c40886c42 --- /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") + public 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..68bc4aa5e8 --- /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"); + + private Service service; + private Baeldung baeldungProxy; + private 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 fa4777860d..c9f47a870a 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ assertj - + apache-cxf core-java core-java-8 gson