SpringWS创建webservice服务端及客户端

SpringWS创建webservice服务端及客户端一、服务端下图主要目录结构,这个是完整的,下面,我们就来构建1.1、创建springinitializr,下一步1.2选中springweb,下一步,最后创建1.3导入依赖 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web- services&

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

springws项目地址

一、服务端

下图 主要目录结构,这个是完整的,下面,我们就来构建
在这里插入图片描述

1.1、创建spring initializr,下一步

在这里插入图片描述

1.2选中springweb,下一步,最后创建

在这里插入图片描述

1.3导入依赖

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web-				services</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.ws</groupId>
			<artifactId>spring-ws-core</artifactId>
			<version>2.4.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>wsdl4j</groupId>
			<artifactId>wsdl4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>6.0.18.Final</version>
		</dependency>
	<dependencies>


<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<fork>true</fork>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>jaxb2-maven-plugin</artifactId>
				<version>1.6</version>
				<executions>
					<execution>
						<id>xjc</id>
						<goals>
							<goal>xjc</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<schemaDirectory>
						${ 
   project.basedir}/src/main/resources
					</schemaDirectory>
					<outputDirectory>
						${ 
   project.basedir}/src/main/java
					</outputDirectory>
					<clearOutputDir>
						false
					</clearOutputDir>
					<encoding>utf-8</encoding>
				</configuration>
			</plugin>
		</plugins>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xsd</include> </includes> </resource> </resources> </build> 

2.1生成下列基础文件

1、上面添加的依赖,就是配置自动生成下列文件的

<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>jaxb2-maven-plugin</artifactId>
				<version>1.6</version>
				<executions>
					<execution>
						<id>xjc</id>
						<goals>
							<goal>xjc</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<schemaDirectory>
						${ 
   project.basedir}/src/main/resources
					</schemaDirectory>
					<outputDirectory>
						${ 
   project.basedir}/src/main/java
					</outputDirectory>
					<clearOutputDir>
						false
					</clearOutputDir>
					<encoding>utf-8</encoding>
				</configuration>
			</plugin>

2、编写pim.xsd文件,此文件放于resources(springWS\src\main\resources\pim.xsd)

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://tempuri.org/"
           elementFormDefault="qualified">
    <xs:element name="PlmWsMainRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="UserToken" type="xs:string"/>
                <xs:element name="IFaceCode" type="xs:string"/>
                <xs:element name="BatchNumber" type="xs:string"/>
                <xs:element name="Data" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="PlmWsMainResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="ResponseData" type="xs:string"/>
                <xs:element name="ReturnCode" type="xs:string"/>
                <xs:element name="ReturnMessage" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

3、运行install即可生成,运行10秒即可停止,不用等全部运行完
在这里插入图片描述

2、在包下springWS\src\main\java\org\tempuri生成的文件,其中PlmWsMainRequest和PlmWsMainResponse是用于传输请求的参数和返回的参数
(1)ObjectFactory.java



//
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.7 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2021.01.13 时间 05:01:43 PM CST 
//


package org.tempuri;

import javax.xml.bind.annotation.XmlRegistry;


/** * This object contains factory methods for each * Java content interface and Java element interface * generated in the org.tempuri package. * <p>An ObjectFactory allows you to programatically * construct new instances of the Java representation * for XML content. The Java representation of XML * content can consist of schema derived interfaces * and classes representing the binding of schema * type definitions, element declarations and model * groups. Factory methods for each of these are * provided in this class. * */
@XmlRegistry
public class ObjectFactory { 
   


    /** * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.tempuri * */
    public ObjectFactory() { 
   
    }

    /** * Create an instance of {@link PlmWsMainResponse } * */
    public PlmWsMainResponse createPlmWsMainResponse() { 
   
        return new PlmWsMainResponse();
    }

    /** * Create an instance of {@link PlmWsMainRequest } * */
    public PlmWsMainRequest createPlmWsMainRequest() { 
   
        return new PlmWsMainRequest();
    }

}

(2)package-info.java



//
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.7 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2021.01.13 时间 05:01:43 PM CST 
//

@javax.xml.bind.annotation.XmlSchema(namespace = "http://tempuri.org/", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.tempuri;


(3)PlmWsMainRequest.java

//
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.7 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2021.01.13 时间 05:01:43 PM CST 
//


package org.tempuri;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/** * <p>anonymous complex type的 Java 类。 * * <p>以下模式片段指定包含在此类中的预期内容。 * * <pre> * &lt;complexType> * &lt;complexContent> * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * &lt;sequence> * &lt;element name="UserToken" type="{http://www.w3.org/2001/XMLSchema}string"/> * &lt;element name="IFaceCode" type="{http://www.w3.org/2001/XMLSchema}string"/> * &lt;element name="BatchNumber" type="{http://www.w3.org/2001/XMLSchema}string"/> * &lt;element name="Data" type="{http://www.w3.org/2001/XMLSchema}string"/> * &lt;/sequence> * &lt;/restriction> * &lt;/complexContent> * &lt;/complexType> * </pre> * * */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { 
   
    "userToken",
    "iFaceCode",
    "batchNumber",
    "data"
})
@XmlRootElement(name = "PlmWsMainRequest")
public class PlmWsMainRequest { 
   

