Spring boot 集成WebService服务

Maven引入依赖

		
        
            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.2.4
        

编写服务端接口

package com.jeesite.modules.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * @Desc 电子关锁WebService
 * @author PGQing
 *
 */
@WebService(name="lockservice",targetNamespace="http://webservice.modules.jeesite.com")
public interface ElectronicsLockService {

	/**
	 * @DEsc 电子关锁施锁服务
	 * @param userName 用户姓名
	 * @param userIdentityCard 用户身份证号
	 * @param phoneNum 用户手机号
	 * @param passward 用户密码
	 * @param lock_id 电子关锁ID
	 * @param box_num 集装箱号码
	 * @param car_num 车牌号码
	 * @return 施封结果:0-施封成功  1-施封失败
	 */
	@WebMethod
	public int SealLock(@WebParam(name = "userName") String userName,
			@WebParam(name = "userIdentityCard") String userIdentityCard, @WebParam(name = "phoneNum") String phoneNum,
			@WebParam(name = "passward") String passward, @WebParam(name = "lock_id") String lock_id,
			@WebParam(name = "box_num") String box_num, @WebParam(name = "car_num") String car_num);

	/**
	 * @Desc 电子关锁解锁服务
	 * @param userName 用户姓名
	 * @param userIdentityCard 用户身份证号
	 * @param phoneNum 用户手机号
	 * @param passward 用户密码
	 * @param lock_id 用户密码
	 * @param box_num 集装箱号码
	 * @param car_num 车牌号码
	 * @return 施封结果:0-施封成功  1-施封失败
	 */
	@WebMethod
	public int UnSealLock(@WebParam(name = "userName") String userName,
			@WebParam(name = "userIdentityCard") String userIdentityCard, @WebParam(name = "phoneNum") String phoneNum,
			@WebParam(name = "passward") String passward, @WebParam(name = "lock_id") String lock_id,
			@WebParam(name = "box_num") String box_num, @WebParam(name = "car_num") String car_num);
}

编写服务端实现类

package com.jeesite.modules.webservice;

import java.util.List;
import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.jeesite.common.utils.SpringUtils;
import com.jeesite.modules.quarantine.lock.entity.JnhgWsLock;
import com.jeesite.modules.quarantine.lock.service.JnhgWsLockService;
import com.jeesite.modules.quarantine.transplan.entity.JnhgTransplan;
import com.jeesite.modules.quarantine.transplan.service.JnhgTransplanService;
import com.jeesite.modules.utils.StrUtils;

/**
 * @Desc 电子关锁WebService 施封 、 解封
 * @author PGQing
 */
@WebService(serviceName = "lockservice", // 与接口中指定的name一致
		targetNamespace = "http://webservice.modules.jeesite.com", // 与接口中的命名空间一致,一般是接口的包名倒
		endpointInterface = "com.jeesite.modules.webservice.ElectronicsLockService"// 接口地址
)
@Component
public class ElectronicsLockServiceImpl implements ElectronicsLockService {

	@Override
	public int SealLock(String userName, String userIdentityCard, String phoneNum, String passward, String lock_id,
			String box_num, String car_num) {
		// 校验参数
		int result = 0;
		result = StrUtils.ifynotnull(userName, userIdentityCard, phoneNum, passward, lock_id, box_num, car_num);
		if (result == 0) {
			return result;
		}
		// 1.更新运输计划的运输状态
		JnhgTransplanService jnhgTransplanService = SpringUtils.getBean(JnhgTransplanService.class);
		JnhgWsLockService jnhgWsLockService = SpringUtils.getBean(JnhgWsLockService.class);
		String planId = "";
		JnhgTransplan jnhgTransplan = new JnhgTransplan();
		jnhgTransplan.setStatus("0");
		// 2.查询所有的运输计划,按照时间倒序排列,获取第一个。
		List list = jnhgTransplanService.findList(jnhgTransplan);
		if (null != list && !list.isEmpty()) {
			JnhgTransplan tempObj = list.get(0);
			planId = tempObj.getId();// 获取计划ID
			tempObj.setTransstatus("1");// 运输状态改为"开始运输"
			jnhgTransplanService.update(tempObj);
			// 3.将加锁的数据入库
			JnhgWsLock jnhgWsLock = new JnhgWsLock();
			jnhgWsLock.setUsername(userName);// 用户名
			jnhgWsLock.setUseridentitycard(userIdentityCard);// 用户身份证号
			jnhgWsLock.setPhonenum(phoneNum);// 手机号
			jnhgWsLock.setPassword(passward);// 用户密码
			jnhgWsLock.setLockid(lock_id);// 电子关锁ID
			jnhgWsLock.setBoxnum(box_num);// 集装箱号码
			jnhgWsLock.setCarnum(car_num);// 车牌号码
			jnhgWsLock.setStatus("0");
			jnhgWsLock.setLockstatus("0");
			jnhgWsLock.setTransplanid(planId);// 运输计划ID
			jnhgWsLockService.save(jnhgWsLock);
		} else {
			result = 0;
			System.out.println("加锁:没有获取到计划信息");
		}
		return result;
	}

