spring-boot(十)webservice配置

分类:spring-boot
阅读:786
作者: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

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web-services</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.apache.cxf</groupId>
  7. <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
  8. <version>3.1.7</version>
  9. </dependency>

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

  1. package hello.jax.ws;
  2. /**
  3. * @author majinding888@foxmail.com
  4. * @date 2016-12-30 下午5:28:59
  5. */
  6. import javax.jws.WebMethod;
  7. import javax.jws.WebParam;
  8. import javax.jws.WebResult;
  9. import javax.jws.WebService;
  10. @WebService(name = "Hello")
  11. public interface Hello {
  12. @WebResult()
  13. @WebMethod()
  14. String sayHello(@WebParam(name = "myname") String myname);
  15. }

新建一个实现类 HelloPortImpl.java

  1. package hello.jax.ws;
  2. import javax.jws.WebService;
  3. /**
  4. * @author majinding888@foxmail.com
  5. * @date 2016-12-30 下午5:29:29
  6. */
  7. @WebService
  8. public class HelloPortImpl implements Hello {
  9. public String sayHello(String myname) {
  10. return "Hello, Welcome to CXF Spring boot " + myname + "!!!";
  11. }
  12. }

新建 WebServiceConfig.java 配置,启用ws

  1. package hello.jax.ws;
  2. /**
  3. * @author majinding888@foxmail.com
  4. * @date 2016-12-30 下午5:30:09
  5. */
  6. import javax.xml.ws.Endpoint;
  7. import org.apache.cxf.Bus;
  8. import org.apache.cxf.jaxws.EndpointImpl;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. @Configuration
  13. public class WebServiceConfig {
  14. @Autowired
  15. private Bus bus;
  16. @Bean
  17. public Endpoint endpoint() {
  18. EndpointImpl endpoint = new EndpointImpl(bus, new HelloPortImpl());
  19. endpoint.publish("/Hello");
  20. return endpoint;
  21. }
  22. }

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

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

QQ截图20161230180643.png

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