    @XmlElement(name = "UserToken", required = true)
    protected String userToken;
    @XmlElement(name = "IFaceCode", required = true)
    protected String iFaceCode;
    @XmlElement(name = "BatchNumber", required = true)
    protected String batchNumber;
    @XmlElement(name = "Data", required = true)
    protected String data;

    /** * 获取userToken属性的值。 * * @return * possible object is * {@link String } * */
    public String getUserToken() { 
   
        return userToken;
    }

    /** * 设置userToken属性的值。 * * @param value * allowed object is * {@link String } * */
    public void setUserToken(String value) { 
   
        this.userToken = value;
    }

    /** * 获取iFaceCode属性的值。 * * @return * possible object is * {@link String } * */
    public String getIFaceCode() { 
   
        return iFaceCode;
    }

    /** * 设置iFaceCode属性的值。 * * @param value * allowed object is * {@link String } * */
    public void setIFaceCode(String value) { 
   
        this.iFaceCode = value;
    }

    /** * 获取batchNumber属性的值。 * * @return * possible object is * {@link String } * */
    public String getBatchNumber() { 
   
        return batchNumber;
    }

    /** * 设置batchNumber属性的值。 * * @param value * allowed object is * {@link String } * */
    public void setBatchNumber(String value) { 
   
        this.batchNumber = value;
    }

    /** * 获取data属性的值。 * * @return * possible object is * {@link String } * */
    public String getData() { 
   
        return data;
    }

    /** * 设置data属性的值。 * * @param value * allowed object is * {@link String } * */
    public void setData(String value) { 
   
        this.data = value;
    }

}

(4)PlmWsMainResponse.java

//
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.7 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2021.01.13 时间 05:01:43 PM CST 
//


package org.tempuri;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/** * <p>anonymous complex type的 Java 类。 * * <p>以下模式片段指定包含在此类中的预期内容。 * * <pre> * &lt;complexType> * &lt;complexContent> * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * &lt;sequence> * &lt;element name="ResponseData" type="{http://www.w3.org/2001/XMLSchema}string"/> * &lt;element name="ReturnCode" type="{http://www.w3.org/2001/XMLSchema}string"/> * &lt;element name="ReturnMessage" type="{http://www.w3.org/2001/XMLSchema}string"/> * &lt;/sequence> * &lt;/restriction> * &lt;/complexContent> * &lt;/complexType> * </pre> * * */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { 
   
    "responseData",
    "returnCode",
    "returnMessage"
})
@XmlRootElement(name = "PlmWsMainResponse")
public class PlmWsMainResponse { 
   

    @XmlElement(name = "ResponseData", required = true)
    protected String responseData;
    @XmlElement(name = "ReturnCode", required = true)
    protected String returnCode;
    @XmlElement(name = "ReturnMessage", required = true)
    protected String returnMessage;

    /** * 获取responseData属性的值。 * * @return * possible object is * {@link String } * */
    public String getResponseData() { 
   
        return responseData;
    }

    /** * 设置responseData属性的值。 * * @param value * allowed object is * {@link String } * */
    public void setResponseData(String value) { 
   
        this.responseData = value;
    }

    /** * 获取returnCode属性的值。 * * @return * possible object is * {@link String } * */
    public String getReturnCode() { 
   
        return returnCode;
    }

    /** * 设置returnCode属性的值。 * * @param value * allowed object is * {@link String } * */
    public void setReturnCode(String value) { 
   
        this.returnCode = value;
    }

    /** * 获取returnMessage属性的值。 * * @return * possible object is * {@link String } * */
    public String getReturnMessage() { 
   
        return returnMessage;
    }

    /** * 设置returnMessage属性的值。 * * @param value * allowed object is * {@link String } * */
    public void setReturnMessage(String value) { 
   
        this.returnMessage = value;
    }

}

3、编写webservice(主要是业务逻辑)
springWS\src\main\java\com\example\demo\webservice\PlmEndPoint.java

package com.example.demo.webservice;


import org.tempuri.PlmWsMainResponse;
import org.tempuri.PlmWsMainRequest;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import java.util.Date;

@Endpoint
public class PlmEndPoint { 
   
    private static final String NAMESPACE_URI = "http://tempuri.org/";
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "PlmWsMainRequest")
    @ResponsePayload
    public PlmWsMainResponse responsePimData(@RequestPayload PlmWsMainRequest request) { 
   
        Date startTime = new Date();
        PlmWsMainResponse response = new PlmWsMainResponse();
        String batchNumber = request.getBatchNumber();
        String iFaceCode = request.getIFaceCode();
        String xmlInfo =request.getData();
        System.out.println(xmlInfo);

        response.setReturnCode("S");
        response.setReturnMessage("sdfsdfsd");
        response.setResponseData("111");
        return response;
    }
}

