webservice服务端搭建_webserviceclient

webservice服务端搭建_webserviceclient简介WebService它是一种跨编程语言和跨操作系统平台的远程调用技术。实现不同系统,进程间,任何地点的数据交换,webservice是跨语言技术。webservice是基于HTTP与XML的技术。采用标准SOAP(SimpleObjectAccessProtocol)协议传输,so

大家好,欢迎来到IT知识分享网。

简介

WebService 它是一种跨编程语言和跨操作系统平台的远程调用技术。

实现不同系统,进程间,任何地点的数据交换,webservice是跨语言技术。webservice是基于HTTP与XML的技术。

采用标准SOAP(Simple Object Access Protocol)协议传输,soap属于W3C标准。Soap协议是基于http的应用层协议,传输是xml数据。

采用wsdl作为描述语言即webservice使用说明书,wsdl属于w3c标准。

一、Soap协议

  soap是一种基于xml格式访问网络服务的协议,通过http来进行信息交换。 一次WebService的调用,不是方法的调用,而是soap消息之间的输入和输出

webservice服务端搭建_webserviceclient

二、客户端调用步骤

webservice服务端搭建_webserviceclient

 

三、WSDL报文总体概述

webservice服务端搭建_webserviceclient

 

第一部分:definitions

webservice服务端搭建_webserviceclient

 

第二部分:types

定义了接口中的数据参数和返回值

webservice服务端搭建_webserviceclient

代码:

@WebService(name = "HelloService", //接口名称
            targetNamespace = "http://server.webservice.example.com/")

public interface HelloService {

    @WebMethod
    @WebResult(name = "backMethod")
    String sayHello(@WebParam (name = "username") String name);

}

第三部分:message

webservice服务端搭建_webserviceclient

 

第四部分:portType

prottype = 接口名, 该标签中可以看到服务暴露出来的接口名称和方法  

operation 指的是接口中的方法,可以为多个

 

webservice服务端搭建_webserviceclient

 

代码:


@WebService(name = "HelloService", //接口名称
            targetNamespace = "http://server.webservice.example.com/")

public interface HelloService {

    @WebMethod
    @WebResult(name = "backMethod")
    String sayHello(@WebParam (name = "username") String name);

}

第五部分:binding

binding :  那么也就是给远程调用方法设置消息格式和协议细节

webservice服务端搭建_webserviceclient

 

第六部分:service

service :说明哪个地址url上绑定了哪个服务

地址:配置文件yml中声明了一个 : /maky + CXF配置类中,在调用对外发布的方法时,里面传入的参数  :   即:localhost:8080//maky/helloMaky?wsdl


cxf:
  path: /maky

webservice服务端搭建_webserviceclient

 

基于CXF服务端代码

接口:

//服务端代码:
package com.example.webservice.server;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(name = "HelloService", //接口名称
            targetNamespace = "http://server.webservice.example.com/")

public interface HelloService {

    @WebMethod
    @WebResult(name = "backMethod") //自定义的返回的response方法,可以在WSDL的Type标签中看到
    String sayHello(@WebParam (name = "username") String name);  

}

实现类:

package com.example.webservice.serverImpl;

import com.example.webservice.server.HelloService;
import org.springframework.stereotype.Service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;


@WebService(serviceName = "HelloService", //对外发布的服务名
            //指定名称空间,通常为包名反转
            targetNamespace = "http://server.webservice.example.com/",
            //服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
            endpointInterface = "com.example.webservice.server.HelloService"
            )
@Service
public class HelloServiceImpl implements HelloService{

    @Override
    public String sayHello(@WebParam(name = "username") String name) {

        return "Songwl"+ name;
    }
}

CXF的配置类:

package com.example.webservice.config;

