WebService对外提供接口

记录一次给老系统提供Webservice接口,话不多说,如有不足之处,请各位大佬纠正!

Pom.xml文件导入所需依赖的配置

	
		
            org.apache.cxf
            cxf-rt-frontend-jaxws
            3.1.12
        
        
            org.apache.cxf
            cxf-rt-transports-http
            3.1.12
        

定义一个接口类定添加WebService注解添加一个addUser方法,在该方法头上添加@WebMethod注解

@WebParam为请求参数名,

targetNamespace为接口与实现类包名的倒序,比如报名路径com.adb.demo.service, 那targetNamespace = "http://service.demo.adb.com"

这里入参方式较多可以考虑对象等其他方式,

@WebService
public interface IUserService {
	
	
	@WebMethod
	public  String addUser(@WebParam(name="loginname",targetNamespace="http://service.soap.enrol.shtelcom.productivetech.cn/") String loginname
			,@WebParam(name="username",targetNamespace="http://service.soap.enrol.shtelcom.productivetech.cn/") String username
			,@WebParam(name="department",targetNamespace="http://service.soap.enrol.shtelcom.productivetech.cn/") String department
			,@WebParam(name="sslkey",targetNamespace="http://service.soap.enrol.shtelcom.productivetech.cn/") String sslkey);

实现类代码

public class UserServiceImpl implements IUserService {
	private final static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); 
	private IAppService appservice = null;

	public UserServiceImpl() {
		this.appservice = SpringBeanUtils.getBean(IAppService.class);
	}

	public UserServiceImpl(IAppService appService) {
		this.appservice = appService;
	}

	// 用户新增
	@Override
	public String addUser(String loginname, String username, String department, String sslkey) {
		
		UserBean users = appservice.findByName(loginname);
		
		if (users != null) {
			logger.error("Funcation addUser——————————》The user already exists");
			return "0";
		}

注意此处实现类中是无法注入Dao或者Service层添加注解也没有用,

注解不起作用,只好用代码初始化资源,从类路径ClassPath中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作

@Component
public class SpringBeanUtils  implements ApplicationContextAware  {
	private static ApplicationContext context;

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}

	public static  T getBean(Class requiredType) {

		if (context == null) {
			throw new IllegalStateException("spring 环境没有启动!");
		}
		return context.getBean(requiredType);
	}

	public static  T getBean(String beanName, Class requiredType) {

		if (context == null) {
			throw new IllegalStateException("spring 环境没有启动!");
		}
		return context.getBean(beanName, requiredType);

	}

	public static ApplicationContext getContext() {

		if (context == null) {
			throw new IllegalStateException("spring 环境没有启动!");
		}
		return context;
	}



}

还需要创建一个WebseriviceConfig配置类来进行发布

@Configuration
public class WebServiceConfig {
	
	@Autowired
	private IAppService appService;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Bean(name="soap")
	public ServletRegistrationBean dispatcherServlet() {
		ServletRegistrationBean soapServletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/services/*");
		return soapServletRegistrationBean;
	}

	@Bean(name = Bus.DEFAULT_BUS_ID)
	public SpringBus springBus() {
		SpringBus bus = new SpringBus();
		ServletDestinationFactory destinationFactory = new ServletDestinationFactory();
		bus.setExtension(destinationFactory, HttpDestinationFactory.class);
		return bus;
	}

	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
		endpoint.publish("/user");
		return endpoint;
	}
	
	@Bean
     public UserServiceImpl userService() {
         return new UserServiceImpl(appService);
     }
}

最后浏览器请求地址http://localhost:4568/services/user?wsdl(这里端口修改成4568了)

WebService对外提供接口_第1张图片

最后发布成功了,用postman进行请求

WebService对外提供接口_第2张图片

请求成功

WebService对外提供接口_第3张图片

各位,有什么不足之处还请纠正!

你可能感兴趣的:(boot)