SpringBoot整合WebService

9 篇文章 0 订阅
2 篇文章 0 订阅

引入POM依赖

<!-- fastjson -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.75</version>
</dependency>
<!-- webService-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- CXF webservice -->
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
	<version>3.4.5</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>3.4.5</version>
</dependency>

发送WebService请求工具类

package repalce.your.package;

import javax.xml.namespace.QName;

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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class WebServiceUtil {
	/**
	 * 发送WebService请求
	 * @param wsdlUrl	   webservice请求地址
	 * @param namespaceURI 命名空间URI
	 * @param localPart	   方法名
	 * @param params	   请求参数
	 * @return Json格式字符串
	 * @throws Exception 异常
	 */
	public static String sendWsdl(String wsdlUrl, String namespaceURI, String localPart, Object... params) throws Exception{
		// 创建动态客户端
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient(wsdlUrl);
		QName qName = new QName(namespaceURI, localPart);
		
		// 发送请求并获取返回信息
		Object[] objects = client.invoke(qName, params);
		
		// 返回信息转为字符串
		String str = (String) objects[0];
		
		// 返回信息字符串
		return str;
	}
	
	/**
	 * 发送WebService请求
	 * @param wsdlUrl	   webservice请求地址
	 * @param namespaceURI 命名空间URI
	 * @param localPart	   方法名
	 * @param params	   请求参数
	 * @return JSONObject对象(FastJson)
	 * @throws Exception 异常
	 */
	public static JSONObject getJsonObject(String wsdlUrl, String namespaceURI, String localPart, Object... params) throws Exception {
		String jsonStr = sendWsdl(wsdlUrl, namespaceURI, localPart, params);
		return JSONObject.parseObject(jsonStr);
	}
	
	/**
	 * 发送WebService请求
	 * @param wsdlUrl	   webservice请求地址
	 * @param namespaceURI 命名空间URI
	 * @param localPart	   方法名
	 * @param params	   请求参数
	 * @return JSONArray对象(FastJson)
	 * @throws Exception 异常
	 */
	public static JSONArray getJsonArray(String wsdlUrl, String namespaceURI, String localPart, Object... params) throws Exception {
		String jsonStr = sendWsdl(wsdlUrl, namespaceURI, localPart, params);
		return JSONArray.parseArray(jsonStr);
	}
}

WebService配置类

package repalce.your.package;

import javax.xml.ws.Endpoint;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import repalce.your.package.ApiService;

@Configuration
public class WebServiceConfig {

    @Autowired
    private ApiService apiService;

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

    @Bean
    public Endpoint endpoint() {
      EndpointImpl endpoint = new EndpointImpl(springBus(), apiService);
      endpoint.publish("/ws/api");
      return endpoint;
    }
}

WebService接口类

package repalce.your.package;

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

/** 服务端开放的Api接口 */
@WebService(
  name = "ApiService",
  targetNamespace = "http://package.your.repalce"
)
public interface ApiService {

  /**
   * 你好,世界!
   * @param param1 参数1
   * @param param2 参数2
   * @return 数据
   */
  @WebMethod
  public String HelloWorld(@WebParam String param1, @WebParam String param2);

}


WebService接口实现类

package repalce.your.package;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

import repalce.your.package.ApiService;

@Component
@WebService(
  serviceName = "ApiService",
  targetNamespace = "http://package.your.repalce",
  endpointInterface = "repalce.your.package.ApiService"
)
public class ApiServiceImpl implements ApiService {
  @Override
  public String HelloWorld(String param1, String param2) {
    return "Hello,World!";
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值