调用WebService接口实现短信发送并打包运行

调用WebService接口实现短信发送并打包运行

1、建立SpringBoot项目,导入相关依赖

2、编写utils工具类

package com.webservice.sms.utils;


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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * webservice发送请求工具类
 */
public class WebServiceUtils {

    /**
     * 发送短信,连接WebService接口
     *
     * @param wsdlUrl       http://114.247.42.150:8080/SMSG/services/SMS?wsdl
     * @param operationName 方法名: AddSMSList
     * @param params        方法名的方法参数
     * @return
     * @throws Exception
     */
    public static String callWebSV(String wsdlUrl, String operationName, Object... params) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdlUrl);
        Object[] objects;
        objects = client.invoke(operationName, params);
        return objects[0].toString();
    }

    /**
     * 生成SmsID
     *
     * @return
     */
    public static String getSmsID() {
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String num = (Math.random() * 900000) + 100000 + "";
        String smsId = (format + num).replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "").substring(0, 20);
        //System.out.println("smsId = " + smsId);
        return smsId;
    }

    /**
     * 获取XML
     *
     * @return
     */
    public static String getXml(String phoneNum, String content) {

        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<SMS type=\"send\">\n" +
                "<Message " +
                "SmsID=\"smsId\" " +
                "SendNum=\"100\" " +
                "RecvNum=\"phoneNum\" " +
                "Content=\"content\"/>\n" +
                "</SMS>";
        xml = xml.replaceAll("phoneNum", phoneNum).replaceAll("content", content).replaceAll("smsId", getSmsID());
        //System.out.println("xml = " + xml);
        return xml;
    }

    /**
     * 给多个手机号发送
     * @param phoneNum
     * @param content
     * @return
     */
    public static String getXmls(String[] phoneNum, String content) {
        String xml1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<SMS type=\"send\">\n" + "";
        String xml2 = null;
        List<String> list = new ArrayList<>();
        for (int i = 0; i < phoneNum.length; i++) {
            xml2 = '"' +"<Message " +
                    "SmsID=\"smsId\" " +
                    "SendNum=\"100\" " +
                    "RecvNum=\"phoneNum\" " +
                    "Content=\"content\"/>\n" +'"' ;
            xml2 = xml2.replaceAll("phoneNum", phoneNum[i]).replaceAll("content", content).replaceAll("smsId", getSmsID());
            list.add(xml2);
        }
        String xml3 = "</SMS>";
        String xml = xml1 + list + xml3;
        return xml;
    }


}

3、字符集编码格式和异常全局捕捉

package com.webservice.sms.filter;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import java.io.IOException;

/**
 * 中文乱码过滤器
 */
@Configuration
public class EncodingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        chain.doFilter(request,response);
    }
}

package com.webservice.sms.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionConfig {

    @ExceptionHandler(Exception.class)
    public String myException(Exception e) {
        if (e instanceof Exception) {
            e.printStackTrace();
            return "系统错误";
        }
        return "抱歉,出错了!!";
    }

}

4、Controller层

@RestController
public class WenServiceController {
    @GetMapping("/send")
    public String send(@RequestParam String name, @RequestParam String passWord, @RequestParam String[] phoneNum, @RequestParam String content) throws Exception {
    	//参数说明 name:信息平台的登录名 passWord:登录密码 phoneNum:手机号 content:短信内容
    	// webUrl:短信平台地址 methodName:调用的方法
        String webUrl = "http://127.0.0.1:8080/SMSG/services/SMS?wsdl";
        String methodName = "AddSMSList";
        String xml = WebServiceUtils.getXmls(phoneNum, content);
        WebServiceUtils.callWebSV(webUrl, methodName, name, passWord, xml);
        return "success";
    }

}

5、打包并运行

开始按照网上推荐方式进行打包,总是报找不到com.sun.tools.*的包,找遍了所有可以试的方法就是不行,所以自己就琢磨吧,结果终于搞出来了,所以写个文章,保存下来,也希望能帮到需要的人!

5.1添加tools.jar

手动添加tools.jar,目录结构

在这里插入图片描述

tools.jar 在jdk安装目录下的lib目录下,第一次导入之后先右击导入的tools.jar,然后往下拉,点击Add as Library 在这里插入图片描述

5.2打包

在这里插入图片描述
在这里插入图片描述

之后点击导航栏上的Build,找到Build Artifacts

在这里插入图片描述

5.3 运行

在这里插入图片描述
打开cmd,
在这里插入图片描述

utils方法来自: https://www.cnblogs.com/metu/p/10567269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值