Handling interfaces in RPC-style web services

Recall that the message style for our web service implementation class is Document and the encoding is literal. Let's change the style to RPC. Open CourseManagementService.java and change the style of the SOAP binding from Style.DOCUMENT to Style.RPC:

@WebService 
@SOAPBinding(style=Style.RPC, use=Use.LITERAL) 
public class CourseManagementService {...} 

Restart Tomcat. In the Tomcat console, you might see the following error:

    Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
    java.util.List is an interface, and JAXB can't handle interfaces.
      this problem is related to the following location:
        at java.util.List

This problem is caused by the following method definition in the CourseManagementService class:

  public List<Course> getCourses() {...} 

In RPC-style SOAP binding, JAX-WS uses JAXB, and JAXB cannot marshal interfaces very well. A blog entry at https://community.oracle.com/blogs/kohsuke/2006/06/06/jaxb-and-interfaces tries to explain the reason for this. The workaround is to create a wrapper for List and annotate it with @XMLElement. So, create a new class called Courses in the same package:

package packt.jee.eclipse.ws.soap; 
 
import java.util.List; 
 
import javax.xml.bind.annotation.XmlAnyElement; 
import javax.xml.bind.annotation.XmlRootElement; 
 
@XmlRootElement 
public class Courses { 
  @XmlAnyElement 
  public List<Course> courseList; 
 
  public Courses() { 
 
  } 
 
  public Courses (List<Course> courseList) { 
    this.courseList = courseList; 
  } 
} 

Then, modify the getCourses method of CourseManagementService to return the Courses object instead of List<Course>:

  public Courses getCourses() { 
    //Here, courses could be fetched from database using, 
    //for example, JDBC or JDO. However, to keep this example 
    //simple, we will return hardcoded list of courses 
 
    List<Course> courses = new ArrayList<Course>(); 
 
    courses.add(new Course(1, "Course-1", 4)); 
    courses.add(new Course(2, "Course-2", 3)); 
 
    return new Courses(courses); 
  } 

Restart Tomcat. This time, the application should be deployed in Tomcat without any error. Re-generate the client classes by using wsimport, run the client application, and verify the results.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.32