	@Override
	public int UnSealLock(String userName, String userIdentityCard, String phoneNum, String passward, String lock_id,
			String box_num, String car_num) {
		// 校验参数
		int result = 0;
		result = StrUtils.ifynotnull(userName, userIdentityCard, phoneNum, passward, lock_id, box_num, car_num);
		if (result == 0) {
			return result;
		}
		// 1.更新运输计划的运输状态
		String planId = "";
		JnhgTransplanService jnhgTransplanService = SpringUtils.getBean(JnhgTransplanService.class);
		JnhgWsLockService jnhgWsLockService = SpringUtils.getBean(JnhgWsLockService.class);
		JnhgTransplan jnhgTransplan = new JnhgTransplan();
		jnhgTransplan.setStatus("0");
		// 2.查询所有的运输计划,按照时间倒序排列,获取第一个。
		List list = jnhgTransplanService.findList(jnhgTransplan);
		if (null != list && !list.isEmpty()) {
			JnhgTransplan tempObj = list.get(0);
			planId = tempObj.getId();
			tempObj.setTransstatus("2");// 运输状态改为"运输完成"
			jnhgTransplanService.update(tempObj);
			// 3.根据传递的数据查询、并且更新电子关锁记录的加锁、释锁状态
			JnhgWsLock lock = new JnhgWsLock();
			lock.setTransplanid(planId);// 运输计划ID
			lock.setUsername(userName);// 用户名
			lock.setUseridentitycard(userIdentityCard);// 用户身份证号
			lock.setPhonenum(phoneNum);// 手机号
			lock.setPassword(passward);// 用户密码
			lock.setLockid(lock_id);// 电子关锁ID
			lock.setBoxnum(box_num);// 集装箱号码
			lock.setCarnum(car_num);// 车牌号码
			lock.setStatus("0");// 状态:正常
			lock.setLockstatus("0");// 加锁状态
			List lockList = jnhgWsLockService.findList(lock);
			if (null != lockList && !lockList.isEmpty()) {
				JnhgWsLock tempLock = lockList.get(0);
				tempLock.setLockstatus("1");// 锁状态:解锁
				jnhgWsLockService.update(tempLock);
			}
		} else {
			result = 0;
			System.out.println("加锁:没有获取到计划信息");
		}
		return result;
	}

}

编写发布

package com.jeesite.modules.webservice;

import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CxfConfig {

	@Autowired
	private Bus bus;

	@Autowired
	private ElectronicsLockService electronicsLockService;

	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpoint = new EndpointImpl(bus, electronicsLockService);
		endpoint.publish("/lockservice");
		System.out.println("lockservice发布··");
		return endpoint;
	}

}

由于WebService发布后,Spring Boot无法注入我们要用的Service,所以我单独写了一个获取上下文的工具类,当然也有人说可以注入,反正我是没成功。

获取上下文工具类

package com.jeesite.modules.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtils implements ApplicationContextAware {

	private static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		if (null == applicationContext) {
			applicationContext = applicationContext;
		}
	}

	/**
	 * 获取applicationContext
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * 通过name获取 Bean.
	 * 
	 * @param name
	 * @return
	 */
	public static Object getBean(String name) {
		return getApplicationContext().getBean(name);
	}

	/**
	 * 通过class获取Bean.
	 * 
	 * @param clazz
	 * @param       
	 * @return
	 */
	public static  T getBean(Class clazz) {
		return getApplicationContext().getBean(clazz);
	}

	/**
	 * 通过name,以及Clazz返回指定的Bean
	 * 
	 * @param name
	 * @param clazz
	 * @param       
	 * @return
	 */
	public static  T getBean(String name, Class clazz) {
		return getApplicationContext().getBean(name, clazz);
	}

}

启动的时候无需多加额外代码,我们启动项目后,Spring Boot会自动发布。发布后的访问地址和以往的不太一样,地址格式: http://IP:PORT/项目名/services/发布WebService名字?wsdl,比如我的就是http://127.0.0.1:8085/jyzs/services/lockservice?wsdl。
打开浏览器输入地址,会看到你接口中的方法和参数,如下:
Spring boot 集成WebService服务_第1张图片

你可能感兴趣的:(WebService)