Skip to content

Commit 648fb26

Browse files
authored
Merge pull request eugenp#483 from nguyennamthai/master
Introduction to Apache-CXF
2 parents da58ba4 + 2673f28 commit 648fb26

12 files changed

Lines changed: 331 additions & 1 deletion

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>cxf-introduction</artifactId>
7+
<parent>
8+
<groupId>com.baeldung</groupId>
9+
<artifactId>apache-cxf</artifactId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
</parent>
12+
<properties>
13+
<skipTests>true</skipTests>
14+
<cxf.version>3.1.6</cxf.version>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.apache.cxf</groupId>
19+
<artifactId>cxf-rt-frontend-jaxws</artifactId>
20+
<version>${cxf.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.apache.cxf</groupId>
24+
<artifactId>cxf-rt-transports-http-jetty</artifactId>
25+
<version>${cxf.version}</version>
26+
</dependency>
27+
</dependencies>
28+
<profiles>
29+
<profile>
30+
<id>server</id>
31+
<build>
32+
<defaultGoal>test</defaultGoal>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.codehaus.mojo</groupId>
36+
<artifactId>exec-maven-plugin</artifactId>
37+
<executions>
38+
<execution>
39+
<phase>test</phase>
40+
<goals>
41+
<goal>java</goal>
42+
</goals>
43+
<configuration>
44+
<mainClass>com.baeldung.cxf.introduction.Server</mainClass>
45+
</configuration>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</profile>
52+
</profiles>
53+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.cxf.introduction;
2+
3+
import java.util.Map;
4+
5+
import javax.jws.WebService;
6+
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
7+
8+
@WebService
9+
public interface Baeldung {
10+
public String hello(String name);
11+
12+
public String helloStudent(Student student);
13+
14+
@XmlJavaTypeAdapter(StudentMapAdapter.class)
15+
public Map<Integer, Student> getStudents();
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.cxf.introduction;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
import javax.jws.WebService;
7+
8+
@WebService(endpointInterface = "com.baeldung.cxf.introduction.Baeldung")
9+
public class BaeldungImpl implements Baeldung {
10+
private Map<Integer, Student> students = new LinkedHashMap<Integer, Student>();
11+
12+
public String hello(String name) {
13+
return "Hello " + name;
14+
}
15+
16+
public String helloStudent(Student student) {
17+
students.put(students.size() + 1, student);
18+
return "Hello " + student.getName();
19+
}
20+
21+
public Map<Integer, Student> getStudents() {
22+
return students;
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.cxf.introduction;
2+
3+
import javax.xml.ws.Endpoint;
4+
5+
public class Server {
6+
private Server() {
7+
BaeldungImpl implementor = new BaeldungImpl();
8+
String address = "http://localhost:8080/baeldung";
9+
Endpoint.publish(address, implementor);
10+
}
11+
12+
public static void main(String args[]) throws InterruptedException {
13+
new Server();
14+
System.out.println("Server ready");
15+
Thread.sleep(60 * 1000);
16+
System.out.println("Server exiting");
17+
System.exit(0);
18+
}
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.cxf.introduction;
2+
3+
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
4+
5+
@XmlJavaTypeAdapter(StudentAdapter.class)
6+
public interface Student {
7+
public String getName();
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.cxf.introduction;
2+
3+
import javax.xml.bind.annotation.adapters.XmlAdapter;
4+
5+
public class StudentAdapter extends XmlAdapter<StudentImpl, Student> {
6+
public StudentImpl marshal(Student student) throws Exception {
7+
if (student instanceof StudentImpl) {
8+
return (StudentImpl) student;
9+
}
10+
return new StudentImpl(student.getName());
11+
}
12+
13+
public Student unmarshal(StudentImpl student) throws Exception {
14+
return student;
15+
}
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.cxf.introduction;
2+
3+
import javax.xml.bind.annotation.XmlType;
4+
5+
@XmlType(name = "Student")
6+
public class StudentImpl implements Student {
7+
private String name;
8+
9+
StudentImpl() {
10+
}
11+
12+
public StudentImpl(String name) {
13+
this.name = name;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.cxf.introduction;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.xml.bind.annotation.XmlElement;
7+
import javax.xml.bind.annotation.XmlType;
8+
9+
@XmlType(name = "StudentMap")
10+
public class StudentMap {
11+
private List<StudentEntry> entries = new ArrayList<StudentEntry>();
12+
13+
@XmlElement(nillable = false, name = "entry")
14+
public List<StudentEntry> getEntries() {
15+
return entries;
16+
}
17+
18+
@XmlType(name = "StudentEntry")
19+
public static class StudentEntry {
20+
private Integer id;
21+
private Student student;
22+
23+
public void setId(Integer id) {
24+
this.id = id;
25+
}
26+
27+
public Integer getId() {
28+
return id;
29+
}
30+
31+
public void setStudent(Student student) {
32+
this.student = student;
33+
}
34+
35+
public Student getStudent() {
36+
return student;
37+
}
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.cxf.introduction;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
import javax.xml.bind.annotation.adapters.XmlAdapter;
7+
8+
public class StudentMapAdapter extends XmlAdapter<StudentMap, Map<Integer, Student>> {
9+
public StudentMap marshal(Map<Integer, Student> boundMap) throws Exception {
10+
StudentMap valueMap = new StudentMap();
11+
for (Map.Entry<Integer, Student> boundEntry : boundMap.entrySet()) {
12+
StudentMap.StudentEntry valueEntry = new StudentMap.StudentEntry();
13+
valueEntry.setStudent(boundEntry.getValue());
14+
valueEntry.setId(boundEntry.getKey());
15+
valueMap.getEntries().add(valueEntry);
16+
}
17+
return valueMap;
18+
}
19+
20+
public Map<Integer, Student> unmarshal(StudentMap valueMap) throws Exception {
21+
Map<Integer, Student> boundMap = new LinkedHashMap<Integer, Student>();
22+
for (StudentMap.StudentEntry studentEntry : valueMap.getEntries()) {
23+
boundMap.put(studentEntry.getId(), studentEntry.getStudent());
24+
}
25+
return boundMap;
26+
}
27+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.cxf.introduction;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.util.Map;
9+
10+
import javax.xml.namespace.QName;
11+
import javax.xml.ws.Service;
12+
import javax.xml.ws.soap.SOAPBinding;
13+
14+
import com.baeldung.cxf.introduction.Baeldung;
15+
import com.baeldung.cxf.introduction.Student;
16+
import com.baeldung.cxf.introduction.StudentImpl;
17+
18+
public class StudentTest {
19+
private static QName SERVICE_NAME = new QName("http://introduction.cxf.baeldung.com/", "Baeldung");
20+
private static QName PORT_NAME = new QName("http://introduction.cxf.baeldung.com/", "BaeldungPort");
21+
22+
private Service service;
23+
private Baeldung baeldungProxy;
24+
private Baeldung baeldungImpl;
25+
26+
{
27+
service = Service.create(SERVICE_NAME);
28+
String endpointAddress = "http://localhost:8080/baeldung";
29+
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
30+
}
31+
32+
@Before
33+
public void reinstantiateBaeldungInstances() {
34+
baeldungImpl = new BaeldungImpl();
35+
baeldungProxy = service.getPort(PORT_NAME, Baeldung.class);
36+
}
37+
38+
@Test
39+
public void whenUsingHelloMethod_thenCorrect() {
40+
String endpointResponse = baeldungProxy.hello("Baeldung");
41+
String localResponse = baeldungImpl.hello("Baeldung");
42+
assertEquals(localResponse, endpointResponse);
43+
}
44+
45+
@Test
46+
public void whenUsingHelloStudentMethod_thenCorrect() {
47+
Student student = new StudentImpl("John Doe");
48+
String endpointResponse = baeldungProxy.helloStudent(student);
49+
String localResponse = baeldungImpl.helloStudent(student);
50+
assertEquals(localResponse, endpointResponse);
51+
}
52+
53+
@Test
54+
public void usingGetStudentsMethod_thenCorrect() {
55+
Student student1 = new StudentImpl("Adam");
56+
baeldungProxy.helloStudent(student1);
57+
58+
Student student2 = new StudentImpl("Eve");
59+
baeldungProxy.helloStudent(student2);
60+
61+
Map<Integer, Student> students = baeldungProxy.getStudents();
62+
assertEquals("Adam", students.get(1).getName());
63+
assertEquals("Eve", students.get(2).getName());
64+
}
65+
}

0 commit comments

Comments
 (0)