Generate Java code cho client từ WSDL

Phiên bản IDE sử dụng: IntelliJ IDEA 2021 Ultimate. Có thể sử dụng URL sau như một sample WSDL resource có sẵn trên internet: http://apismsbrand.viettel.vn:8998/bulkapi?wsdl Khởi tạo project với pom.xml <?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>sample_generate_code_wsdl</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><name>sample_generate_code_wsdl</name><modules><module>projects</module><module>primary-source</module><module>servlets</module><module>ejbs</module><module>ear</module></modules><distributionManagement><site><id>site</id><name>project website</name><url>scp://local.company.com/websites/project.company.com/</url></site></distributionManagement><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency><groupId>org.example</groupId><artifactId>logging</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.example</groupId><artifactId>primary-source</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.example</groupId><artifactId>servlet</artifactId><version>1.0-SNAPSHOT</version><type>war</type></dependency><dependency><groupId>org.example</groupId><artifactId>ejbs</artifactId><version>1.0-SNAPSHOT</version><type>ejb</type></dependency></dependencies></dependencyManagement><build><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-ear-plugin</artifactId><version>3.0.1</version></plugin><plugin><artifactId>maven-ejb-plugin</artifactId><version>3.0.1</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin><plugin><artifactId>maven-javadoc-plugin</artifactId><version>3.0.0</version></plugin></plugins></pluginManagement></build></project> Một

Phiên bản IDE sử dụng: IntelliJ IDEA 2021 Ultimate. Có thể sử dụng URL sau như một sample WSDL resource có sẵn trên internet: http://apismsbrand.viettel.vn:8998/bulkapi?wsdl

Khởi tạo project với pom.xml

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>sample_generate_code_wsdl</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><name>sample_generate_code_wsdl</name><modules><module>projects</module><module>primary-source</module><module>servlets</module><module>ejbs</module><module>ear</module></modules><distributionManagement><site><id>site</id><name>project website</name><url>scp://local.company.com/websites/project.company.com/</url></site></distributionManagement><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency><groupId>org.example</groupId><artifactId>logging</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.example</groupId><artifactId>primary-source</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.example</groupId><artifactId>servlet</artifactId><version>1.0-SNAPSHOT</version><type>war</type></dependency><dependency><groupId>org.example</groupId><artifactId>ejbs</artifactId><version>1.0-SNAPSHOT</version><type>ejb</type></dependency></dependencies></dependencyManagement><build><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-ear-plugin</artifactId><version>3.0.1</version></plugin><plugin><artifactId>maven-ejb-plugin</artifactId><version>3.0.1</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin><plugin><artifactId>maven-javadoc-plugin</artifactId><version>3.0.0</version></plugin></plugins></pluginManagement></build></project>

Một class sample được generate

packagecom.example;importjavax.xml.bind.annotation.XmlAccessType;importjavax.xml.bind.annotation.XmlAccessorType;importjavax.xml.bind.annotation.XmlElement;importjavax.xml.bind.annotation.XmlType;/**
 * <p>Java class for checkBalance complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * <complexType name="checkBalance">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <sequence>
 *         <element name="User" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         <element name="Password" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         <element name="CPCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       </sequence>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * </pre>
 * 
 * 
 */@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name ="checkBalance", propOrder ={"user","password","cpCode"})publicclassCheckBalance{@XmlElement(name ="User")protectedString user;@XmlElement(name ="Password")protectedString password;@XmlElement(name ="CPCode")protectedString cpCode;/**
     * Gets the value of the user property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */publicStringgetUser(){return user;}/**
     * Sets the value of the user property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */publicvoidsetUser(String value){this.user = value;}/**
     * Gets the value of the password property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */publicStringgetPassword(){return password;}/**
     * Sets the value of the password property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */publicvoidsetPassword(String value){this.password = value;}/**
     * Gets the value of the cpCode property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */publicStringgetCPCode(){return cpCode;}/**
     * Sets the value of the cpCode property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */publicvoidsetCPCode(String value){this.cpCode = value;}}

Mã nguồn: https://github.com/donhuvy/sample_generate_code_wsdl
Trong trường hợp URL WSDL không sẵn có, bạn có thể sử dụng file tĩnh bulkapi.xml .
Có thể dùng một số WSDL có sẵn trên mạng http://www.dneonline.com/calculator.asmx?WSDL

Nguồn: viblo.asia

Bài viết liên quan

9 Mẹo lập trình Web “ẩn mình” giúp tiết kiệm hàng giờ đồng hồ

Hầu hết các lập trình viên (kể cả những người giỏi) đều tốn thời gian x

Can GPT-4o Generate Images? All You Need to Know about GPT-4o-image

OpenAI‘s GPT-4o, introduced on March 25, 2025, has revolutionized the way we create visual con

Khi nào nên dùng main, section, article, header, footer, và aside trong HTML5

HTML5 đã giới thiệu các thẻ ngữ nghĩa giúp cấu trúc nội dung web một cách có

So sánh Webhook và API: Khi nào nên sử dụng?

Trong lĩnh vực công nghệ thông tin và phát triển phần mềm, Webhook và API là hai th