Springboot集成WebService

一、介绍

1、介绍

WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互。 其实WebService并不是什么神秘的东西,它就是一个可以远程调用的类,或者说是组件,把你本地的功能开放出去共别人调用。具体的说,Web Service可以让你的网站使用其他网站的资源,比如在网页上显示天气、地图、twitter上的最新动态等等。

2、原理(soa)

在Web Service的体系架构中有三个角色:服务提供者(Service Provider),也叫服务生产者;服务请求者(Service Requester),也叫服务消费者;服务注册中心(Service Register),也叫服务代理,服务提供者在这里发布服务,服务请求者在这里查找服务,获取服务的绑定信息。

二、集成

1、导入

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-web-servicesartifactId>
        dependency>


        
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-frontend-jaxwsartifactId>
            <version>3.1.6version>
        dependency>
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-transports-httpartifactId>
            <version>3.1.6version>
        dependency>

       
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-spring-boot-starter-jaxwsartifactId>
            <version>3.5.2version>
        dependency>

2、创建类

service

package com.zhk.routine.webservice;

import com.zhk.routine.entity.VehicleAlarm;
import com.zhk.routine.redis.Geo;

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

@WebService( targetNamespace = "http://webservice.routine.zhk.com")//这里是你的包名的倒着写
public interface DemoService {
    @WebMethod
    String hello(@WebParam(name = "geo") String geo);
}

impl

package com.zhk.routine.webservice;

import com.zhk.routine.entity.VehicleAlarm;
import com.zhk.routine.redis.Geo;
import org.springframework.stereotype.Service;

import javax.jws.WebService;

@Service
@WebService(serviceName ="DemoService",// 与接口中指定的name一致
        targetNamespace = "http://webservice.routine.zhk.com",// 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.zhk.routine.webservice.DemoService" // 接口地址
)
public class DemoServiceImpl implements DemoService {
    @Override
    public String hello(String geo) {
    //VehicleAlarm是实体类这里就不粘贴出来了下面wsdl介绍会说到VehicleAlarm类包含什么
        return "你好";
    }
}

config配置类

package com.zhk.routine.webservice;

import lombok.SneakyThrows;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {
    @Autowired
    private DemoService demoService;

    /**
     * 注入servlet  bean name不能dispatcherServlet 否则会覆盖dispatcherServlet
     *或者 yml文件配置
     *cxf.path=/cxfServlet
     *
     * @return
     */
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet() {
        //这个是你接口暴漏出的地址
        return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 注册WebServiceDemoService接口到webservice服务
     * @return
     */
    @Bean(name = "DemoServiceEndpoint")
    public Endpoint sweptPayEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), demoService);
 		//绑定后缀名称访问
        endpoint.publish("/ws/api");
        return endpoint;
    }
//这里是调用
    @SneakyThrows
    public static void main(String[] args) {
        JaxWsDynamicClientFactory dcf  = JaxWsDynamicClientFactory.newInstance();
        //这地址是你出现wsdl的地址
        Client client = dcf.createClient("http://localhost:8080/webservice/ws/api?wsdl");
        //反射的调用  方法名   , 参数-----,参数参数
        Object[] invoke = client.invoke("hello", "demo");
        for (Object o : invoke) {
            System.out.println(o.toString());
        }
    }
}

http://localhost:8080/webservice/ws/api?wsdl 返回wsdl文件内容 @XmlAccessorType注解实体类

二、通过wsdl生成java类

wsimport -d C:\Users\zhk_work\Desktop\报销  -s C:\Users\zhk_work\Desktop\报销 http://localhost:8080/webservice/ws/api?wsdl

### 代表含义
-d  class类存放目录

-s 源代码存放目录

wsdl文档存放目录

随后将生成java类放项目中

三、http获取

下载工具
下载工具
Springboot集成WebService_第1张图片
Springboot集成WebService_第2张图片
Springboot集成WebService_第3张图片
Springboot集成WebService_第4张图片

把?换成参数

使用http请求 参数就加xml就行

demo详细推荐

简单

你可能感兴趣的:(idea,spring,boot,java,后端)