JAVA-JAXB

Java to xml binding

jaxb

//Without name attribute
@XmlRootElement             //1
 
//With name attribute         
@XmlRootElement(name = "employee")                //2

@XmlRootElement with ‘name’ attribute:

@XmlRootElement(name = "employee")
public class Employee implements Serializable 
{
  @XmlElement(name=employeeId)
  private Integer id;
 
  @XmlElement
  private String firstName;
 
  private String lastName;
  private Department department;
}

Above converts to:

<?xml version="1.0" encoding="UTF-8"?>
<employee>
  <employeeId>1</employeeId>
    <firstName>Lokesh</firstName>
</employee>

@XmlRootElement with ‘name’ attribute with JSON attribute:

@XmlRootElement(name = "employee")
public class Employee implements Serializable 
{
  @XmlElement(name="id")
  @JonProperty("id")
  private Integer id;
 
  @XmlElement(name="firstName")
  @JonProperty("firstName")
  private String firstName;
  
  @XmlElement(name="lastName")
  @JonProperty("lastName")
  private String lastName;
  
  @XmlElement(name="department")
  @JonProperty("department")
  private Department department;
}

Maven plugin:

<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>

   <groupId>org.codehaus.mojo</groupId>
   <artifactId>jaxb2-maven-plugin</artifactId>
    <version>1.6</version>

Student POJO:

    public class Student {
      private Long id;
      private String name;
      private String passportNumber;
      }

Endpoint:

@Endpoint
 public class StudentDetailsEndpoint {

  @PayloadRoot(namespace = "http://in28minutes.com/students", localPart = "GetStudentDetailsRequest")
   @ResponsePayload
    public GetStudentDetailsResponse processCourseDetailsRequest(@RequestPayload GetStudentDetailsRequest request) {
          GetStudentDetailsResponse response = new GetStudentDetailsResponse();

          StudentDetails studentDetails = new StudentDetails();
          studentDetails.setId(request.getId());
          studentDetails.setName("Adam");
          studentDetails.setPassportNumber("E1234567");

          response.setStudentDetails(studentDetails);

          return response;
        }
      }

WebServiceConfig:

  @EnableWs
  @Configuration
  public class WebServiceConfig {

    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext context) {
      MessageDispatcherServlet messageDispatcherServlet = new MessageDispatcherServlet();
      messageDispatcherServlet.setApplicationContext(context);
      messageDispatcherServlet.setTransformWsdlLocations(true);
      return new ServletRegistrationBean(messageDispatcherServlet, "/ws/*");
    }

    @Bean(name = "students")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema studentsSchema) {
      DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
      definition.setPortTypeName("StudentPort");
      definition.setTargetNamespace("http://in28minutes.com/students");
      definition.setLocationUri("/ws");
      definition.setSchema(studentsSchema);
      return definition;
    }

    @Bean
    public XsdSchema studentsSchema() {
      return new SimpleXsdSchema(new ClassPathResource("student-details.xsd"));
    }
  }

/src/main/resources/student-details.xsd

    <?xml version="1.0" encoding="UTF-8"?>

  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  targetNamespace="http://in28minutes.com/students" 
  xmlns:tns="http://in28minutes.com/students" elementFormDefault="qualified">

    <xs:element name="GetStudentDetailsRequest">
      <xs:complexType>
        <xs:sequence>
          <xs:element name= "id" type="xs:int"/>
        </xs:sequence>	
      </xs:complexType>
    </xs:element>

    <xs:element name="GetStudentDetailsResponse">
      <xs:complexType>
        <xs:sequence>
          <xs:element name= "StudentDetails" type="tns:StudentDetails"/>
        </xs:sequence>	
      </xs:complexType>
    </xs:element>

    <xs:complexType name="StudentDetails">
      <xs:sequence>
        <xs:element name="id" type="xs:int"/>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="passportNumber" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
</xs:schema>