From 9f27df367f7c7f0bd56682eabce83fa0d6a121a1 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Thu, 14 Jul 2016 20:38:34 +0700 Subject: [PATCH] Apache CXF with Spring --- apache-cxf/cxf-spring/pom.xml | 117 ++++++++++++++++++ .../com/baeldung/cxf/spring/Baeldung.java | 9 ++ .../com/baeldung/cxf/spring/BaeldungImpl.java | 17 +++ .../java/com/baeldung/cxf/spring/Student.java | 20 +++ .../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 | 32 +++++ apache-cxf/pom.xml | 79 ++++++------ 9 files changed, 266 insertions(+), 39 deletions(-) create mode 100644 apache-cxf/cxf-spring/pom.xml create mode 100644 apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/Baeldung.java create mode 100644 apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/BaeldungImpl.java create mode 100644 apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/Student.java create mode 100644 apache-cxf/cxf-spring/src/main/resources/client-beans.xml create mode 100644 apache-cxf/cxf-spring/src/main/webapp/WEB-INF/cxf-servlet.xml create mode 100644 apache-cxf/cxf-spring/src/main/webapp/WEB-INF/web.xml create mode 100644 apache-cxf/cxf-spring/src/test/java/com/baeldung/cxf/spring/StudentTest.java diff --git a/apache-cxf/cxf-spring/pom.xml b/apache-cxf/cxf-spring/pom.xml new file mode 100644 index 0000000000..02750dd790 --- /dev/null +++ b/apache-cxf/cxf-spring/pom.xml @@ -0,0 +1,117 @@ + + 4.0.0 + cxf-spring + war + + com.baeldung + apache-cxf + 0.0.1-SNAPSHOT + + + 3.1.6 + 4.3.1.RELEASE + 2.19.1 + + + + + maven-war-plugin + 2.6 + + src/main/webapp/WEB-INF/web.xml + + + + 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 + cxf-rt-frontend-jaxws + ${cxf.version} + + + org.apache.cxf + cxf-rt-transports-http-jetty + ${cxf.version} + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-web + ${spring.version} + + + \ No newline at end of file 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 new file mode 100644 index 0000000000..4f880e6ada --- /dev/null +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/Baeldung.java @@ -0,0 +1,9 @@ +package com.baeldung.cxf.spring; + +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/BaeldungImpl.java b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/BaeldungImpl.java new file mode 100644 index 0000000000..911ce7f876 --- /dev/null +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/BaeldungImpl.java @@ -0,0 +1,17 @@ +package com.baeldung.cxf.spring; + +import javax.jws.WebService; + +@WebService(endpointInterface = "com.baeldung.cxf.spring.Baeldung") +public class BaeldungImpl implements Baeldung { + private int counter; + + public String hello(String name) { + return "Hello " + name + "!"; + } + + public String register(Student student) { + counter++; + return student.getName() + " is registered student number " + counter; + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/Student.java b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/Student.java new file mode 100644 index 0000000000..eb58057eb6 --- /dev/null +++ b/apache-cxf/cxf-spring/src/main/java/com/baeldung/cxf/spring/Student.java @@ -0,0 +1,20 @@ +package com.baeldung.cxf.spring; + +public class Student { + private String name; + + Student() { + } + + public Student(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-spring/src/main/resources/client-beans.xml b/apache-cxf/cxf-spring/src/main/resources/client-beans.xml new file mode 100644 index 0000000000..3f30562193 --- /dev/null +++ b/apache-cxf/cxf-spring/src/main/resources/client-beans.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file 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 new file mode 100644 index 0000000000..7669d3f942 --- /dev/null +++ b/apache-cxf/cxf-spring/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file 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 new file mode 100644 index 0000000000..0d7687771b --- /dev/null +++ b/apache-cxf/cxf-spring/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,14 @@ + + + + cxf + org.apache.cxf.transport.servlet.CXFServlet + 1 + + + cxf + /services/* + + \ 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 new file mode 100644 index 0000000000..44a9d11687 --- /dev/null +++ b/apache-cxf/cxf-spring/src/test/java/com/baeldung/cxf/spring/StudentTest.java @@ -0,0 +1,32 @@ +package com.baeldung.cxf.spring; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class StudentTest { + private ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "client-beans.xml" }); + private Baeldung baeldungProxy; + + { + baeldungProxy = (Baeldung) context.getBean("client"); + } + + @Test + public void whenUsingHelloMethod_thenCorrect() { + String response = baeldungProxy.hello("John Doe"); + assertEquals("Hello John Doe!", response); + } + + @Test + public void whenUsingRegisterMethod_thenCorrect() { + Student student1 = new Student("Adam"); + 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); + } +} \ No newline at end of file diff --git a/apache-cxf/pom.xml b/apache-cxf/pom.xml index 41ae75a1de..e86e4be5cd 100644 --- a/apache-cxf/pom.xml +++ b/apache-cxf/pom.xml @@ -1,40 +1,41 @@ - 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 - - - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.baeldung + apache-cxf + 0.0.1-SNAPSHOT + pom + + cxf-introduction + cxf-spring + + + + 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