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();
		}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章