> 文章列表 > springboot集成webservice

springboot集成webservice

springboot集成webservice

1、首先是springboot的pom.xml文件,关键依赖

        <dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.2.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.2.4</version></dependency>

2、新建webservice的接口文件

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;@WebService(targetNamespace = "http://hb.tobacco.com",name = "monthPlanWS"
)
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
public interface MonthPlanWS {//查询月计划String getMonthPlan(@WebParam(name = "yearMonth") String yearMonth,@WebParam(name = "comId") String comId) throws Exception;}

3、然后是接口的实现类

import com.wiseda.sale.contract.mapper.plan.PoTMpMonthplanMapper;
import com.wiseda.sale.contract.webservice.MonthPlanWS;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.jws.WebService;@Log4j2
@Component
@WebService(targetNamespace = "http://hb.tobacco.com",                              // 与接口中的命名空间一致serviceName = "monthPlanWS",                                            // 与接口中指定的name一致endpointInterface = "com.wiseda.sale.contract.webservice.MonthPlanWS"   // 接口地址
)
public class MonthPlanWSImpl implements MonthPlanWS {@Resourceprivate PoTMpMonthplanMapper mpMonthplanMapper;@Overridepublic String getMonthPlan(String yearMonth,String comId) throws Exception {StringBuffer buffer = new StringBuffer();buffer.append("<MSGNAME>"+comId+yearMonth+"同步反馈</MSGNAME>\\n");return buffer.toString();}}

4、再然后是webservice的配置相关

import com.wiseda.sale.contract.webservice.MonthPlanWS;
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.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;@Configuration
@Slf4j
public class WebServiceConfig {@Autowiredprivate MonthPlanWS monthPlanWS;@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {SpringBus springBus = new SpringBus();springBus.getProperties().put("org.apache.cxf.stax.maxTextLength", Integer.MAX_VALUE);return springBus;}@Beanpublic ServletRegistrationBean servicesServlet() {ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/webservice/*");bean.addInitParameter("hide-service-list-page", "true");return bean;}/* 注册 MonthPlanWS 接口到webservice服务* @return*/@Beanpublic Endpoint monthPlanWS_endpointPublish() {EndpointImpl endpoint = new EndpointImpl(springBus(), monthPlanWS);endpoint.publish("/monthPlanWS");log.info("webservice服务发布成功!地址为:http://localhost:8202/contract/webservice/monthPlanWS?wsdl");return endpoint;}}

启动springboot,访问对应的地址http://localhost:8202/contract/webservice/monthPlanWS?wsdl,

得到熟悉的界面,可以看到webservice也启动成功了(webservice的端口号就是springboot项目自己的端口号)


然后,可以用soupui工具进行访问

  点击ok,就可以看到在接口文件中定义的方法

 点击里面的emrService节点的request1,进行测试,可以看到返回数据结果

我用apifox工具进行测试,body里放左侧的请求的xml,得到完全相同的的结果

使用代码调用

我用springboot自带的RestTemplate类发送了一次post请求,发现也是得到了右侧的返回内容,这是我的测试类 

/* webservice测试类*/
public class WbClient {@Testpublic void invokeService_2(){try {//1、直接引用远程的wsdl文件  String endpoint = "http://localhost:8202/contract/webservice/monthPlanWS?wsdl";Service service = new Service();//2、创建服务Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint);  //3、定义报名和接口方法call.setOperationName(new QName("http://hb.tobacco.com", //wsdl文件中的targetNamespace"getMonthPlan") //接口实现功能的方法);//4、设置参数call.addParameter("yearMonth", XMLType.XSD_STRING,ParameterMode.IN);// 接口的参数call.addParameter("comId",XMLType.XSD_STRING,ParameterMode.IN);// 接口的参数call.setReturnType(XMLType.XSD_STRING);// 设置返回类型  //5、给方法传递参数,并且调用方法String yearMonth="202209";String comId="COM_78";String result = (String) call.invoke(new Object[] {yearMonth ,comId});System.out.println("result="+result);} catch (Exception e) {  e.printStackTrace();}}
}