import com.example.webservice.serverImpl.HelloServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CXFConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private HelloServiceImpl helloServiceImpl;

    //发布服务
    @Bean
    public Endpoint helloServer() {

        //解读:http://localhost:8080//maky/publicHello?wsdl
        //maky: 是yml配置文件中path 声明的值
        //helloMaky: 是CXF对外发布时public中的地址
        EndpointImpl endpoint = new EndpointImpl(bus, helloServiceImpl);
        endpoint.publish("helloMaky");   //对外提供的地址: http://localhost:8080//maky/helloMaky?wsdl
        return endpoint;
    }
}

配置文件yml:

cxf:
  path: /maky

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.7</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<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.apache.cxf</groupId>
			<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
			<version>3.5.0</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
	</dependencies>
</project>

工程列表:

webservice服务端搭建_webserviceclient 

 

WSDL 报文:

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://server.webservice.example.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloService" targetNamespace="http://server.webservice.example.com/">
    <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://server.webservice.example.com/" elementFormDefault="unqualified" targetNamespace="http://server.webservice.example.com/" version="1.0">
        <xs:element name="sayHello" type="tns:sayHello"/>
        <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
        <xs:complexType name="sayHello">
        <xs:sequence>
        <xs:element minOccurs="0" name="username" type="xs:string"/>
        </xs:sequence>
        </xs:complexType>
        <xs:complexType name="sayHelloResponse">
        <xs:sequence>
        <xs:element minOccurs="0" name="backMethod" type="xs:string"/>
        </xs:sequence>
        </xs:complexType>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="sayHello">
        <wsdl:part element="tns:sayHello" name="parameters"> </wsdl:part>
        </wsdl:message>
        <wsdl:message name="sayHelloResponse">
        <wsdl:part element="tns:sayHelloResponse" name="parameters"> </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="HelloService">
        <wsdl:operation name="sayHello">
        <wsdl:input message="tns:sayHello" name="sayHello"> </wsdl:input>
        <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"> </wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="HelloServiceSoapBinding" type="tns:HelloService">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="sayHello">
        <soap:operation soapAction="" style="document"/>
        <wsdl:input name="sayHello">
        <soap:body use="literal"/>
        </wsdl:input>
        <wsdl:output name="sayHelloResponse">
        <soap:body use="literal"/>
        </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="HelloService">
        <wsdl:port binding="tns:HelloServiceSoapBinding" name="HelloServiceImplPort">
        <soap:address location="http://localhost:8080//maky/helloMaky"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

 

Java对象与XML转换

JAXB = Java Architecture for XML Binding

webservice服务端搭建_webserviceclient

UnMarshaller : 将xml数据反序列化,生成java对象

Marshaller: 将java对象生成xml数据

webservice服务端搭建_webserviceclient

 

Java转XML

package com.example.webservice.test;

import com.example.webservice.pojo.Book;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class XmlToJava {

    public static void myMarshaller() throws JAXBException {

        //Java对象转xml报文
        JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
        marshaller.marshal(new Book(11,"Ma",999999999d),System.out);

    }

    public static void main(String[] args) throws JAXBException {
        myMarshaller();
    }
}


//实体类
package com.example.webservice.pojo;

import com.sun.xml.txw2.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Book {

    private int id;
    private String name;
    private double priece;

    public Book() {
    }

    public Book(int id, String name, double priece) {
        this.id = id;
        this.name = name;
        this.priece = priece;
    }

    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 double getPriece() {
        return priece;
    }

    public void setPriece(double priece) {
        this.priece = priece;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", priece=" + priece +
                '}';
    }
}

转换后的xml报文:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<book>
    <id>11</id>
    <name>Ma</name>
    <priece>9.9999999E7</priece>
</book>

 

XML转Java对象 : 使用的是UnMarshaller 对象进行处理

public static void myUnMarshaller() throws JAXBException {

        //Java对象转xml报文
        //xml报文:
        String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><book><id>11</id><name>Moon</name><priece>9.99999999E8</priece></book>";
        JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Book book = (Book)unmarshaller.unmarshal(new StringReader(xmlString));
        System.out.println(book.toString());
        if("Moon".equals(book.getName())){
            System.out.println("MakyAndMoon");
        }
    }

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/28278.html

(0)

相关推荐

发表回复

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

关注微信