最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過springboot發(fā)布WebService接口并調(diào)用方式

 更新時間:2025年09月24日 17:20:32   作者:大樂哥。  
Spring Boot集成CXF需注意版本對應(yīng),配置注解并發(fā)布服務(wù),通過WSDL驗(yàn)證,結(jié)合Controller和Swagger測試,CXF支持SOAP、REST等服務(wù),提供代碼與合同優(yōu)先開發(fā)模式

SpringBoot集成cxf步驟記錄

1.什么是cxf

Apache CXF是一個開源的Service框架,簡化用戶的service開發(fā),基于CXF開發(fā)的應(yīng)用可提供SOAP、XML/HTTP、RESTFUL HTTP或CORBA等服務(wù)。CXF底層頁可以使用不同的傳輸協(xié)議,包括HTTP、JMS或JBI等。

cxf特性

支持大量的Web Service標(biāo)準(zhǔn):包括SOAP、WS-I Basic Profile、WSDL、WS-Addressing、WS-Policy、WS-ReliableMessaging和WS-Security。

支持大量的前端(frontend)編程模型。CXF實(shí)現(xiàn)了標(biāo)準(zhǔn)的JAX-WS API,它也包括一種被稱為簡單前端(simple frontend)的模型,這種模型無需annotation支持。

CXF支持web service的兩種開發(fā)模式:

  • 規(guī)則(contract)優(yōu)先: 通過編寫WSDL來開發(fā)web service;
  • 代碼優(yōu)先: 通過編寫java代碼來開發(fā)webservice.

2.集成引入

    <!-- cxf start -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-features-logging</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!-- cxf end -->

注意事項(xiàng):cxf版本需要與SpringBoot的版本對應(yīng),不然因版本問題, 整合cxf-spring-boot-starter-jaxws的啟動項(xiàng)目會出現(xiàn)異常。

具體對應(yīng)關(guān)系上倉庫進(jìn)行查看下

https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws查找到j(luò)axws的各種版本,進(jìn)入之后可以看到對應(yīng)的springboot版本

3.服務(wù)端

3.1 CXF配置類

package com.mrxu.config;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.ext.logging.LoggingFeature;
import org.springframework.context.annotation.Bean;

/**
 * <b>功能描述:CXF配置類</b><br>
 *
 * @author newzhong
 * @version 1.0.0
 * @since JDK 1.8
 */
public class CxfConfig {
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        SpringBus bus = new SpringBus();
        bus.getFeatures().add(new LoggingFeature());
        return bus;
    }
}

3.2 服務(wù)的發(fā)布

自定義注解標(biāo)注要發(fā)布的服務(wù)類,發(fā)布出去

package com.mrxu.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * <p>description:自動發(fā)布接口地址注解</p>
 *
 * @author newzhong
 * @version 1.0
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoPublish {
    /**
     *<p>description:發(fā)布地址</p>
     * @return String
     * @author newzhong
     */
    String publishAddress();
}


package com.mrxu.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

@Component
@Slf4j
public class PublishEndpoint implements ApplicationRunner {

    @Autowired
    private WebApplicationContext applicationConnect;

    @Autowired()
    @Qualifier(Bus.DEFAULT_BUS_ID)
    private SpringBus bus;

    @SuppressWarnings("resource")
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        log.info("開始進(jìn)行自動發(fā)布webService接口");

        String[] beanNames = applicationConnect.getBeanNamesForAnnotation(AutoPublish.class);
        for(String beanName : beanNames) {
            String publishAddr = applicationConnect.getType(beanName).getAnnotation(AutoPublish.class).publishAddress();
            EndpointImpl endpoint = new EndpointImpl(bus, applicationConnect.getBean(beanName));
            endpoint.publish(publishAddr);

            log.info(String.format("發(fā)布接口地址:[%s]", publishAddr));
        }

        log.info("weBservice接口自動發(fā)布結(jié)束");
    }

}


3.3 發(fā)布的地址yml,nacos配置

cxf:

path: /cxf

原本默認(rèn)的默認(rèn)端口號后 +拼接 /service,現(xiàn)在可以根據(jù)需求進(jìn)行修改

3.4. 新增服務(wù)接口

在接口上添加@WebService注解

  • @WebParam表示方法的參數(shù),如果不加此注解,方法的參數(shù)都從arg0,開始,隨著參數(shù)增多,name不斷增加為arg1,arg2…;
  • @WebResult表示方法的返回值, 沒有此注解 返回值名字為return
package com.mrxu.service;

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

/**
 * <b>功能描述:webService測試接口</b><br>
 * @author newzhong
 * @version 1.0.0
 * @since JDK 1.8
 *
 * @Note
 * <b>創(chuàng)建時間:</b> 2021-03-27 14:32
 */
