spring boot 创建和发布web service接口

一:创建springboot项目

这里不再赘述搭建过程,注意访问WebService接口时候,如果访问不成功,请先关闭防火墙

下面是red hat/CentOs7关闭防火墙的命令!
1:查看防火状态
systemctl status firewalld
service  iptables status
2:暂时关闭防火墙
systemctl stop firewalld
service  iptables stop
3:永久关闭防火墙
systemctl disable firewalld
chkconfig iptables off
4:重启防火墙
systemctl enable firewalld
service iptables restart  

二:导入pom依赖

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

三:创建接口,以及实现类

SDAPISubmitService.java

package com.middleware.webservice.interf;

import java.util.Map;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface SDAPISubmitService {
	@WebMethod
    public Map<String, Object> submit(@WebParam(name ="orderNum")String orderNum,@WebParam(name ="note")String note,
    		@WebParam(name ="receiptQuantity")String receiptQuantity,@WebParam(name ="receiptLocation")String receiptLocation,
    		@WebParam(name ="nonReceiptQuantity")String nonReceiptQuantity,@WebParam(name ="nonReceiptLocation")String nonReceiptLocation);
}

SDAPISubmitServiceImpl.java

package com.middleware.webservice.impl;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jws.WebService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.middleware.common.Config;
import com.middleware.util.MD5Util;
import com.middleware.util.SSLHttpHelper;
import com.middleware.webservice.interf.SDAPISubmitService;
@Service
@WebService(serviceName = "SDAPISubmitService", // 与接口中指定的name一致
		targetNamespace = "http://webservice.mq.middleware.aac.com", // 与接口中的命名空间一致,一般是接口的包名倒
		endpointInterface = "com.middleware.webservice.interf.SDAPISubmitService" // 接口地址
)
public class SDAPISubmitServiceImpl implements SDAPISubmitService{
	@Autowired 
	Config config;
	Logger logger = LoggerFactory.getLogger(SDAPISubmitServiceImpl.class);
	@Override
	public Map<String, Object> submit(String orderNum,String note,String receiptQuantity,String receiptLocation,String nonReceiptQuantity,String nonReceiptLocation) {
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			String url =config.getBpm()+"/api/submitTask/SDTask.action";
			Map<String, String> parms = new  HashMap<>();
			parms.put("orderNum", orderNum);
			parms.put("note", note);
			parms.put("receiptQuantity", receiptQuantity);
			parms.put("receiptLocation", receiptLocation);
			parms.put("nonReceiptQuantity", nonReceiptQuantity);
			parms.put("nonReceiptLocation", nonReceiptLocation);
			if(!isNull(orderNum) && !isNull(note) && !isNull(receiptQuantity) && !isNull(receiptLocation) && !isNull(nonReceiptQuantity)&& !isNull(nonReceiptLocation)){
				if(isNumeric(receiptQuantity) && isNumeric(receiptLocation) && isNumeric(nonReceiptQuantity) && isNumeric(nonReceiptLocation)){
					String responsMess = SSLHttpHelper.getHelper().post(url, parms);
					result.put("responsMess", responsMess);
				}else {
					result.put("responsMess", "实物收货数量receiptQuantity,实物收货库位receiptLocation,无实物收货数量nonReceiptQuantity,无实物收货库位nonReceiptLocation,输入值只允许输入整数!");
				}
			}else {
				result.put("responsMess", "订单号orderNum,审批意见note,实物收货数量receiptQuantity,实物收货库位receiptLocation,无实物收货数量nonReceiptQuantity,无实物收货库位nonReceiptLocation,输入值均不允许为空!");
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			result.put("responsMess", "接口调用失败,请确认BPM订单号orderNum是否有误或者联系管理员!");
		}
		return result;
	}

	/*
	 * 判断字符串是否为空
	 */
	public static boolean isNull(String str) {
		if ("".equals(str) || str == null) {
			return true;
		}
		return false;
	}

	/*
	 * 利用正则表达式判断字符串是否是数字
	 */
	public boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("^[0-9]*$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}
}

四:发布服务

创建配置类进行发布

WebserviceConfig.java

package com.middleware.webservice;

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 org.springframework.web.context.request.async.TimeoutCallableProcessingInterceptor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.middleware.webservice.interf.SDAPISubmitService;
import javax.xml.ws.Endpoint;

@Configuration
public class WebserviceConfig extends WebMvcConfigurerAdapter{

	 @Autowired
	 private SDAPISubmitService sdAPISubmitService;
	     /**
	     * 注入servlet  bean name不能用dispatcherServlet 否则会覆盖dispatcherServlet
	     * @return
	     */
	    @Bean(name = "cxfServlet")
	    public ServletRegistrationBean cxfServlet() {
	        return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
	    }
	    
        @Bean(name = Bus.DEFAULT_BUS_ID)
	    public SpringBus springBus() {
	        return new SpringBus();
	    }
	    /**
	     * 注册SDAPISubmitService接口到webservice服务
	     * @return
	     */
	    @Bean(name = "SDAPISubmitService")
	    public Endpoint SDAPISubmitServiceEndPoint() {
	        EndpointImpl endpoint = new EndpointImpl(springBus(), sdAPISubmitService);
	        //暂时注释
	        //endpoint.publish("/SDAPISubmitService");
	        //return endpoint;
	        return null;
	    }
	     @Override
	    public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
	        configurer.setDefaultTimeout(100);
	        configurer.registerCallableInterceptors(timeoutInterceptor());
	    }
		@Bean
		public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
	    	return new TimeoutCallableProcessingInterceptor();
		}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值