WebService server interface design and implementation

Reference blog:
https://www.cnblogs.com/nwu-edu/p/9542074.html
https://www.cnblogs.com/liulihaha/p/10518214.html

WebService interface design and testing

1. Server-side design webService interface

1. Create a web project, introduce WebService and Spring related maven dependencies, and configure pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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.0</modelVersion>

  <groupId>com.codequ</groupId>
  <artifactId>webService</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>webService Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>
    <!-- webService的相关依赖 -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.1.8</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.1.8</version>
    </dependency>
    <!-- 测试 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- spring相关依赖引入 参考博客:https://www.cnblogs.com/nwu-edu/p/9542074.html -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
  </dependencies>

</project>

2. Configure spring-cxf.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: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">

    <!-- webService服务接口的发布,在此文件进行配置(一般在服务接口业务实现后再发布) -->
    <jaxws:endpoint id="helloService" implementor="com.codequ.service.impl.HelloServiceImpl" address="/helloService"/>

</beans>

3. Configure web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>contextConfigLocation</param-name>
    <param-value>classpath:spring-cxf.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/service/*</url-pattern>
  </servlet-mapping>
</web-app>

4. Define the entity class used by webService to receive and return parameters.

package com.codequ.entity;

/**
 * description:   </br>
 *
 * @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:   </br>
 *
 * @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. Define the webService service interface

package com.codequ.service;

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

import javax.jws.WebService;

/**
 * description:   </br>
 * 参考网址: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. Implement webService service interface

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:   </br>
 *
 * @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. Publish service interface in spring-cxf.xml

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

8. Start the web service, access the link in the browser: http://localhost:8080/项目名/service/helloService?wsdl, and get the result shown in the figure below, which is success!

Insert image description here

2. Test the WebServie service interface through Soap UI

1. Create an interface test project, New SOAP Project

Insert image description here

2. Name the test interface project and initialize WSDL, which is the address where the browser test is successful:http://localhost:8080/项目名/service/helloService?wsdl

Insert image description here

3. After the creation is successful, we will see all the business methods under the webService service interface, as shown in the following figure: testParamWebService and testWebService methods

Insert image description here

4. Here the testParamWebService method is used as an example for testing.

1) Layout function

Insert image description here

2) Fill in the test data, send the request, and return the response result

Insert image description here

3) The server-side test method executes normally

Insert image description here

3. The client calls the WebService service interface of the server

pending upgrade. . .

Guess you like

Origin blog.csdn.net/weixin_42164880/article/details/122961216