spring boot實現多個webservice接口

本文使用cxf發佈webservice接口。

一、webservice config配置類

springboot啓動時,會自動加載配置類。配置類應用cxf發佈webservice接口。
注意:下文中的dispatcherServletForWebservice函數,名稱不能是dispatcherServlet,否則會和springboot自己的dispatcherServlet函數重名。

WebserviceConfig.java

package com.jwt.webservicedemo.config;

import com.jwt.webservicedemo.controller.OneServiceImp;
import com.jwt.webservicedemo.controller.TwoServiceImp;
import com.jwt.webservicedemo.interfaces.OneServiceInterface;
import com.jwt.webservicedemo.interfaces.TwoServiceInterface;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class WebserviceConfig
{
    @Bean
    public ServletRegistrationBean dispatcherServletForWebservice()
    {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus()
    {
        return new SpringBus();
    }

    @Bean
    public OneServiceInterface oneService()
    {
        return new OneServiceImp();
    }

    @Bean
    public TwoServiceInterface twoService()
    {
        return new TwoServiceImp();
    }

    @Bean
    public Endpoint endpoint()
    {
        EndpointImpl endpoint = new EndpointImpl(springBus(), oneService());
        endpoint.publish("/OneServiceForTest");
        return endpoint;
    }

    @Bean
    public Endpoint endpoint2()
    {
        EndpointImpl endpoint = new EndpointImpl(springBus(), twoService());
        endpoint.publish("/TwoServiceForTest");
        return endpoint;
    }
}

二、測試接口類

(1)使用@WebService註解:
name:對外發布的接口名
targetNamespace:接口類包名倒序
(2)函數參數使用@WebParam註解:
name:參數對外名稱

OneServiceInterface.java

package com.jwt.webservicedemo.interfaces;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(name = "OneServiceForTest", targetNamespace = "http://interfaces.webservicedemo.jwt.com")
public interface OneServiceInterface {

    public String one(@WebParam(name = "surname") String surname, @WebParam(name = "name") String name);
}

TwoServiceInterface.java

package com.jwt.webservicedemo.interfaces;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(name = "TwoServiceForTest", targetNamespace = "http://interfaces.webservicedemo.jwt.com")
public interface TwoServiceInterface {

    public String two(@WebParam(name = "param") String param);
}

三、接口實現類

使用@WebService註解:
name:對外發布的接口名,保持和接口類一致
targetNamespace:接口類包名倒序,保持和接口類一致
endpointInterface:繼承接口類全路徑

OneServiceImp.java

package com.jwt.webservicedemo.controller;

import com.jwt.webservicedemo.interfaces.OneServiceInterface;

import javax.jws.WebService;

@WebService(name = "OneServiceForTest", targetNamespace = "http://interfaces.webservicedemo.jwt.com",
        endpointInterface = "com.jwt.webservicedemo.interfaces.OneServiceInterface")
public class OneServiceImp implements OneServiceInterface {
    @Override
    public String one(String surname, String name) {
        return surname + " " + name;
    }
}

TwoServiceImp.java

package com.jwt.webservicedemo.controller;

import com.jwt.webservicedemo.interfaces.TwoServiceInterface;

import javax.jws.WebService;

@WebService(name = "TwoServiceForTest", targetNamespace = "http://interfaces.webservicedemo.jwt.com",
        endpointInterface = "com.jwt.webservicedemo.interfaces.TwoServiceInterface")
public class TwoServiceImp implements TwoServiceInterface {
    @Override
    public String two(String param) {
        return " " + param;
    }
}

四、啓動引導類

WebservicedemoApplication

package com.jwt.webservicedemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebservicedemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(WebservicedemoApplication.class, args);
	}

}

五、webservice相關依賴

引入cxf-spring-boot-starter-jaxws即可

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.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.jwt</groupId>
	<artifactId>webservicedemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>webservicedemo</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</artifactId>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
			<version>3.2.6</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

六、wsdl

