springboot整合webserviceb暴露接口和訪問服務


依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.4</version>
        </dependency>

接口

package com.llf.service;

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

/**
 * @author :llf
 * @date :Created in 2020-05-14 8:40
 * @description:${description}
 * @version: v1.0
 */
@WebService(name = "WbService",targetNamespace = "http://service.llf.com")
public interface WbService {

    @WebMethod
    public Object webService(@WebParam(name = "name") String name);
}

接口實現

package com.llf.service.impl;

import com.llf.service.WbService;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.lang.annotation.Annotation;

/**
 * @author :llf
 * @date :Created in 2020-05-14 8:43
 * @description:${description}
 * @version: v1.0
 */
@Service
@WebService(name = "WbService",
        targetNamespace = "http://service.llf.com",
        endpointInterface = "com.llf.service.WbService")
public class WebServiceImpl implements WbService {

    @Override
    public Object webService(String name) {
        System.out.println("webService接口實現執行。。。。。。。");
        return name;
    }
}

配置

package com.llf.config;

import com.llf.service.WbService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
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;

/**
 * @author xzy
 * @date 2020-03-15 17:49
 * 說明:服務端發布相關接口
 */
@Configuration
public class CxfConfig {

    @Autowired
    private WbService wbService;

    /**
     * 注入servlet  bean name不能dispatcherServlet 否則會覆蓋dispatcherServlet
     * @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 = "WebServiceDemoEndpoint")
    public Endpoint sweptPayEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), wbService);
        endpoint.publish("/webservice");
        return endpoint;
    }
}

訪問路徑:http://localhost:9527/webservice?wsdl

接口暴露成功,下圖所示

 

 

 訪問暴露的接口:

 

package com.llf.config;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * @author :llf
 * @date :Created in 2020-05-18 9:29
 * @description:${description}
 * @version: v1.0
 */
public class Text {

        public static void main(String[] args) {
                JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
                Client client = dcf.createClient("http://localhost:9527/webservice/webservice?wsdl");
                // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
                Object[] objects = new Object[0];
                try {
                        // invoke("方法名",參數1,參數2,參數3....);
                        objects = client.invoke("webService", "傳遞的參數");
                        System.out.println("返回數據:" + objects[0]);
                } catch (java.lang.Exception e) {
                        e.printStackTrace();
                }
        }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM