WebService学习(一)

使用工具:

IDEA 2020.3.2、JDK1.8_64x

WebService介绍

简单说   WebService 就是一个服务 ,别的程序能访问并使用这项在线服务。(提供服务和wsdl,调用解析你来搞)
api  就相当于一个别人编译好的类,你直接引用后使用(提供接口和服务,调用返回简单点);

常见的两种WebService处理方式:

1:基于WSDL / SOAP 的方式;

2:Rest 方式。

方式1是比较正统的,客户端调用必须先取得WSDL文件,然后生成调用的API才可以使用;方式2在后续说明。

本次学习使用 WSDL基本方式,在SpringBoot微服务中使用JWS(JAVA Web Service)发布webService,目标:实现一个简单例子 客户端和服务端的一次简单示例。

步骤:

1、使用IDEA创建SpringBoot工程+web

目录结构如下

2、测试新建SpringBoot web,在默认生成的DemoApplication同目录下新建TestController类,用注解@RestController标注,创建测试方法index,如下图:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping(value="/index")
    public String index(){
        return "Hello World!";
    }
}

启动项目,访问 http://localhost:8080/index,如显示 Hello World! 则标识正常

3、编写WebService接口 WebServiceInterface,使用jws的 WebService,WebMenthod,WebParam

package com.example.demo.webService.inter;

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

@WebService
public interface WebServiceInterface {

    @WebMethod
    String hiWebService(@WebParam(name="HiWebService") String str);

}

 4、实现接口方法。并且在该类上使用javax.jws.WebService注解和org.springframework.stereotype.Service注解,是该类即是一个webService接口服务类又是作为一个可以被spring管理的bean;

package com.example.demo.webService.impl;

import com.example.demo.webService.inter.WebServiceInterface;
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 WebServiceImpl implements WebServiceInterface{

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

    @Override
    public String hiWebService(String str) {
        String msg = "WebServiceImpl获取Str>>"+str;
        log.info(msg);
        return msg;
    }
}

 5、BeforeStartUp

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

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

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

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

package com.example.demo.webService;

import com.example.demo.webService.inter.WebServiceInterface;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import javax.xml.ws.Endpoint;

/**
 * 使用ApplicationContext事件机制来完成webService接口的自动发布。
 *
 * 使用ApplicationListener监听ContextRefreshedEvent事件。ContextRefreshedEvent就是在ApplicationContext被初始化(所有的bean被成功装载,后处理bean被检测或成功激活,所有的singleton bean被实例化,ApplicationConte容器已就绪可用)或刷新时,该事件被发布。
 *
 * 在webservice目录下新建一个类BeforeStartUp.java,实现ApplicationListene接口。
 *
 * 重写onApplicationEvent方法。在该方法中发布webService服务。使用javax.xml.ws.Endpoint的publish方法发布。该方法有两个参数,第一个是要使用的地址和传输/协议的URI。URI必须使用SOAP 1.1 / HTTP 绑定。第二个参数是webService的接口实现类。
 *
 * 该类也必须使用@Service,@Component或者@Configuration注解被spring管理,使其可以被自动装载。
 */
@Service
public class BeforeStartUp implements ApplicationListener<ContextRefreshedEvent> {

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

    private static String address = "http://localhost:8002/ws/hello";

    @Autowired
    private WebServiceInterface webServiceInterface;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        Endpoint.publish(address,webServiceInterface);
        log.info("WebService 服务发布成功");
        log.info("wsdl地址:"+address+"?wsdl");
    }
}

6、启动服务,如下截图》》服务端启动成功

 

 7、客户端,同样SpringBoot工程

8、选择webService,选择generate java code from wsdl (或者 工程名字上右键,若都没有请查看 选不到 generate java code from wsdl

9、编写Main方法进行调用 

package com.example.clientdemo.client;

import client.WebServiceImpl;
import client.WebServiceImplService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Run {

    private static Log log = LogFactory.getLog(Run.class);

    public static void main(String[] args) {
        WebServiceImpl service = new WebServiceImplService().getWebServiceImplPort();
        String str = service.hiWebService("Client:Hi webService!");
        log.info(str);
    }
}

 10、运行Main方法

源码:源码GitHub路径

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我也不清楚

有钱的捧个钱场,(~ ̄▽ ̄)~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值