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 {
@Resource
private PoTMpMonthplanMapper mpMonthplanMapper;
@Override
public 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 {
@Autowired
private 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;
}
@Bean
public ServletRegistrationBean servicesServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
bean.addInitParameter("hide-service-list-page", "true");
return bean;
}
/**
* 注册 MonthPlanWS 接口到webservice服务
* @return
*/
@Bean
public 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 {
@Test
public 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();
}
}
}