准备工作:
创建springBoot项目webservice_server
创建springBoot项目webservice_client
分别添加CXF的依赖:
org.apache.cxf cxf-spring-boot-starter-jaxws3.1.11
一.定义要发布的接口和实现类
接口:
@WebService
public interface AppService {
@WebMethod
String getUserName(@WebParam(name = "id") String id) throws UnsupportedEncodingException;
@WebMethod
public User getUser(String id) throws UnsupportedEncodingException;
}
实现类:
//name暴露的服务名称, targetNamespace:命名空间,设置为接口的包名倒写(默认是本类包名倒写). endpointInterface接口地址
@WebService(name = "test" ,targetNamespace ="http://cxf.wolfcode.cn/" ,endpointInterface = "cn.wolfcode.cxf.AppService")
public class AppServiceImpl implements AppService {
JSonResult jsonResult = JSONResult.getJsonResult();
@Override
public String getUserName(String id) throws UnsupportedEncodingException {
System.out.println("==========================="+id);
JSonResult result= JSONResult.getJsonResult();
result.setSuccess(true);
result.setMessage("明哥");
return result.toJsonObject();
}
@Override
public User getUser(String id)throws UnsupportedEncodingException {
System.out.println("==========================="+id);
return new User(1L,"明哥");
}
}
二.发布服务
1.定义配置类
@Configuration
public class CxfConfig {
//默认servlet路径
public class LoginInterceptor extends AbstractPhaseInterceptor {
private String username="root";
private String password="admin";
public LoginInterceptor(String username, String password) {
//设置在发送请求前阶段进行拦截
super(Phase.PREPARE_SEND);
this.username=username;
this.password=password;
}
@Override
public void handleMessage(SoapMessage soapMessage) throws Fault {
List headers = soapMessage.getHeaders();
document doc = DOMUtils.createdocument();
Element auth = doc.createElementNS("http://cxf.wolfcode.cn/","SecurityHeader");
Element UserName = doc.createElement("username");
Element UserPass = doc.createElement("password");
UserName.setTextContent(username);
UserPass.setTextContent(password);
auth.appendChild(UserName);
auth.appendChild(UserPass);
headers.add(0, new Header(new QName("SecurityHeader"),auth));
}
}
4.调用接口
public class Cxfclient {
//webservice接口地址
private static String address = "http://localhost:8888/services/user?wsdl";
//测试
public static void main(String[] args) {
test1();
test2();
}
public static void test1() {
try {
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
//添加用户名密码拦截器
jaxWsProxyFactoryBean.getOutInterceptors().add(new LoginInterceptor("root","admin"));;
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(AppService.class);
// 创建一个代理接口实现
AppService cs = (AppService) jaxWsProxyFactoryBean.create();
// 数据准备
String LineId = "1";
// 调用代理接口的方法调用并返回结果
User result = (User)cs.getUser(LineId);
System.out.println("==============返回结果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test2() {
// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(address);
// 需要密码的情况需要加上用户名和密码
client.getOutInterceptors().add(new LoginInterceptor("root","admin"));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
System.out.println("======client"+client);
objects = client.invoke("getUserName", "1");
System.out.println("返回数据:" + objects[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
嗯...总体上就是这么简单, 演示代码可以去这里下载:http://xz.jb51.net:81/201808/yuanma/springBoot_WebService_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



