Java webSerivce的使用看完你就明白了

yizhihongxing

下面是一个完整的攻略,帮助你了解和使用Java Web Service。

Java Web Service 的使用看完你就明白了

什么是 Java Web Service

Java Web Service 是一种基于 XML 和 HTTP 协议的远程服务技术,它允许应用程序在不同操作系统、不同编程语言和不同的硬件平台上进行交互和通信。

Java Web Service 的工作原理是基于三个基本要素:WSDL、SOAP 和 XML Schema。

  • WSDL:Web Services Description Language,定义 Web Service 的接口和方法。通过 WSDL 文件,客户端和服务端可以相互了解和通信。
  • SOAP:Simple Object Access Protocol,是一种基于 XML 的消息交换协议,用于在 Web Service 客户端和服务端传递消息。
  • XML Schema:用于定义 XML 文档的结构和内容,同时也用于定义 Web Service 的数据格式和数据类型。

Java Web Service 的类型

Java Web Service 主要分为两种类型:

  • SOAP Web Service:基于 SOAP 协议的 Web Service,与平台和语言无关。
  • RESTful Web Service:一种基于 RESTful 架构风格的 Web Service,通常使用 JSON 数据格式。

本文重点介绍 SOAP Web Service 的使用。

Java Web Service 的创建步骤

要创建一个 Java Web Service,通常需要以下几个步骤:

  1. 定义接口:定义 Web Service 的接口和方法,同时指定 WSDL 文件的位置和格式。
  2. 实现接口:实现 Web Service 的接口和方法,同时根据需要进行参数验证。
  3. 发布服务:使用 JAX-WS API 将实现类发布为 Web Service,同时指定服务名称和访问路径。
  4. 测试服务:编写 Web Service 客户端,测试 Web Service 的功能和可用性。

下面,我们通过一个示例来详细说明这四个步骤。

示例一:发布一个简单的 Web Service

  1. 定义接口

首先,我们创建一个简单的接口,用于演示 Web Service 的基本用法。

@WebService
public interface HelloWorld {
    @WebMethod
    String sayHello(String name);
}

在上述代码中,使用了 @WebService 注解定义了一个 Web Service 接口,并使用 @WebMethod 注解定义了一个 sayHello 的方法。

  1. 实现接口

接下来,我们实现这个接口,并为 sayHello 方法进行参数验证。

@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    @Override
    public String sayHello(String name) {
        if (name == null || name.trim().equals("")) {
            throw new IllegalArgumentException("Name cannot be null or empty");
        }
        return "Hello, " + name;
    }
}

在上述代码中,我们使用 @WebService 注解指定了服务名称和命名空间,并继承了 HelloWorld 接口。同时在 sayHello 方法中,加入了简单的参数验证。

  1. 发布服务

接下来,我们使用 JAX-WS API 将实现类发布为 Web Service。

public class Main {
    public static void main(String[] args) {
        HelloWorldImpl implementor = new HelloWorldImpl();
        String address = "http://localhost:8080/HelloWorld";
        Endpoint.publish(address, implementor);
    }
}

在上述代码中,使用 Endpoint 类的 publish 方法将 HelloWorldImpl 实现类发布为 Web Service。同时,指定了服务的访问路径为 http://localhost:8080/HelloWorld

  1. 测试服务

最后,我们编写一个简单的 Web Service 客户端,用于测试 Web Service 的功能和可用性。

public class Client {
    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("http://localhost:8080/HelloWorld?wsdl");
        QName qname = new QName("http://example.com/", "HelloWorldImplService");
        Service service = Service.create(url, qname);
        HelloWorld helloWorld = service.getPort(HelloWorld.class);

        String result = helloWorld.sayHello("World");
        System.out.println(result);
    }
}

在上述代码中,我们先通过 URL 和 QName 获取 Web Service 的实现类,并通过该实现类的 sayHello 方法获取 Web Service 的返回结果。

示例二:使用 JAXB 处理复杂数据类型

在 Web Service 中,还可以使用 JAXB(Java Architecture for XML Binding)将复杂的数据类型转换成 XML,并通过 Web Service 进行传输。

以下示例演示了如何在 Web Service 中使用 JAXB 处理复杂数据类型。

  1. 定义 Java 类

首先,我们创建一个 Java 类,用于表示一个简单的学生对象。

@XmlRootElement
public class Student {
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

在上述代码中,我们使用 @XmlRootElement 注解将 Student 类标记为根元素,同时定义了三个属性: idnameage

  1. 修改 Web Service 接口

接下来,我们使用 Student 对象作为参数,修改 Web Service 的接口和实现类。

@WebService
public interface StudentService {
    @WebMethod
    Student getStudent(int id);
}
@WebService(endpointInterface = "com.example.StudentService")
public class StudentServiceImpl implements StudentService {
    public Student getStudent(int id) {
        Student student = new Student();
        student.setId(id);
        student.setName("Tom");
        student.setAge(20);
        return student;
    }
}

在上述代码中,我们在 StudentService 接口中,使用 Student 对象作为返回值类型,并在 StudentServiceImpl 实现类中,使用 Student 对象作为返回值。

  1. 生成 WSDL 文件

接下来,我们需要使用 wsgen 工具来生成 WSDL 文件。

要生成 WSDL 文件,使用下述代码:

wsgen -cp . com.example.StudentServiceImpl -wsdl

在上述代码中,使用 wsgen 命令生成了 StudentServiceImpl 类的 WSDL 文件。

