java实现简单的webservice方式

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接收两个数字并返回它们的求和结果。

  1. 创建一个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;
    }
}
  1. 使用javac编译类文件
javac com/example/MyWebService.java
  1. 使用wsimport生成WSDL文件和客户端代码
wsimport -keep http://localhost:8080/MyWebService?wsdl
  1. 部署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接收一个字符串,并返回该字符串的大写形式。

  1. 创建一个Java类来实现Web Service
package com.example;

import javax.jws.WebService;

@WebService
public class MyWebService {
    public String toUpperCase(String s) {
        return s.toUpperCase();
    }
}
  1. 使用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>
  1. 编写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>
  1. 生成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
  1. 部署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技术站

(0)
上一篇 2023年5月18日
下一篇 2023年5月18日

相关文章

  • java-list创建的两种常见方式

    下面是Java中使用List创建的两种常见方式的详细解释和示例: 1. 使用ArrayList类创建List对象 ArrayList是Java中最常用的List实现类之一,它提供了一个可以自动扩容的动态数组,可以存储任何类型的对象。ArrayList的构造方法和内置方法都很简单,可以快速轻松地创建和操作List对象。 创建ArrayList对象 使用Arra…

    Java 2023年5月26日
    00
  • Java小白第一次就能看懂的网络编程

    Java小白第一次就能看懂的网络编程攻略 什么是网络编程 网络编程指的是利用计算机网络进行通讯和信息交换的程序设计。Java中的网络编程是通过Java自带的网络库进行开发。 Java中的网络编程有两种:基于TCP协议的Socket编程和基于UDP协议的DatagramSocket编程。 Socket编程 Socket编程是指采用TCP协议来进行编程,是一个基…

    Java 2023年5月23日
    00
  • .properties文件读取及占位符${…}替换源码解析

    下面我来给出“.properties文件读取及占位符${…}替换源码解析”的完整攻略,并提供两个示例说明。 .properties文件读取 在Java中,我们可以使用java.util.Properties类来解析.properties文件。具体的步骤如下: 使用java.io.FileInputStream类将.properties文件读取到输入流中,…

    Java 2023年5月27日
    00
  • maven为MANIFEST.MF文件添加内容的方法

    下面是使用 Maven 为 MANIFEST.MF 文件添加内容的方法的详细攻略。 1. 使用 Maven 插件配置 MANIFEST.MF 文件 Maven 提供了一个叫做 maven-jar-plugin 的插件,可以在 Maven 构建过程中配置 MANIFEST.MF 文件。我们可以通过在 pom.xml 文件中配置此插件来实现在 MANIFEST.…

    Java 2023年5月20日
    00
  • Sprint Boot @CachePut使用方法详解

    在Spring Boot中,@CachePut注解用于将方法的返回值存储到缓存中。使用@CachePut注解可以在方法执行后将结果缓存起来,以便下次使用相同的参数调用该方法时,可以直接从缓存中获取结果,而不必再次执行该方法。本文将详细介绍@CachePut注解的作用和使用方法,并提供两个示例说明。 @CachePut注解的作用 在Spring Boot中,@…

    Java 2023年5月5日
    00
  • java实现KFC点餐系统

    Java实现KFC点餐系统 系统功能 KFC点餐系统是一款简单的餐饮点餐系统,具备以下功能: 浏览菜单:按照品类和价格等条件进行筛选、搜索。 点菜:选择想要的菜品和数量,加入购物车。 查看购物车:查看购物车中的点菜情况,可以修改数量和删除。 下单支付:填写订单信息,选择支付方式并完成支付。 系统架构 KFC点餐系统采用B/S架构模式,使用Java Web技术…

    Java 2023年5月23日
    00
  • 二、设置开发、运行环境

    关于“二、设置开发、运行环境”的完整攻略,我需要进行一些详细的讲解。具体如下: 1. 确定开发环境 首先,我们需要确定我们要使用哪一种语言和开发环境来进行网站开发。通常用于web开发的主流语言有PHP、Python、Ruby等,而开发环境则包括了各种编辑器、库、框架等工具。 例如,如果我们选择使用PHP来进行开发,那么我们可以选择使用著名的开发环境XAMPP…

    Java 2023年6月15日
    00
  • jsp JFreeChart使用心得与例子

    JSP JFreeChart使用心得与例子 简介 JFreeChart是一个Java开源的图表库,可以创建各种类型的图表,包括折线图、散点图、柱状图等等。JFreeChart的使用非常灵活,可以通过Java代码生成图表,也可以使用JSP等Web技术生成图表。 这篇文章主要介绍使用JSP结合JFreeChart生成图表的方法,并给出两个示例。 实现 引入JFr…

    Java 2023年6月15日
    00
合作推广
合作推广
分享本页
返回顶部