spring boot 创建和发布web service接口

一:创建springboot项目
二:导入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>

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

SD01APISubmitService.java

package com.aac.middleware.webservice.interf;

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

@WebService
public interface SD01APISubmitService {
	@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);
}

SD01APISubmitServiceImpl.java

package com.aac.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.aac.middleware.common.Config;
import com.aac.middleware.util.MD5Util;
import com.aac.middleware.util.SSLHttpHelper;
import com.aac.middleware.webservice.interf.SD01APISubmitService;
@Service
@WebService(serviceName = "SD01APISubmitService", // 与接口中指定的name一致
		targetNamespace = "http://webservice.mq.middleware.aac.com", // 与接口中的命名空间一致,一般是接口的包名倒
		endpointInterface = "com.aac.middleware.webservice.interf.SD01APISubmitService" // 接口地址
)
public class SD01APISubmitServiceImpl implements SD01APISubmitService{
	@Autowired Config config;
	Logger logger = LoggerFactory.getLogger(SD01APISubmitServiceImpl.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/SD01Task.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", "BPM订单号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.aac.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.aac.middleware.webservice.interf.SD01APISubmitService;
import javax.xml.ws.Endpoint;

@Configuration
public class WebserviceConfig extends WebMvcConfigurerAdapter{

	 @Autowired
	 private SD01APISubmitService sd01APISubmitService;
	     /**
	     * 注入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();
	    }
	    /**
	     * 注册SD01APISubmitService接口到webservice服务
	     * @return
	     */
	    @Bean(name = "SD01APISubmitService")
	    public Endpoint SD01APISubmitServiceEndPoint() {
	        EndpointImpl endpoint = new EndpointImpl(springBus(), sd01APISubmitService);
	        //暂时注释
	        //endpoint.publish("/SD01APISubmitService");
	        //return endpoint;
	        return null;
	    }
	     @Override
	    public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
	        configurer.setDefaultTimeout(100);
	        configurer.registerCallableInterceptors(timeoutInterceptor());
	    }
		@Bean
		public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
	    	return new TimeoutCallableProcessingInterceptor();
		}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章