  1. 修改 Main 方法

因为现在要使用 Student 对象作为参数,同时需要使用 JAXBStudent 对象转换为 XML,因此要对 Main 方法进行些许修改。

public class Main {
    public static void main(String[] args) {
        StudentServiceImpl implementor = new StudentServiceImpl();
        String address = "http://localhost:8080/StudentService";
        Endpoint.publish(address, implementor);

        System.out.println("Web Service 已发布。");
    }
}

在上述代码中,我们移除了 Main 方法中的 URLEncoded 方法,因为现在已经不需要获取 WSDL 文件。同时,我们在 StudentServiceImpl 类中使用了 @XmlRootElement 注解,以表示 Student 类型可以通过 XML 进行传输。

  1. 修改 Client 方法

最后,我们修改 Client 方法,使其可以将 XML 转换为 Student 类型。

public class Client {
    public static void main(String[] args) throws MalformedURLException, JAXBException {
        URL url = new URL("http://localhost:8080/StudentService?wsdl");
        QName qname = new QName("http://example.com/", "StudentServiceImplService");
        Service service = Service.create(url, qname);
        StudentService studentService = service.getPort(StudentService.class);

        Student student = studentService.getStudent(123);
        JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter sw = new StringWriter();
        marshaller.marshal(student, sw);
        String xml = sw.toString();
        System.out.println(xml);
    }
}

在上述代码中,我们先使用 service.getPort 方法获取 StudentService 对象,然后通过 getStudent 方法获得 Student 类的实例化对象。同时,使用 JAXBContextStudent 类型转换为 XML,最后将 XML 输出到控制台中。

结语

Java Web Service 为我们提供了一种简单而强大的远程服务技术,可以让我们的应用程序在不同环境和平台之间进行通信。通过对本文提供的两个示例的详细解释,相信你已经掌握了基础的 Java Web Service 的使用方法。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java webSerivce的使用看完你就明白了 - Python技术站

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

相关文章

  • Java如何实现支付宝电脑支付基于servlet版本

    Java 如何实现支付宝电脑支付基于 Servlet 版本,具体的实现步骤如下: 1. 注册支付宝商家账号 首先需要注册一个支付宝商家账号。 2. 下载支付宝开发者工具包 下载支付宝提供的开发者工具包,官方推荐使用 Java 版本的 SDK。 3. 创建订单 在进行支付前需要创建一个订单,在创建订单时需要填写订单的一些基本信息,例如订单金额、商品名称、订单号…

    Java 2023年5月26日
    00
  • 浅谈hibernate之映射文件VS映射注解

    如何选择使用Hibernate的映射文件或映射注解?这是Hibernate初学者常常疑惑的问题。本文将深入浅出地介绍这个话题,帮助读者更好地掌握Hibernate的使用方法。 什么是映射文件? Hibernate的映射文件定义了Java类和数据库表之间的映射关系。映射文件只是一个XML格式的文件,用于Hibernate根据属性及其映射关系创建数据表和对象。H…

    Java 2023年5月19日
    00
  • 一文详解Java线程的6种状态与生命周期

    一文详解Java线程的6种状态与生命周期 线程生命周期 Java线程的生命周期可以分为6种不同的状态:1. New(新建): 当线程对象被创建时,它处于新建状态,但还没有开始运行。2. Runnable(可运行): 当调用start()方法时,线程进入可运行状态,等待被线程调度器分派时间片得以运行。3. Blocked(阻塞): 线程被阻塞于某一个等待状态,…

    Java 2023年5月19日
    00
  • Java中使用fileupload组件实现文件上传功能的实例代码

    介绍 在Java Web开发中,文件上传功能是一个非常常见和基础的功能。而使用fileupload组件实现文件上传,不仅方便易用,而且功能强大,能够满足大多数文件上传需求。 本文将介绍如何使用fileupload组件实现文件上传功能的实例代码并附有完整代码和两个示例供您参考。在实现文件上传的过程中,我们需要引入Apache Commons FileUploa…

    Java 2023年5月19日
    00
  • Spring Boot Thymeleaf实现国际化的方法详解

    在Spring Boot应用程序中,我们可以使用Thymeleaf模板引擎来实现国际化。Thymeleaf提供了一种简单而有效的方式来处理多语言文本,它可以根据用户的语言环境自动选择正确的文本。在本文中,我们将详细讲解Spring Boot Thymeleaf实现国际化的方法。 配置文件 在Spring Boot应用程序中,我们可以使用配置文件来定义多语言文…

    Java 2023年5月18日
    00
  • 利用Java的Struts框架实现电子邮件发送功能

    利用Java的Struts框架实现电子邮件发送功能 在Struts框架中,可以使用JavaMail实现电子邮件的发送。下面是实现电子邮件发送的完整攻略: 步骤1:导入JavaMail和相关依赖 要使用JavaMail,需要将相关的jar包导入项目中。可以下载JavaMail的jar包和JAF(Java Activation Framework)的jar包,导…

    Java 2023年5月20日
    00
  • Java SE Development Kit (JDK7) 介绍与配置方法

    Java SE Development Kit (JDK7) 介绍与配置方法 Java SE Development Kit (JDK)是Java平台的核心组件,可以提供编译、调试和执行Java应用程序的环境。JDK包含Java运行时环境(Java Runtime Environment,JRE),Java编译器(Java Compiler,javac)和J…

    Java 2023年5月26日
    00
  • java el简介及用法

    Java EL 简介及用法 Java Expression Language(Java EL)是用于在Java Web应用程序中计算表达式的语言。Java EL 可以在页面中引用或调用Java Bean中的属性、方法等,并能在JSP、JSF、Struts、Spring等框架中使用。 语法 Java EL 对象名称可以分为两部分:对象名称和对象属性。对象名称是…

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