@WebService(targetNamespace = "http://service.mrxu.com/")
public interface IWebServiceTest {

    String getDept(@WebParam(name = "jsonStr")String jsonStr);
}

3.5 新增服務(wù)接口實(shí)現(xiàn)類

  • serviceName: 對外發(fā)布的服務(wù)名,指定 Web Service 的服務(wù)名稱:wsdl:service。缺省值為 Java 類的簡單名稱 + Service。(字符串)
  • endpointInterface: 服務(wù)接口全路徑, 指定做SEI(Service EndPoint Interface)服務(wù)端點(diǎn)接口
  • name:此屬性的值包含XML Web Service的名稱。在默認(rèn)情況下,該值是實(shí)現(xiàn)XML Web Service的類的名稱,wsdl:portType 的名稱。缺省值為 Java 類或接口的非限定名稱。(字符串
  • portName: wsdl:portName。缺省值為 WebService.name+Port。
  • targetNamespace:指定你想要的名稱空間,認(rèn)是使用接口實(shí)現(xiàn)類的包名的反綴
  • wsdlLocation:指定用于定義 Web Service 的 WSDL 文檔的 Web 地址。Web 地址可以是相對路徑或絕對路徑。(字符串)

注意:實(shí)現(xiàn)類上可以不添加Webservice注解 ,加在接口上

package com.mrxu.service.impl;

import com.mrxu.common.domain.Phone;
import com.mrxu.config.AutoPublish;
import com.mrxu.dao.PhoneMapper;
import com.mrxu.service.IWebServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.List;


@Service("WebServiceTest")
@AutoPublish(publishAddress = "test")
@WebService(endpointInterface = "com.mrxu.service.IWebServiceTest",
            serviceName = "WebServiceTest",
            targetNamespace = "http://service.mrxu.com/"
)
public class WebServiceTest implements IWebServiceTest {

    @Autowired
    public PhoneMapper phoneMapper;

    @Override
    public String getDept(String jsonStr) {
        List<Phone> phones = phoneMapper.selectPhone();
        String s = phones.toString();
        return s;
    }
}

3.6 驗(yàn)證服務(wù)發(fā)布

通過瀏覽器訪問wsdl,wsdl路徑即為發(fā)布的路徑加上?wsdl

http://127.0.0.1:[端口號]/cxf/test?wsdl


可以看到接口就成功了。

測試webservice接口

1.新建一個controller

package com.mrxu.controller;

import com.mrxu.service.TestWebService;
import io.swagger.annotations.Api;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "webser")
@RestController
@RequestMapping("/wbser")
public class WebServiceController {

    private static final long CONNECT_TIMEOUT = 10 * 1000;

    private static final long RECEIVE_TIMEOUT = 60 * 1000;

    private static final String ACTINFO_WSDL = "http://175.2.70.225:9002/cxf/test?wsdl";

    private TestWebService service = getProxyService(ACTINFO_WSDL, TestWebService.class);

    @PostMapping("/wbser")
    public String wbeser(String id){
        String userService = service.getDept("1");
        return userService;
    }


    public static <T> T getProxyService(String wsdl, Class<T> serviceClass) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(serviceClass);
        factory.setAddress(wsdl);
        T service = (T) factory.create();
        org.apache.cxf.endpoint.Client proxy = ClientProxy.getClient(service);
        HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(CONNECT_TIMEOUT);
        policy.setReceiveTimeout(RECEIVE_TIMEOUT);
        conduit.setClient(policy);
        return service;
    }
}

2.新建一個WebService接口

package com.mrxu.service;


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


@WebService(targetNamespace="http://service.mrxu.com/")
public interface TestWebService {

    @WebMethod
    @WebResult
    String getData(@WebParam String requestData);

    @WebMethod
    @WebResult
    String getData1(@WebParam String requestData);

    @WebMethod
    @WebResult
    String getDept(@WebParam(name = "jsonStr")String id);
}

最后可以通過swagger進(jìn)行測試

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

南溪县| 北京市| 兖州市| 故城县| 丹凤县| 德钦县| 正定县| 太保市| 兴海县| 青州市| 乌鲁木齐县| 临江市| 蒙城县| 西吉县| 武义县| 南岸区| 若尔盖县| 安溪县| 炉霍县| 佛冈县| 尚义县| 循化| 开阳县| 肃南| 阿坝县| 南和县| 井陉县| 大化| 手游| 郓城县| 吉木乃县| 苏州市| 弋阳县| 黄浦区| 聂荣县| 那坡县| 安远县| 库尔勒市| 濮阳县| 锡林郭勒盟| 莲花县|