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

概述

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

详细

一、创建springboot项目

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

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. 启动应用程序,观察控制台日志。出现如下日志即表示服务发布成功。

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

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

五、生成客户端代码(使用的是IDEA编辑器),通过客户端访问webservice接口

  1. 新建一个Java项目。

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

3.

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方法、查看接口调用

客户端:

说明接口已经成功调用。

服务端:

七、项目结构

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

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

在spring boot微服务中使用JWS发布webService的更多相关文章

  1. 【原创】Docker容器及Spring Boot微服务应用

    Docker容器及Spring Boot微服务应用 1 什么是Docker 1.1 Docker的出现 问题一:项目实施环境复杂问题 传统项目实施过程中经常会出现“程序在我这跑得好好的,在你那怎么就不 ...

  2. 【spring boot】spring cloud下spring boot微服务启动没有报错,但是访问访问不到

    spring cloud下spring boot微服务启动没有报错,但是访问访问不到 解决方法: 可能是端口被占用了,但是依旧启用成功了. 更改一下项目启用的端口号,再重新启动查看是否可以正常访问.

  3. Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警

    Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警 一.添加依赖 1.1 Actuator 的 /prometheus端点 二.Prometheus 配置 部 ...

  4. Spring Cloud微服务中网关服务是如何实现的?(Zuul篇)

    导读 我们知道在基于Spring Cloud的微服务体系中,各个微服务除了在内部提供服务外,有些服务接口还需要直接提供给客户端,如Andirod.IOS.H5等等. 而一个很尴尬的境地是,如果直接将提 ...

  5. 基于Centos7.4搭建prometheus+grafana+altertManger监控Spring Boot微服务(docker版)

    目的:给我们项目的微服务应用都加上监控告警.在这之前你需要将 Spring Boot Actuator引入 本章主要介绍 如何集成监控告警系统Prometheus 和图形化界面Grafana 如何自定 ...

  6. Spring Boot微服务架构入门

    概述 还记得在10年毕业实习的时候,当时后台三大框架为主流的后台开发框架成软件行业的标杆,当时对于软件的认识也就是照猫画虎,对于为什么会有这么样的写法,以及这种框架的优势或劣势,是不清楚的,Sprin ...

  7. Spring Boot微服务如何集成fescar解决分布式事务问题?

    什么是fescar? 关于fescar的详细介绍,请参阅fescar wiki. 传统的2PC提交协议,会持有一个全局性的锁,所有局部事务预提交成功后一起提交,或有一个局部事务预提交失败后一起回滚,最 ...

  8. Spring Cloud 微服务中搭建 OAuth2.0 认证授权服务

    在使用 Spring Cloud 体系来构建微服务的过程中,用户请求是通过网关(ZUUL 或 Spring APIGateway)以 HTTP 协议来传输信息,API 网关将自己注册为 Eureka ...

  9. Spring Boot微服务框架的搭建

    (1)spring boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...

随机推荐

  1. 数学图形之贝塞尔(Bézier)曲面

    前面章节中讲了贝塞尔(Bézier)曲线,而贝塞尔曲面是对其多一个维度的扩展.其公式依然是曲线的公式: . 而之所以由曲线变成曲面,是将顶点横向连了再纵向连. 很多计算机图形学的教程都会有贝塞尔曲面的 ...

  2. C++两个矩阵相乘

    /*编程求两个矩阵相乘的结果.输入第一行是整数m,n,表示第一个矩阵式m行n列的:然后是一个m * n的矩阵.再下一行的输入时整数p,q,表示下一个矩阵p行,q列的(n=p);然后就是一个p行q列的矩 ...

  3. android 数据加密——加密的概述

    数据加密又称密码学,它是一门历史悠久的技术,指通过加密算法和加密密钥将明文转变为密文,而解密则是通过解密算法和解密密钥将密文恢复为明文.数据加密目前仍是计算机系统对信息进行保护的一种最可靠的办法.它利 ...

  4. ML、DL相关资源

    1. http://x-algo.cn/index.php/category/nlp/

  5. [转]0.python:scikit-learn基本用法

    感谢百小度治哥,该文原地址:here 经Edwin Chen的推荐,认识了scikit-learn这个非常强大的python机器学习工具包.这个帖子作为笔记.(其实都没有笔记的意义,因为他家文档做的太 ...

  6. springMVC 防重校验(拦截器)

    <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.bitspace. ...

  7. BigDecimal 的幂次方运算

    public static void main(String[] args){ BigDecimal bg1, bg2; bg1 = new BigDecimal("200000.45&qu ...

  8. hdu Boring count(BestCode round #11)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. 关于Puppet不得不说的故事

    Puppet对于做DevOps的同学来说,是个熟悉的名字,但仍有许多人并不了解它.那么我先来简单介绍一下:Puppet是由Puppetlabs公司开发的系统管理框架和工具集,被用于IT服务的自动化管理 ...

  10. myeclipse单元测试

    步骤:1.新建new-other 2.选择Junit Test Case 3.注意红线圈出部分 4.选择需要测试的方法5.Finish后出现如下 示例代码: package com.jrgc.dao; ...