Java实现简单的Web Service方式可以通过使用两种不同的编程方式来完成:JAX-WS和Apache CXF。下面我们分别介绍这两种方式。
JAX-WS方式实现Web Service
JAX-WS是Java API for XML Web Services的缩写,是Java SE 6及以上提供的一种WebService编程API。
下面是一个简单的JAX-WS Web Service的例子,该Web Service接收两个数字并返回它们的求和结果。
- 创建一个Java类来实现Web Service
package com.example;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class MyWebService {
@WebMethod
public int add(int a, int b) {
return a + b;
}
}
- 使用javac编译类文件
javac com/example/MyWebService.java
- 使用wsimport生成WSDL文件和客户端代码
wsimport -keep http://localhost:8080/MyWebService?wsdl
- 部署Web Service
可以使用Java Web容器如Apache Tomcat或Jetty部署该Web Service,或者使用以下命令启动内置Web服务器:
java javax.xml.ws.Endpoint publish("http://localhost:8080/MyWebService", new MyWebService());
至此,我们的Web Service已经成功部署,并且可以通过访问http://localhost:8080/MyWebService?wsdl来获取WSDL文件,然后使用生成的客户端代码调用该Web Service的方法。
Apache CXF方式实现Web Service
Apache CXF是一个开源的WebService框架,可以使用其提供的工具轻松创建并发布Web Service。下面是一个简单的Apache CXF Web Service的例子,该Web Service接收一个字符串,并返回该字符串的大写形式。
- 创建一个Java类来实现Web Service
package com.example;
import javax.jws.WebService;
@WebService
public class MyWebService {
public String toUpperCase(String s) {
return s.toUpperCase();
}
}
- 使用Apache CXF Maven插件来生成代码
mvn archetype:generate -DgroupId=com.example -DartifactId=my-webservice -DarchetypeArtifactId=maven-archetype-quickstart
cd my-webservice
然后在pom.xml中添加CXF的依赖,并配置插件。
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>3.3.1</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/wsdl/mywebservices.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 编写WSDL文件
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="MyWebService" targetNamespace="http://example.com/">
<wsdl:types>
<xsd:schema targetNamespace="http://example.com/">
<xsd:element name="toUpperCase" type="xsd:string"/>
<xsd:element name="toUpperCaseResponse" type="xsd:string"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="toUpperCaseRequest">
<wsdl:part name="parameters" element="tns:toUpperCase" />
</wsdl:message>
<wsdl:message name="toUpperCaseResponse">
<wsdl:part name="parameters" element="tns:toUpperCaseResponse" />
</wsdl:message>
<wsdl:portType name="MyWebServicePortType">
<wsdl:operation name="toUpperCase">
<wsdl:input message="tns:toUpperCaseRequest" />
<wsdl:output message="tns:toUpperCaseResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="MyWebServiceBinding" type="tns:MyWebServicePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"/>
<wsdl:operation name="toUpperCase">
<wsdlsoap:operation soapAction=""/>
<wsdl:input><wsdlsoap:body use="literal"/></wsdl:input>
<wsdl:output><wsdlsoap:body use="literal"/></wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MyWebService">
<wsdl:port binding="tns:MyWebServiceBinding" name="MyWebServicePort">
<wsdlsoap:address location="http://localhost:8080/MyWebService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
- 生成Python客户端代码
java -cp target/classes/:$HOME/.m2/repository/org/apache/cxf/cxf-rt-bindings-soap/3.3.1/cxf-rt-bindings-soap-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-bundle-jaxrs/3.3.1/cxf-bundle-jaxrs-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-core/3.3.1/cxf-core-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-rt-bindings-xml/3.3.1/cxf-rt-bindings-xml-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-rt-frontend-jaxrs/3.3.1/cxf-rt-frontend-jaxrs-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-rt-frontend-simple/3.3.1/cxf-rt-frontend-simple-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-rt-transports-http/3.3.1/cxf-rt-transports-http-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-rt-wsdl/3.3.1/cxf-rt-wsdl-3.3.1.jar org.apache.cxf.tools.wsdlto.WSDLToJava -client -d target/src/main/java -p com.example http://localhost:8080/MyWebService?wsdl
- 部署Web Service
可以使用Java Web容器如Apache Tomcat或Jetty部署该Web Service,或者使用以下命令启动内置Web服务器:
java -cp target/classes/:$HOME/.m2/repository/org/apache/cxf/cxf-rt-bindings-soap/3.3.1/cxf-rt-bindings-soap-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-bundle-jaxrs/3.3.1/cxf-bundle-jaxrs-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-core/3.3.1/cxf-core-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-rt-bindings-xml/3.3.1/cxf-rt-bindings-xml-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-rt-frontend-jaxrs/3.3.1/cxf-rt-frontend-jaxrs-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-rt-frontend-simple/3.3.1/cxf-rt-frontend-simple-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-rt-transports-http/3.3.1/cxf-rt-transports-http-3.3.1.jar:$HOME/.m2/repository/org/apache/cxf/cxf-rt-wsdl/3.3.1/cxf-rt-wsdl-3.3.1.jar org.apache.cxf.endpoint.ServerImpl -serviceClass com.example.MyWebService -serviceName "{http://example.com/}MyWebService" -portName "{http://example.com/}MyWebServicePort" -address "/MyWebService"
至此,我们的Web Service已经成功部署,并且可以通过访问http://localhost:8080/MyWebService?wsdl来获取WSDL文件,然后使用生成的客户端代码调用该Web Service的方法。
以上就是使用JAX-WS和Apache CXF两种方式实现Java Web Service的完整攻略,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java实现简单的webservice方式 - Python技术站