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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • oracle 修改表名

    oracle 修改表名ALTER TABLE AA RENAME TO BB;

    2022年5月13日
    39
  • rst markdown_r语言markdown导出

    rst markdown_r语言markdown导出markdown文件转RST文件时遇到的一些问题最近需要把一些markdown文件转成RST文件,第一次接触RST文件,使用中会有一些语法问题需要注意。在这里做个记录。转化工具我们可以先采用工具对整体做个初步的转换。因为转换工具不能做到完美转换,在初步转换完成后再根据实际的显示情况进行下一步的调整。这里推荐一个网站:MD在线转换成RST如果不注册的话,每天最多可以转化10个文件。后续调整转换后的大体格式是正确的,包括不同级别的标题,一些加粗斜体显示等,但是很多细节还是要自己调整的。1.网页链

    2022年9月30日
    3
  • 矩阵特征值分解(EDV)与奇异值分解(SVD)在机器学习中的应用

    目录特征分解定义(来自百度百科词条:特征分解)特征分解(Eigendecomposition),又称谱分解(Spectraldecomposition)是将矩阵分解为由其特征值和特征向量表示的矩阵之积的方法。需要注意只有对可对角化矩阵才可以施以特征分解。(来自百度百科词条:矩阵特征值)什么是特征值,特征向量?设A是n阶方阵,如果数λ和n维非零列向量x使关系式Ax=λx成立,那么这样的数λ称为矩阵A特征值,非零向量x称为A的对应于特征值λ的特征向量。式Ax=λx也可写成(A-λE)X=0。这是

    2022年4月5日
    49
  • 腾讯架构大调整!七大事业群变六大事业群

    腾讯架构大调整!七大事业群变六大事业群微信又改版了,为了方便第一时间看到我们的推送,请按照下列操作,设置“置顶”:点击上方蓝色字体“程序员之家”-点击右上角“…”-点击“设为星标”。可以啦,让我们继续相互陪伴…

    2022年5月12日
    60
  • Linux实例初始化系统盘后重新挂载数据盘「建议收藏」

    Linux实例初始化系统盘后重新挂载数据盘「建议收藏」Linux实例初始化系统盘后重新挂载数据盘

    2022年4月24日
    78
  • Spring Aop底层原理详解(利用spring后置处理器实现AOP)

    Spring Aop底层原理详解(利用spring后置处理器实现AOP)写在前面:对于一个java程序员来说,相信绝大多数都有这样的面试经历,面试官问:你知道什么是aop吗?谈谈你是怎么理解aop的?等等诸如此类关于aop的问题。当然对于一些小白可能会一脸懵逼;对于一些工作一两年的,可能知道,哦!aop就是面向切面变成,打印日志啊,什么什么的,要是有点学习深度的呢可能会说aop底层实现利用了jdk动态代理,cglib啊什么的。很多时候可能面试就到此打住了,当然,然后也…

    2022年9月16日
    2

发表回复

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

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