#
Create a SOAP WS (Document style)
This tutorial explains to you how to create a simple Java Web Service using SOAP Document style. This Java Web Service will not be deployed on a Java Application Server.
Supposing you want a Web Service which will receive a name in parameter and will respond you with a list of students having that name. The students attend to some courses. Each course have some attributes. All these information must be received by the Web Service Consumers.
For this, you have to create one class for the Courses:
Course.java
package com;
import java.util.Date;
public class Course {
private String title;
private int durationWeeks;
private Date startDate;
//Class Constructors
public Course() { }
public Course(String title, int durationWeeks, Date startDate) {
this.title = title;
this.durationWeeks = durationWeeks;
this.startDate = startDate;
}
// Getters & setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getDurationWeeks() {
return durationWeeks;
}
public void setDurationWeeks(int durationWeeks) {
this.durationWeeks = durationWeeks;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
}
You need a class for the Students :
Student.java
package com;
import java.util.List;
public class Student {
private String name;
private int age;
private List<course> listCourses;
//Class Constructors
public Student() { }
public Student(String name, int age, List<course> listCourses) {
this.name = name;
this.age = age;
this.listCourses = listCourses;
}
// Getters & setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<course> getListCourses() {
return listCourses;
}
public void setListCourses(List<course> listCourses) {
this.listCourses = listCourses;
}
}
You need a class for add data for this example:
StudentsManagement.java
package com;
import java.util.HashMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class StudentsManagement {
public Map<string, student=""> studentMapList;
private List<course> coursesList = new ArrayList<course>();
private List<string> studentsList = new ArrayList<string>();
public StudentsManagement() throws ParseException {
createMapStudentList();
}
private void createCourseList1() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
Course course1 = new Course("English Language", 8, sdf.parse("2017-07-07"));
Course course2 = new Course("French Language", 8, sdf.parse("2017-07-10"));
Course course3 = new Course("C++ Language", 8, sdf.parse("2017-07-20"));
Course course4 = new Course("Java Language", 8, sdf.parse("2017-07-25"));
coursesList.add(course1);
coursesList.add(course2);
coursesList.add(course3);
coursesList.add(course4);
}
private void createStudentList() {
studentsList.add("John Brown");
studentsList.add("Paul Wollen");
studentsList.add("Paul Brown");
}
private void createMapStudentList() throws ParseException{
createCourseList1();
createStudentList();
Student stud1 = new Student(studentsList.get(0), 21, coursesList.subList(0, 1));
Student stud2 = new Student(studentsList.get(1), 30, coursesList.subList(0, 3));
Student stud3 = new Student(studentsList.get(2), 22, coursesList.subList(0, 1));
studentMapList = new HashMap<string, student="">();
studentMapList.put(studentsList.get(0), stud1);
studentMapList.put(studentsList.get(1), stud2);
studentMapList.put(studentsList.get(2), stud3);
}
}
You need a class for the Service Endpoint Interface (SEI):
ReturnResponse.java
package com;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style=Style.DOCUMENT)
public class ReturnResponse {
private StudentsManagement studentsManagement;
public ReturnResponse () throws ParseException {
studentsManagement = new StudentsManagement();
}
@WebMethod
public List<student> ReturnResponseMethod(String name) {
List<student> studentMapListReturn = new ArrayList<student>();
for(Map.Entry<string, student=""> entry : studentsManagement.studentMapList.entrySet()) {
String key = entry.getKey();
Student value = entry.getValue();
if (key.toLowerCase().contains(name.toLowerCase())) {
studentMapListReturn.add(value);
}
}
return studentMapListReturn;
}
}
If you deploy the Web Service without using a Java Application server, you need a class for Publishing the SEI:
StudentsPublisher.java
package com;
import java.text.ParseException;
import javax.xml.ws.Endpoint;
public class StudentsPublisher {
public static void main(String[ ] args) throws ParseException {
int port = 8888;
String url = "http://localhost:" + port + "/students";
System.out.println("Publishing Students on port " + port);
Endpoint.publish(url, new ReturnResponse());
}
}
Info
- Using this code, you can obtain the WSDL file at http://localhost:8888/students?wsdl .
- Using this code, you can obtain the XSD file at http://localhost:8888/students?xsd=1 .
Info
- Style.RPC : the messages to/from JAX-WS use parameters/ values
- Style.DOCUMENT : the messages to/from JAX-WS use complex XML
- Use.LITERAL : the messages are serialized according to the XSD (XML Schema)
- Use.ENCODED : the messages are serialized according to SOAP data model (in this case we cannot have a XSD validation)
- ParameterStyle .WRAPPED : how the WSDL is created : wrapped style (not very important)
- ParameterStyle .BARE : how the WSDL is created : bare style (not very important)