springboot使用webservice发布和调用接口

springboot使用webservice发布和调用接口

maven依赖:

<!-- cxf框架依赖  -->
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
	<version>3.2.4</version>
</dependency>

服务端service代码:

import cn.ccccltd.ccccoi.mediumterminvestment.projectfilling.synchronization.entity.ProjectAndProApprove;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface ProjectInAndProAppService {

    /**
     * service接口
     * @param projectAndProApprove
     * @return
     */
    @WebMethod
    public String insertProjectInAndProApp(@WebParam(name = "projectAndProApprove") ProjectAndProApprove projectAndProApprove);
}

主要有@WebService @WebMethod @WebParam注解

服务端实现类代码:

import cn.ccccltd.ccccoi.mediumterminvestment.projectfilling.synchronization.dao.SynchronizationDao;
import cn.ccccltd.ccccoi.mediumterminvestment.projectfilling.synchronization.entity.ProjectAndProApprove;
import cn.ccccltd.ccccoi.mediumterminvestment.projectfilling.synchronization.entity.ProjectApprove;
import cn.ccccltd.ccccoi.mediumterminvestment.projectfilling.synchronization.entity.ProjectIn;
import cn.ccccltd.ccccoi.mediumterminvestment.projectfilling.synchronization.service.ProjectInAndProAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.jws.WebService;
import java.util.Objects;

@WebService(
        targetNamespace = "http://service.synchronization.projectfilling.mediumterminvestment.ccccoi.ccccltd.cn/",
        serviceName = "SynchronizationService", //portType名称 客户端生成代码时 为接口名称
        endpointInterface = "cn.ccccltd.ccccoi.mediumterminvestment.projectfilling.synchronization.service.ProjectInAndProAppService")
@Service
public class ProjectInAndProAppServiceImpl implements ProjectInAndProAppService {
    @Autowired
    private SynchronizationDao synchronizationDao;

    /**
     * 接收投资管理系统的项目基本信息数据,添加到项目表和项目批复表
     * @param projectAndProApprove
     * @return
     */
    @Override
    public String insertProjectInAndProApp(ProjectAndProApprove projectAndProApprove) {
        if(Objects.nonNull(projectAndProApprove)){
            ProjectIn projectIn = projectAndProApprove.getProjectIn();
            ProjectApprove projectApprove = projectAndProApprove.getProjectApprove();
            if(Objects.nonNull(projectIn)) {
                //新增项目基本信息表信息
                int count = synchronizationDao.insertProjectIn(projectIn);
                if (count < 1) {
                    return "error";
                }
            }else {
                return "nullData";
            }
            //根据关联投资平台外键查询项目表的projectId赋值给项目批复表的projectId
            String projectId = synchronizationDao.queryProjectId(projectIn.getForeignKeyProjectId());
            if(projectId != null && !projectId.equals("")) {
                projectApprove.setProjectId(projectId);
            }
            if(Objects.nonNull(projectApprove)) {
                //新增项目批复表信息
                int count1 = synchronizationDao.insertProjectApprove(projectApprove);
                if (count1 < 1) {
                    return "error";
                }
            }else {
                return "nullData";
            }
        }else{
            return "参数为空";
        }
        return "success";
    }
}

服务端发布服务类:
我的端口设置为8080,所以我的服务地址为http://127.0.0.1:8080/ws/helloWebService?wsdl

import javax.xml.ws.Endpoint;
import cn.ccccltd.ccccoi.mediumterminvestment.projectfilling.synchronization.service.ProjectInAndProAppService;
import org.apache.cxf.Bus;
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;

/**
 * cxf配置类
 * @author oKong
 *
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private Bus bus;
    @Autowired
    private ProjectInAndProAppService projectInAndProAppService;

    @Bean("cxfServletRegistration")
    public ServletRegistrationBean dispatcherServlet() {
        //注册servlet 拦截/ws 开头的请求 不设置 默认为:/services/*
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }

    /**
     * 发布endpoint
     * @return
     */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, projectInAndProAppService);
        endpoint.publish("/projectInAndProAppService");//发布地址
        return endpoint;
    }
}

WebService发布接口完成

调用别人发布的webService接口

soapUI调试工具
在这里插入图片描述
打开soapUI,点击File->New SOAP Project
在这里插入图片描述
在Initial输入发布的webService接口
在这里插入图片描述
可看到接口所发布的xml报文

调用别人发布的webService接口,只需要把拼接xml报文,在调用

拼接报文方法

    public static String projectBasicInfo(SecondaryUnits secondaryUnits, ProjectCompany projectCompany) throws ParseException {
    	StringBuilder sb = new StringBuilder();
		// 设置头
        sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:con=\"http://www.primeton.com/ContractAndCompanyReportImplService\" xmlns:sel=\"http://selfevalreport.projectcompany.pdiwt.com\"><soapenv:Header/><soapenv:Body><con:ContractAndCompanyReportWeb><con:in0>");

		//拼接数据
        sb.append("<sel:contractInfo>");
        sb.append("<sel:projCapitalRatioPercentage>"+ projProjectCapitalRatioDouble +"</sel:projCapitalRatioPercentage>");
        sb.append("<sel:projChienanCostsRatio>"+ projChienanCostsRatioDouble +"</sel:projChienanCostsRatio>");
        sb.append("<sel:projChienanFeesAmount>"+ projChienanFeesAmountDouble +"</sel:projChienanFeesAmount>");
        sb.append("<sel:projComplementaryContractDuration>" + projSupplementaryContractPeriodDouble + "</sel:projComplementaryContractDuration>");
        sb.append("<sel:projContractDuration>" + projContractPeriodDouble+ "</sel:projContractDuration>");
        if(StringUtils.isNotBlank(secondaryUnits.getProjContractName())) {
            sb.append("<sel:projContractName>" + secondaryUnits.getProjContractName() + "</sel:projContractName>");
        }
        //设置尾
        sb.append("</con:in0></con:ContractAndCompanyReportWeb></soapenv:Body></soapenv:Envelope>");
        return sb.toString();
}

我们这里用StringBuilder 来拼接报文

	// 推送数据
    public static String sendData(String xml) throws Exception {
        String url = "发布接口的webService,后缀不需要加 "?wsdl" ";
        HttpURLConnection conn = getHttpURLConnection(url);
        OutputStream os = conn.getOutputStream();
        os.write(xml.getBytes());
        return getXML(conn, os);
    }

    private static HttpURLConnection getHttpURLConnection(String url) throws IOException {
        // 服务的地址
        URL wsUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setConnectTimeout(120000);
        conn.setReadTimeout(120000);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
        return conn;
    }

    private static String getXML(HttpURLConnection conn, OutputStream os) throws IOException {
        InputStream is = conn.getInputStream();
        byte[] b = new byte[1024];
        int len = 0;
        StringBuilder sb = new StringBuilder();
        while ((len = is.read(b)) != -1) {
            String ss = new String(b, 0, len, "UTF-8");
            sb.append(ss);
        }
        is.close();
        os.close();
        conn.disconnect();
        return sb.toString();
    }
logger.info("推送tz报文");
logger.info(message);
logger.info("推送tz信息接口调用状态");
logger.info(ProjectBasicInfoUtil.sendData(message));

message为拼接后的报文,调用sendData方法,调用webService接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值