http://localhost:1234/services/OneServiceForTest?wsdl

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

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://interfaces.webservicedemo.jwt.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="OneServiceImpService" targetNamespace="http://interfaces.webservicedemo.jwt.com">  
  <wsdl:types> 
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://interfaces.webservicedemo.jwt.com" version="1.0">  
      <xs:element name="one" type="tns:one"/>  
      <xs:element name="oneResponse" type="tns:oneResponse"/>  
      <xs:complexType name="one"> 
        <xs:sequence> 
          <xs:element minOccurs="0" name="surname" type="xs:string"/>  
          <xs:element minOccurs="0" name="name" type="xs:string"/> 
        </xs:sequence> 
      </xs:complexType>  
      <xs:complexType name="oneResponse"> 
        <xs:sequence> 
          <xs:element minOccurs="0" name="return" type="xs:string"/> 
        </xs:sequence> 
      </xs:complexType> 
    </xs:schema> 
  </wsdl:types>  
  <wsdl:message name="oneResponse"> 
    <wsdl:part element="tns:oneResponse" name="parameters"></wsdl:part> 
  </wsdl:message>  
  <wsdl:message name="one"> 
    <wsdl:part element="tns:one" name="parameters"></wsdl:part> 
  </wsdl:message>  
  <wsdl:portType name="OneServiceForTest"> 
    <wsdl:operation name="one"> 
      <wsdl:input message="tns:one" name="one"></wsdl:input>  
      <wsdl:output message="tns:oneResponse" name="oneResponse"></wsdl:output> 
    </wsdl:operation> 
  </wsdl:portType>  
  <wsdl:binding name="OneServiceImpServiceSoapBinding" type="tns:OneServiceForTest"> 
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>  
    <wsdl:operation name="one"> 
      <soap:operation soapAction="" style="document"/>  
      <wsdl:input name="one"> 
        <soap:body use="literal"/> 
      </wsdl:input>  
      <wsdl:output name="oneResponse"> 
        <soap:body use="literal"/> 
      </wsdl:output> 
    </wsdl:operation> 
  </wsdl:binding>  
  <wsdl:service name="OneServiceImpService"> 
    <wsdl:port binding="tns:OneServiceImpServiceSoapBinding" name="OneServiceForTestPort"> 
      <soap:address location="http://localhost:1234/services/OneServiceForTest"/> 
    </wsdl:port> 
  </wsdl:service> 
</wsdl:definitions>

http://localhost:1234/services/TwoServiceForTest?wsdl

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

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://interfaces.webservicedemo.jwt.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="TwoServiceImpService" targetNamespace="http://interfaces.webservicedemo.jwt.com">  
  <wsdl:types> 
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://interfaces.webservicedemo.jwt.com" version="1.0">  
      <xs:element name="two" type="tns:two"/>  
      <xs:element name="twoResponse" type="tns:twoResponse"/>  
      <xs:complexType name="two"> 
        <xs:sequence> 
          <xs:element minOccurs="0" name="param" type="xs:string"/> 
        </xs:sequence> 
      </xs:complexType>  
      <xs:complexType name="twoResponse"> 
        <xs:sequence> 
          <xs:element minOccurs="0" name="return" type="xs:string"/> 
        </xs:sequence> 
      </xs:complexType> 
    </xs:schema> 
  </wsdl:types>  
  <wsdl:message name="two"> 
    <wsdl:part element="tns:two" name="parameters"></wsdl:part> 
  </wsdl:message>  
  <wsdl:message name="twoResponse"> 
    <wsdl:part element="tns:twoResponse" name="parameters"></wsdl:part> 
  </wsdl:message>  
  <wsdl:portType name="TwoServiceForTest"> 
    <wsdl:operation name="two"> 
      <wsdl:input message="tns:two" name="two"></wsdl:input>  
      <wsdl:output message="tns:twoResponse" name="twoResponse"></wsdl:output> 
    </wsdl:operation> 
  </wsdl:portType>  
  <wsdl:binding name="TwoServiceImpServiceSoapBinding" type="tns:TwoServiceForTest"> 
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>  
    <wsdl:operation name="two"> 
      <soap:operation soapAction="" style="document"/>  
      <wsdl:input name="two"> 
        <soap:body use="literal"/> 
      </wsdl:input>  
      <wsdl:output name="twoResponse"> 
        <soap:body use="literal"/> 
      </wsdl:output> 
    </wsdl:operation> 
  </wsdl:binding>  
  <wsdl:service name="TwoServiceImpService"> 
    <wsdl:port binding="tns:TwoServiceImpServiceSoapBinding" name="TwoServiceForTestPort"> 
      <soap:address location="http://localhost:1234/services/TwoServiceForTest"/> 
    </wsdl:port> 
  </wsdl:service> 
</wsdl:definitions>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章