java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot发布WebService接口并调用

通过springboot发布WebService接口并调用方式

作者:大乐哥。

Spring Boot集成CXF需注意版本对应,配置注解并发布服务,通过WSDL验证,结合Controller和Swagger测试,CXF支持SOAP、REST等服务,提供代码与合同优先开发模式

SpringBoot集成cxf步骤记录

1.什么是cxf

Apache CXF是一个开源的Service框架,简化用户的service开发,基于CXF开发的应用可提供SOAP、XML/HTTP、RESTFUL HTTP或CORBA等服务。CXF底层页可以使用不同的传输协议,包括HTTP、JMS或JBI等。

cxf特性

支持大量的Web Service标准:包括SOAP、WS-I Basic Profile、WSDL、WS-Addressing、WS-Policy、WS-ReliableMessaging和WS-Security。

支持大量的前端(frontend)编程模型。CXF实现了标准的JAX-WS API,它也包括一种被称为简单前端(simple frontend)的模型,这种模型无需annotation支持。

CXF支持web service的两种开发模式:

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 -->

注意事项:cxf版本需要与SpringBoot的版本对应,不然因版本问题, 整合cxf-spring-boot-starter-jaxws的启动项目会出现异常。

具体对应关系上仓库进行查看下

https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws查找到jaxws的各种版本,进入之后可以看到对应的springboot版本

3.服务端

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 服务的发布

自定义注解标注要发布的服务类,发布出去

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:自动发布接口地址注解</p>
 *
 * @author newzhong
 * @version 1.0
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoPublish {
    /**
     *<p>description:发布地址</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("开始进行自动发布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("发布接口地址:[%s]", publishAddr));
        }

        log.info("weBservice接口自动发布结束");
    }

}


3.3 发布的地址yml,nacos配置

cxf:

path: /cxf

原本默认的默认端口号后 +拼接 /service,现在可以根据需求进行修改

3.4. 新增服务接口

在接口上添加@WebService注解

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>创建时间:</b> 2021-03-27 14:32
 */
@WebService(targetNamespace = "http://service.mrxu.com/")
public interface IWebServiceTest {

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

3.5 新增服务接口实现类

注意:实现类上可以不添加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 验证服务发布

通过浏览器访问wsdl,wsdl路径即为发布的路径加上?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进行测试

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文