在spring boot微服务中使用JWS发布webService

发布时间:2018-11-22
 
技术:Java+spring+maven
 

概述

在springboot微服务中使用JWS发布webService,在服务启动时自动发布webservice接口。

详细

一、创建springboot项目

1.新建一个springboot项目,不需要添加任何依赖。

image.png

2.在启动类中编写一个接口,然后启动项目,访问http://localhost:8080测试项目是否存在问题。

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

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

   @RequestMapping("/")
   public String hello(){
      return "hello spring boot";
   }
}
二、编写webservice接口
  1. 在com.example.demo路径下新建webservice目录,并在该目录下新建webservice接口TestService.java,编写一个webService接口方法。

  2. 在该接口上使用javax.jws.WebServcie注解;在方法上使用javax.jws.WebMethod注解。

package com.example.demo.webservice;

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

@WebService
public interface TestService {

    @WebMethod
    String hiWebService(@WebParam(name = "hi") String s);
}

3.在相同路径下新建TestService接口的实现类TestServiceImpl.java,实现接口方法。并且在该类上使用javax.jws.WebService注解和org.springframework.stereotype.Service注解,是该类即是一个webService接口服务类又是作为一个可以被spring管理的bean。

package com.example.demo.webservice;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import javax.jws.WebService;

@WebService
@Service
public class TestServiceImpl implements TestService{

    private Log log = LogFactory.getLog(TestServiceImpl.class);
    
    @Override
    public String hiWebService(String s) {
        String msg = "获取内容:"+s;
        log.info(msg);
        return msg;
    }
}
三、启动配置
  1. 使用ApplicationContext事件机制来完成webService接口的自动发布。

    使用ApplicationListener监听ContextRefreshedEvent事件。ContextRefreshedEvent就是在ApplicationContext被初始化(所有的bean被成功装载,后处理bean被检测或成功激活,所有的singleton bean被实例化,ApplicationConte容器已就绪可用)或刷新时,该事件被发布。

  2. 在webservice目录下新建一个类BeforeStartUp.java,实现ApplicationListene接口。

  3. 重写onApplicationEvent方法。在该方法中发布webService服务。使用javax.xml.ws.Endpoint的publish方法发布。该方法有两个参数,第一个是要使用的地址和传输/协议的URI。URI必须使用SOAP 1.1 / HTTP 绑定。第二个参数是webService的接口实现类。

  4. 该类也必须使用@Service,@Component或者@Configuration注解被spring管理,使其可以被自动装载。

package com.example.demo.webservice;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import javax.xml.ws.Endpoint;

@Service
public class BeforeStartUp implements ApplicationListener<ContextRefreshedEvent>{

    private Log log = LogFactory.getLog(BeforeStartUp.class);

//    @Value("${spring.ws.address}")
    private static String address = "http://localhost:8002/ws/hello";

    @Autowired
    private TestService testService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
            Endpoint.publish(address,testService);
            log.info("webService 服务发布成功!!!");
            log.info("wsdl地址:"+address+"?wsdl");
    }
}
四、启动应用,自动发布
  1. 启动应用程序,观察控制台日志。出现如下日志即表示服务发布成功。

    image.png

    点击wsdl地址即可在浏览器上查看该webService的wsdl的内容。

    由于webService是跨平台的。只要通过该wsdl文件就可以生成相应平台上的客户端或者服务端代码。

五、生成客户端代码(使用的是IDEA编辑器),通过客户端访问webservice接口
  1. 新建一个Java项目。

    image.png

  2. 右击client,选择webService,选择generate java code from wsdl

    image.png

3.

image.png

4.点击ok即可生成客户端代码。调用接口跟平常写程序调用方法没什么两样。方法在TestServiceImpl中,获取TestServiceImpl对象的方法在TestServcieImplService中。在main方法中调用:

package main.java;

import main.client.TestServiceImpl;
import main.client.TestServiceImplService;

public class Test {

    public static void main(String[] args){
        TestServiceImpl service = (new TestServiceImplService()).getTestServiceImplPort();
        String s = service.hiWebService("hi webService!");
        System.out.println(s);
    }
}
六、运行main方法、查看接口调用

客户端:

image.png

说明接口已经成功调用。

服务端:

image.png

 

七、项目结构

本例子分为两部分,有客户端,也有webservice端,如下所示:

image.png

 

注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权

转载于:https://www.cnblogs.com/demodashi/p/10492816.html

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值