spring-boot(十)webservice配置

分类:spring-boot
阅读:899
作者:majingjing
发布:2016-12-30 18:08

webservices在前几年是非常流行的,后来rest-api出世后简直是颠覆了ws的天下. 但是在项目中还是有小部分会使用到ws,这里就简单介绍下spring-boot添加ws支持

官方的支持也是只给了简短的说明 QQ截图20161230175910.png 想了解更多的支持信息请访问 spring-ws 相关的文档

在这里只是简单介绍如何快速搭建一个ws项目, 案例代码在 spring-boot(九)websocket配置 基础之上改造

项目结构图 QQ截图20161230180216.png

在pom中添加ws的依赖, 本案例通过cxf来搭建ws

<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.1.7</version>
</dependency>

在包 hello.jax.ws 下 新建一个接口 Hello.java

package hello.jax.ws;

/**
 * @author majinding888@foxmail.com
 *  @date 2016-12-30 下午5:28:59
 */
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(name = "Hello")
public interface Hello {

	@WebResult()
	@WebMethod()
	String sayHello(@WebParam(name = "myname") String myname);
}

新建一个实现类 HelloPortImpl.java

package hello.jax.ws;

import javax.jws.WebService;

/**
 * @author majinding888@foxmail.com
 * @date 2016-12-30 下午5:29:29
 */
@WebService
public class HelloPortImpl implements Hello {

	public String sayHello(String myname) {
		return "Hello, Welcome to CXF Spring boot " + myname + "!!!";
	}

}

新建 WebServiceConfig.java 配置,启用ws

package hello.jax.ws;

/**
 * @author majinding888@foxmail.com
 *  @date 2016-12-30 下午5:30:09
 */
import javax.xml.ws.Endpoint;

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;

@Configuration
public class WebServiceConfig {

	@Autowired
	private Bus bus;

	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpoint = new EndpointImpl(bus, new HelloPortImpl());
		endpoint.publish("/Hello");
		return endpoint;
	}
}


到此ws环境就搭建完成,启动浏览器 访问 http://localhost:666/services/Hello?wsdl

(切记需要在发布路径前面增加services这个path,如果要自定义地址需要在配置文件中增加cxf.path=/webservice)

QQ截图20161230180643.png

源代码附件地址: my-springboot-10.zip