4、发布服务,注意:这个发布类最好与启动类同一目录下

package com.example.demo;


import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter { 
   
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) { 
   
        System.out.println("ServletRegistrationBean服务启动");
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet,"/ws/wsapi/*");

    }
    @Bean(name = "plm")
    public DefaultWsdl11Definition mesWsdl11Definition(XsdSchema mesSchema) { 
   
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("PlmPort");
        wsdl11Definition.setLocationUri("/ws/wsapi/plm");
        wsdl11Definition.setSchema(mesSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema mesSchema() { 
   
        return new SimpleXsdSchema(new ClassPathResource("pim.xsd"));
    }

}

3、启动运行启动类

3.1访问http://localhost:8080/ws/wsapi/plm.wsdl,如下图所示,webservice服务端即发布完成

在这里插入图片描述

二、创建客户端

1、创建一个简单的spring initializr项目
2、打开服务端的服务(启动启动类)
3、指定文件夹,路径在项目下,运行命令
wsimport -keep http://localhost:8080/ws/wsapi/plm.wsdl(本人生成的路径:interfaceTest\src\main\java),运行命令后会在指定文件下生成客户端代码,如下图
在这里插入图片描述
4、创建一个main类,进行测试

package com.example.demo.mainTest;


import org.apache.logging.log4j.util.Base64Util;
import org.tempuri.*;

import static org.bouncycastle.crypto.tls.ConnectionEnd.client;

public class SpringWSTest { 
   
    public static void main(String[] args) { 
   
        PlmPortService plmPortService=new PlmPortService();
        PlmPort plmPortSoap11 = plmPortService.getPlmPortSoap11();
        PlmWsMainRequest plmWsMainRequest=new PlmWsMainRequest();
        plmWsMainRequest.setBatchNumber("batchNum");
        plmWsMainRequest.setData(Base64Util.encode("data"));
        plmWsMainRequest.setIFaceCode("IFACE_CODE");
        plmWsMainRequest.setUserToken("token");
        PlmWsMainResponse pimWsMainResponse = plmPortSoap11.plmWsMain(plmWsMainRequest);
System.out.println(pimWsMainResponse.getReturnMessage());       
System.out.println(pimWsMainResponse.getReturnCode());       System.out.println(pimWsMainResponse.getReturnData());
    }
}

运行该main方法,控制台如下显示时,yeah,表示成功,
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186479.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 15款android设备上的代码编辑器

    15款android设备上的代码编辑器如果你希望你的Android设备,如智能手机和平板电脑,在任何时间和任何地方都能够编写代码,那么,不妨看看下面我将介绍的15款Android代码编辑器,它们必将成为你的理想工具。1.DeuterIDE一款基于移动开发的文本编辑器,在线编译器,内置支持Android的集成开发环境。它的虚拟按键可提供常用的编程符号和语法高亮功能,并支持超过40种编程语言,从而能让你在旅途中写代码变得从未如此简单。

    2022年6月16日
    65
  • linux下卸载jdk1.7安装jdk1.8「建议收藏」

    linux下卸载jdk1.7安装jdk1.8「建议收藏」卸载jdk1.7查询java安装版本java-version查询java的安装目录echo$JAVA_HOME我以前在/etc/profile里配置了全局的JAVA_HOME、CLASSPATH和PATH,针对我这种情况想卸载的话,必须把下边这些语句删除vi/etc/profile/i进入编辑,删除之后:wq保存退出.这样的话,你的jdk1.7实际…

    2022年9月25日
    0
  • Python基础知识总结[通俗易懂]

    Python基础知识总结[通俗易懂]一、Python的优缺点优点:1.简单,易学,免费,开源2.高级语言,解释型语言3.可移植性,可拓展性,可读性4.面向对象,丰富的库缺点:1.执行效率慢2.GIL锁限制并发

    2022年7月5日
    19
  • databus 支持oracle么,Databus[通俗易懂]

    databus 支持oracle么,Databus[通俗易懂]系统如果要应付大规模的请求,一条必经之路就是数据库的分割,单服务器的性能早晚都会成为负载的短板。而数据库分割,通常有Master/Salve或者集群Cluster的方式,这些方式通常都是基于同种类型的数据。对于一个庞大的多类型数据库的系统,在不同的数据库之间(甚至是不同地理位置的机房间)保持数据的同步,需要更复杂的解决方案。LinkedIn良心开源了内部的一个项目Databus,正是解决这个问题的…

    2022年10月17日
    0
  • QMap容器小知识

    QMap容器小知识1便捷的遍历方法示例QMap&lt;QString,int&gt;map;…foreach(intvalue,map)cout&lt;&lt;value&lt;&lt;endl;2判断是否包含某个字段接口boolcontains(constKey&amp;key)const3获取指定字段的值,没有则给与默认值接口co…

    2022年5月30日
    39
  • Ofbiz入门

    Ofbiz入门1 配置 JDK 2 Mysql 本地安装 3

    2025年7月15日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号