当前位置: 首页 > article >正文

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();
		}
	}
}

http://www.kler.cn/a/13940.html

相关文章:

  • 【AutoGPT】你自己运行,我先睡了—— ChatGPT过时了吗?
  • JUC多并发编程 LockSupport和线程中断
  • 最佳实践:Android应用中的网络请求和数据缓存
  • 利用校正板对无人机影像辐射校正
  • [算法前沿]--004-transformer的前世今生
  • java超市会员积分管理系统
  • 【Android车载系列】第10章 系统服务-SystemServer源码分析(API28)
  • mybatis04-mybatis缓存、分页插件、注解开发(一对一、多对一、多对多)
  • Linux清理系统日志或临时文件logrotate使用方法
  • zookeeper笔记
  • AUTOSAR网络管理
  • 【华为OD机试真题 C++】1054 - 统一限载货物数最小值 | 机试题+算法思路+考点+代码解析
  • 对比学习(持续更新ing...)
  • 查询网站ip地址
  • PHP+Vue+java导师学生双选系统设计与实现springnboot+pyton
  • 太阳能电池IV测试软件的主要功能,太阳能电池特性测试
  • 方向梯度直方图(Histogram of Oriented Gradient)
  • Linux 这4个进程相关的命令,太好用!
  • Mybatis学习基础篇(一)——使用Maven快速搭建一个mybatis项目,并实现简单的增删改查
  • MySQL 8.0原理与实战一网打尽,甲骨文数据库专家硬刚5年之作