SpringBoot中整合使用WebService

我使用的SpringBoot的版本为 2.0.3.RELEASE

首先pom.xml文件中加入WebService的相关依赖:

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

WebService接口:

@javax.jws.WebService(serviceName = "WSServer", // 服务名称
        targetNamespace = "http://service.webservice.com/"// 一般是接口的包名倒序
)
public interface WebService {
    @WebMethod
    @WebResult(name = "String", targetNamespace = "")
    public String testWS(@WebParam(name = "msg") String msg);

}

实现类:

@javax.jws.WebService(serviceName = "WSServer", // 与接口中指定的name一致
        targetNamespace = "http://service.webservice.com/"// 与接口中的命名空间一致,一般是接口的包名倒序
)
@Component
public class WebServiceImpl implements WebService {

       @Override
       public String testWS(String msg){
             return msg;
       }

}

发布webservice服务

import com.webservice.service.WebService;
import com.webservice.service.WebServiceImpl;
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @SuppressWarnings("all")
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet() {
        //创建服务并指定服务名称
        return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
    }



    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {

        return new SpringBus();
    }


    @Bean
    public WebService webService(){

        return new WebServiceImpl();
    }

    /**
     * 注册WebServiceDemoService接口到webservice服务
     * @return
     */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(),webService());
        endpoint.publish("/webServiceImpl");
        return endpoint;
    }

}

启动服务,在浏览器输入服务ip:port/webservice   即可看到服务的WSDL地址等相关信息。

接下来使用CXF动态调用我们创建的服务:

        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://192.168.4.100:8080/webservice/webServiceImpl?wsdl");
        // 需要密码的情况需要加上用户名和密码
         //client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("testWS", "测试WS是否调用成功");
            System.out.println("返回数据======================:" + objects[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }

 调用成功控制台会打印出:

返回数据======================:测试WS是否调用成功

 

 

 

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
package com.xfire.core.client; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.xfire.core.entity.UserInfo; import com.xfire.core.service.IUserInfoService; /** *@author jilongliang *@Date 2012-3-5 * */ public class UserInfoClient { public static void main(String[] args) { getServiceList(); setServiceList(); } static String url = "http://localhost:8081/xfire/services/UserInfo"; /** * */ public static void getServiceList() { Service service = new ObjectServiceFactory() .create(IUserInfoService.class); try { IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory() .create(service, url); List list = (ArrayList) iAddressService .getAddressList(); System.out.println("一共多少条数据:" + list.size()); for (Iterator iter = list.iterator(); iter.hasNext();) { UserInfo a = iter.next(); System.out.println(a); } } catch (MalformedURLException e) { e.printStackTrace(); } } public static void setServiceList() { Service service = new ObjectServiceFactory() .create(IUserInfoService.class); try { IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory() .create(service, url); List listAdd = new ArrayList(); UserInfo address = new UserInfo(); address.setIdentifier(1); address.setCountry("國"); address.setProivice("廣東省"); address.setCity("陽江"); address.setAddress("廣東陽春"); address.setPostCode("1111111"); address.setExist(false); address.setArrary(new String[] { "22", "23", "24" }); listAdd.add(address); address.setIdentifier(2); address.setCountry("國"); address.setProivice("廣東省"); address.setCity("陽江"); address.setAddress(
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值