基于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>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Spring Boot可以使用JAX-WS或者Spring Web Services(Spring-WS)来调用SOAP Web Service接口,也可以使用RestTemplate来调用RESTful Web Service接口。 以下是使用Spring-WS调用SOAP Web Service接口的步骤: 1. 引入Spring-WS和JAXB相关依赖 ```xml <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>3.0.0</version> </dependency> ``` 2. 配置WebServiceTemplate 在配置类中添加WebServiceTemplate的Bean,并设置WebServiceTemplate的Marshaller和Unmarshaller,这里使用Jaxb2Marshaller ```java @Configuration public class WebServiceConfig { @Bean public Jaxb2Marshaller marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("com.example.webservice.demo.wsdl"); return marshaller; } @Bean public WebServiceTemplate webServiceTemplate() { WebServiceTemplate template = new WebServiceTemplate(); template.setMarshaller(marshaller()); template.setUnmarshaller(marshaller()); template.setDefaultUri("http://localhost:8080/ws"); return template; } } ``` 3. 调用WebService 使用WebServiceTemplate的marshalSendAndReceive方法来发送SOAP请求并接收响应,示例代码如下: ```java @Autowired private WebServiceTemplate webServiceTemplate; public void callWebService() { GetCountryRequest request = new GetCountryRequest(); request.setName("Spain"); GetCountryResponse response = (GetCountryResponse) webServiceTemplate.marshalSendAndReceive(request); System.out.println(response.getCountry().getCapital()); } ``` 以上就是使用Spring-WS调用SOAP Web Service接口的步骤。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值