了解WSDL服务
首先,让我们来了解一下什么是WSDL(Web Services Description Language)。WSDL是一种XML格式,用于描述Web服务的接口。它定义了服务的位置、可用的操作以及如何使用这些操作。WSDL是构建Web服务的关键组成部分,它使得不同的系统和应用程序能够相互通信。
环境准备
在开始之前,请确保您有以下环境:
- Java开发环境(JDK 1.8或更高版本)
- Maven(用于项目构建)
- Apache CXF(一个开源的Web服务框架)
创建Maven项目
- 打开终端或命令提示符。
- 创建一个新的Maven项目:
mvn archetype:generate -DgroupId=com.example -DartifactId=ws-service -DarchetypeArtifactId=maven-archetype-quickstart
- 等待Maven构建项目结构。
添加依赖
在pom.xml文件中添加以下依赖:
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
创建WSDL服务
- 在项目目录中创建一个名为
src/main/resources的文件夹。 - 在
src/main/resources文件夹中创建一个名为service.wsdl的文件,并添加以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://example.com"
targetNamespace="http://example.com">
<wsdl:message name="GreetingRequest">
<wsdl:part name="name" type="xs:string"/>
</wsdl:message>
<wsdl:message name="GreetingResponse">
<wsdl:part name="greeting" type="xs:string"/>
</wsdl:message>
<wsdl:portType name="GreetingPortType">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:GreetingRequest"/>
<wsdl:output message="tns:GreetingResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="GreetingBinding" type="tns:GreetingPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="sayHello"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="GreetingService">
<wsdl:port name="GreetingPort" binding="tns:GreetingBinding">
<soap:address location="http://localhost:8080/ws-service"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
- 创建一个名为
src/main/java/com/example/wsservice的包。 - 在
com.example.wsservice包中创建一个名为GreetingService.java的类,并添加以下内容:
package com.example.wsservice;
import javax.jws.WebService;
@WebService
public interface GreetingService {
String sayHello(String name);
}
- 创建一个名为
com.example.wsservice.impl的包。 - 在
com.example.wsservice.impl包中创建一个名为GreetingServiceImpl.java的类,并添加以下内容:
package com.example.wsservice.impl;
import com.example.wsservice.GreetingService;
public class GreetingServiceImpl implements GreetingService {
@Override
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
配置CXF
- 在
src/main/resources文件夹中创建一个名为cxf-servlet.xml的文件,并添加以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint id="greetingService" implementor="com.example.wsservice.impl.GreetingServiceImpl"
address="/ws-service"/>
</beans>
启动服务器
- 在
src/main/webapp/WEB-INF文件夹中创建一个名为web.xml的文件,并添加以下内容:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws-service/*</url-pattern>
</servlet-mapping>
</web-app>
- 使用Maven构建项目:
mvn clean install
- 启动Tomcat服务器。
发布API
- 打开浏览器,访问
http://localhost:8080/ws-service?wsdl。 - 您将看到WSDL文件的详细信息。
调用API
- 使用任何支持SOAP的客户端(如Postman)。
- 在客户端中,选择SOAP请求类型,并选择
sayHello操作。 - 在请求体中,输入以下内容:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:sayHello xmlns:ns1="http://example.com">
<name>John</name>
</ns1:sayHello>
</soapenv:Body>
</soapenv:Envelope>
- 发送请求,您将收到以下响应:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:sayHelloResponse xmlns:ns1="http://example.com">
<return>Hello, John!</return>
</ns1:sayHelloResponse>
</soapenv:Body>
</soapenv:Envelope>
恭喜您!您已经成功从零开始部署了一个Java WSDL服务,并学会了如何发布和调用API。希望这个实战教程对您有所帮助!
