Springboot+CFX+WebService(Springboot通过CFX方式集成WebService)

1.导入maven依赖


		org.springframework.boot   
		spring-boot-starter-parent   
		2.0.3.RELEASE   
		
	

	org.apache.cxf
	cxf-rt-frontend-jaxws
	3.2.6


	org.apache.cxf
	cxf-rt-transports-http
	3.2.6

注:Springboot-parent-2.0.3<<==>>WebService-CFX-3.2.6 暂时最高版本支持(有版本对应关系)
2.服务接口

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
import com.alibaba.fastjson.JSONException;
/**
 *@name:接口的服务名称(自定)
 *@targetNamespace:接口命名空间(一般是接口包名的倒序)
 */
@WebService(name="LoginService",targetNamespace="http://android.znv.com")
public interface LoginService {
	
	@WebMethod(operationName="login",action="http://android.znv.com/login")//operationName:服务的接口名(发布的方法名)
	@WebResult(name = "ns:return")//返回的标签
	public int login(@WebParam(name = "username", targetNamespace = "http://android.znv.com")String username, @WebParam(name = "password", targetNamespace = "http://android.znv.com")String password) throws JSONException;
}

3.服务接口的实现

import javax.jws.WebService;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONException;
import com.znv.android.LoginService;
import lombok.extern.slf4j.Slf4j;
/*
 * @name:接口的服务名称
 * @targetNamespace:接口命名空间
 * @endpointInterface:接口地址
 * 
 * */
@Slf4j
@WebService(name="ULoginService",
			targetNamespace="http://android.znv.com",
			endpointInterface="com.znv.android.LoginService")
@Component
@EnableAutoConfiguration
public class LoginServiceImpl implements LoginService{
	
	@Override
	public boolean login(String username, String password){
		boolean bool = false;
		if("admin".equals(username) && "123456".equals(password)){
			bool = true;
		}
		return bool;
	}
}

4.CFX配置

import javax.xml.ws.Endpoint;

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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import com.znv.wpu.service.impl.LoginServiceImpl;
import com.znv.android.LoginService;

/*
 * WebServiceConfig配置发布服务
 * */
@Configuration
public class WebServiceConfig {

	//WebServiced发布调用接口
	@Bean
	public ServletRegistrationBean dispatcherServlet() {
		System.out.println("dispatcherServlet");
		return new ServletRegistrationBean(new CXFServlet(), "/services/*");//自己的WebService根访问路径
	}
	
	//支持Springboot注解调用接口
	@Bean
	public ServletRegistrationBean dispatcherRestServlet() {
		System.out.println("dispatcherRestServlet");
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        //替换成自己的controller包路径
        context.scan("com.tiger.restWs.controller");
        DispatcherServlet disp = new DispatcherServlet(context);
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(disp);
        registrationBean.setLoadOnStartup(1);
        //映射路径自定义,必须设置一个不重复的名称
        registrationBean.addUrlMappings("/rest/*");//自己的Springboot注解访问的根路径
        registrationBean.setName("rest");
        return registrationBean;
    } 

	@Bean(name = Bus.DEFAULT_BUS_ID)
	public SpringBus springBus() {
		return new SpringBus();
	}
	
	//注册用户登录服务
	@Bean
	public LoginService LoginService() {
		return new LoginServiceImpl();
	}
	
	//发布登陆服务
	@Bean
	public Endpoint endpointLogin() {
		EndpointImpl endpoint = new EndpointImpl(springBus(), LoginService());
		endpoint.publish("/UserSession");//接口前缀路径
		return endpoint;
	}
}

客户端(手机APP)服务发布查看路径:http://ip地址:端口/services?wsdl (查看客户端的发布服务)

PC端查看路径:http://ip地址:端口/rest/你的注解端口

你可能感兴趣的:(Java,Android)