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
- Host: GitHub
- URL: https://github.com/sandysanthosh/springboot-jaxb
- Owner: sandysanthosh
- Created: 2018-08-01T17:59:48.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-09-28T05:34:36.000Z (almost 4 years ago)
- Last Synced: 2025-02-28T20:57:11.639Z (over 1 year ago)
- Topics: java, jaxb, xml
- Homepage: https://sandysanthosh.github.io/SpringBoot-JAXB/
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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