Nhắc đến SOAP thì là XML, XSD, WSDL, JAXB, JAX-WS, (Apache CXF & Spring-boot)
, SOAPUI.
Thông thường generate file java từ wsdl sẽ có 2 file chính là XxxService và XxxPortType, ví dụ có CustomerService.java và CustomerPortType.java
Example cách gọi SOAP client
try {
CustomerService service = new CustomerService(new URL("http://quachson.com/example/customer?wsdl"));
CustomerPortType portType = service.getCustomerPortType();
GetOrderRequest request = new GetOrderRequest();
request.setCustomerId(1L);
GetOrderResponse response = portType.getOrders(request);
// handle response ...
} catch(MalformedURLException e) {
... write log
}
Muốn build endpoint từ java file được generated từ wsdl thì implement lại XxxPortType
Google để lấy version mới nhất
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>4.1.0</version>
</dependency>
Nếu dùng Java version >10 thì nên add thêm dependency JAXB API 2.1
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
example apache cxf plugin, google thêm thì search: cxf code-gen plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>4.1.0</version> <!--must the same version above -->
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/query.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.15.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.basedir}/src/main/xsd</schemaDirectory>
<schemaIncludes>
<include>eCH-0215-2-0.xsd</include>
</schemaIncludes>
<bindingDirectory>${project.basedir}/src/main/xsd</bindingDirectory>
<bindingIncludes>
<include>global.xjb</include>
</bindingIncludes>
<generateDirectory>${project.basedir}/src/generated</generateDirectory>
</configuration>
</plugin>
</plugins>
</build>
Download global.xjb at here global.xjb
Using maven to generate java codes.
mvn jaxb2:generate