An open API service indexing awesome lists of open source software.

https://github.com/sandysanthosh/springboot-jaxb

Java to xml binding
https://github.com/sandysanthosh/springboot-jaxb

java jaxb xml

Last synced: 26 days ago
JSON representation

Java to xml binding

Awesome Lists containing this project

README

          

# 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:

```

1
Lokesh

```

#### @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:


wsdl4j
wsdl4j

org.codehaus.mojo
jaxb2-maven-plugin
1.6

#### 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