WebService服务器接口设计和实现

参考博客:
https://www.cnblogs.com/nwu-edu/p/9542074.html
https://www.cnblogs.com/liulihaha/p/10518214.html

文章目录

  • WebService接口设计和测试
    • 一、服务端设计webService接口
      • 1、创建一个web项目,引入WebService和Spring的相关maven依赖,配置pom.xml
      • 2、配置spring-cxf.xml
      • 3、配置web.xml
      • 4、定义用于webService接收传参和返回参数的实体类
      • 5、定义webService服务接口
      • 6、实现webService服务接口
      • 7、spring-cxf.xml中发布服务接口
      • 8、启动web服务,在浏览器中访问链接:`http://localhost:8080/项目名/service/helloService?wsdl`,得到下图所示结果即成功!
    • 二、通过Soap UI对WebServie服务接口进行测试
      • 1、创建接口测试项目,New SOAP Project
      • 2、测试接口项目命名,初始化WSDL,即浏览器测试成功的地址:`http://localhost:8080/项目名/service/helloService?wsdl`
      • 3、创建成功后,我们会看到webService服务接口下的所有业务方法,如下图中的:testParamWebService和testWebService方法
      • 4、这里testParamWebService方法为例进行测试
    • 三、客户端调用服务端的WebService服务接口

WebService接口设计和测试

一、服务端设计webService接口

1、创建一个web项目,引入WebService和Spring的相关maven依赖,配置pom.xml



<project xmlns="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.0modelVersion>

  <groupId>com.codequgroupId>
  <artifactId>webServiceartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>webService Maven Webappname>
  
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
  properties>

  <dependencies>
    
    <dependency>
      <groupId>org.apache.cxfgroupId>
      <artifactId>cxf-rt-frontend-jaxwsartifactId>
      <version>3.1.8version>
    dependency>
    <dependency>
      <groupId>org.apache.cxfgroupId>
      <artifactId>cxf-rt-transports-httpartifactId>
      <version>3.1.8version>
    dependency>
    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-coreartifactId>
      <version>4.3.7.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>4.3.7.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aopartifactId>
      <version>4.3.7.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>4.3.7.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>4.3.7.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>4.3.7.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>4.3.7.RELEASEversion>
    dependency>
  dependencies>

project>

2、配置spring-cxf.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://cxf.apache.org/jaxws
                    http://cxf.apache.org/schemas/jaxws.xsd">

    
    <jaxws:endpoint id="helloService" implementor="com.codequ.service.impl.HelloServiceImpl" address="/helloService"/>

beans>

3、配置web.xml


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring-cxf.xmlparam-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  <servlet>
    <servlet-name>cxfservlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>cxfservlet-name>
    <url-pattern>/service/*url-pattern>
  servlet-mapping>
web-app>

4、定义用于webService接收传参和返回参数的实体类

package com.codequ.entity;

/**
 * description:   
* * @version 1.0 * @className: RequestBO * @author: liqing.qu * @date: 2022/2/16 11:06 * @since JDK 1.8 */
public class RequestBO { private String name; private String message; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Override public String toString() { return "RequestBO{" + "name='" + name + '\'' + ", message='" + message + '\'' + '}'; } }
package com.codequ.entity;

/**
 * description:   
* * @version 1.0 * @className: ResultData * @author: liqing.qu * @date: 2022/2/16 11:05 * @since JDK 1.8 */
public class ResultData { private String code; private String result; private String message; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }

5、定义webService服务接口

package com.codequ.service;

import com.codequ.entity.RequestBO;
import com.codequ.entity.ResultData;

import javax.jws.WebService;

/**
 * description:   
* 参考网址:https://www.cnblogs.com/liulihaha/p/10518214.html * @version 1.0 * @className: HelloService * @author: liqing.qu * @date: 2022/2/15 22:08 * @since JDK 1.8 */
@WebService public interface HelloService { public void testWebService(); public ResultData testParamWebService(RequestBO requestBO); }

6、实现webService服务接口

package com.codequ.service.impl;

import com.codequ.entity.RequestBO;
import com.codequ.entity.ResultData;
import com.codequ.service.HelloService;

import javax.jws.WebService;

/**
 * description:   
* * @version 1.0 * @className: HelloServiceImpl * @author: liqing.qu * @date: 2022/2/15 22:09 * @since JDK 1.8 */
@WebService public class HelloServiceImpl implements HelloService { @Override public void testWebService() { System.out.println("服务器webService成功启动!"); } @Override public ResultData testParamWebService(RequestBO requestBO) { System.out.println(requestBO); ResultData resultData = new ResultData(); resultData.setCode("200"); resultData.setResult("SUCCESS"); resultData.setMessage("测试成功"); return resultData; } }

7、spring-cxf.xml中发布服务接口

<jaxws:endpoint id="helloService" implementor="com.codequ.service.impl.HelloServiceImpl" address="/helloService"/>

8、启动web服务,在浏览器中访问链接:http://localhost:8080/项目名/service/helloService?wsdl,得到下图所示结果即成功!

WebService服务器接口设计和实现_第1张图片

二、通过Soap UI对WebServie服务接口进行测试

1、创建接口测试项目,New SOAP Project

WebService服务器接口设计和实现_第2张图片

2、测试接口项目命名,初始化WSDL,即浏览器测试成功的地址:http://localhost:8080/项目名/service/helloService?wsdl

WebService服务器接口设计和实现_第3张图片

3、创建成功后,我们会看到webService服务接口下的所有业务方法,如下图中的:testParamWebService和testWebService方法

WebService服务器接口设计和实现_第4张图片

4、这里testParamWebService方法为例进行测试

1)布局功能

WebService服务器接口设计和实现_第5张图片

2)填写测试数据,发送请求,并返回响应结果

WebService服务器接口设计和实现_第6张图片

3)服务端测试方法正常执行

在这里插入图片描述

三、客户端调用服务端的WebService服务接口

待更新。。。

你可能感兴趣的:(第三方接口开发,服务器,spring,webservice)