WebService的一些实战

本文详细介绍了如何使用Java创建WebService服务端和客户端,包括接口定义、实现及发布服务的过程。同时,深入探讨了客户端拦截器的编写,并展示了如何利用Spring框架整合WebService服务器和客户端,实现更高效的服务交互。
摘要由CSDN通过智能技术生成

目录

 

一、创建服务端

1、定义要发布的接口

2、定义对应的实现

3、发布服务

二、创建客户端

1、通过jdk自带的wsimport命令生成对应的客户端代码

2、客户端调用服务器端发布的接口

三、编写客户端的拦截器

四、客户端拦截器

五、Spring整合WebService服务器

六、Spring整合WebService服务器


一、创建服务端

1、定义要发布的接口

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

/*
 * SEI:
 */
@WebService
public interface HelloWS {

	@WebMethod
	public String sayHello(String name);
}

2、定义对应的实现

import javax.jws.WebService;

/*
 * SEI的实现
 */
@WebService
public class HelloWSImpl implements HelloWS {

	public String sayHello(String name) {
		System.out.println("server sayHello()"+name);
		return "hello "+name;
	}

}

3、发布服务

import javax.xml.ws.Endpoint;

import day01_ws.ws.HelloWSImpl;

/*
 * 发布web service
 */
public class ServerTest {

	public static void main(String[] args) {
		String address = "http://127.0.0.1:8989/day01_ws/hellows";
		Endpoint.publish(address, new HelloWSImpl());
		System.out.println("发布webservice成功!");
	}
}

二、创建客户端

1、通过jdk自带的wsimport命令生成对应的客户端代码

    wsimport -keep http://127.0.0.1:8989/day01_ws/hellows?wsdl

2、客户端调用服务器端发布的接口

import day01_ws.ws.HelloWSImpl;
import day01_ws.ws.HelloWSImplService;

/*
 * 调用webservice
 */
public class ClientTest {

	public static void main(String[] args) {
		HelloWSImplService factory = new HelloWSImplService();
		HelloWSImpl helloWS = factory.getHelloWSImplPort();
		System.out.println(helloWS.getClass());
		
		String result = helloWS.sayHello("Jack");
		System.out.println("client "+result);
	}
}

三、编写客户端的拦截器

/*
 * 检查用户的拦截器
 */
public class CheckUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

	public CheckUserInterceptor() {
		super(Phase.PRE_PROTOCOL);
	}
	
	/*
	 * <Envelope>
	 *      <head>
	 *          <atguigu>
	 *            <name>xfzhang</name>
	 *            <password>123456</password>
	 *          </atguigu>
	 *          <atguigu2>
	 *            <name>xfzhang</name>
	 *            <password>123456</password>
	 *          </atguigu2>
	 *      </head>
	 *      <Body>
	 *          <sayHello>
	 *              <arg0>BOB</arg0>
	 *          <sayHello>
	 *      </Body>
	 * </Envelope>     
	 * 	 
	*/
	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		Header header = message.getHeader(new QName("atguigu"));
	    if (header!=null) {
		  Element atguiguEle = (Element) header.getObject();
		  String name = atguiguEle.getElementsByTagName("name").item(0).getTextContent();
		  String password = atguiguEle.getElementsByTagName("password").item(0).getTextContent();
	      if ("xfzhang".equals(name)&&"123456".equals(password)) {
			System.out.println("server 通过拦截器...");
		    return;
	      }		  
	    }
	    //不能通过
	    System.out.println("server 没有通过拦截器....");
	    throw new Fault(new RuntimeException("请求需要一个正确的用户名和密码!"));
	}
}

四、客户端拦截器

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.xml.utils.DOMHelper;
import org.w3c.dom.Element;

public class AddUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

	private String name;
	private String password;
	
	public AddUserInterceptor(String name,String password) {
		super(Phase.PRE_PROTOCOL);//准备协议化时拦截
		this.name=name;
		this.password=password;
	}

	/*
	 * <Envelope>
	 *      <head>
	 *          <atguigu>
	 *            <name>xfzhang</name>
	 *            <password>123456</password>
	 *          </atguigu>
	 *          <atguigu2>
	 *            <name>xfzhang</name>
	 *            <password>123456</password>
	 *          </atguigu2>
	 *      </head>
	 *      <Body>
	 *          <sayHello>
	 *              <arg0>BOB</arg0>
	 *          <sayHello>
	 *      </Body>
	 * </Envelope>     
	 * 	 
	*/
	public void handleMessage(SoapMessage msg) throws Fault {
		 List<Header> headers = msg.getHeaders();
		 /*
		  * <atguigu>
	      *    <name>xfzhang</name>
	      *    <password>123456</password>
	      * </atguigu>
		  */
		 org.w3c.dom.Document document = DOMHelper.createDocument();
		 Element rootEle = document.createElement("atguigu");
		 Element nameEle = document.createElement("name");
		 nameEle.setTextContent("name");
		 rootEle.appendChild(nameEle);
		 
		 Element passwordEle = document.createElement("password");
		 nameEle.setTextContent("password");
		 rootEle.appendChild(passwordEle);
		 
		 headers.add(new Header(new QName("atguigu"), rootEle));
		 
		 System.out.println("client handleMessage()...");
	}
}

五、Spring整合WebService服务器

   编写相应的配置文件,其它接口和实现同上所示。发布已经由endpoint做好了,address就是对应的wsdl路径,前面加上项目名和端口即可。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jaxws="http://cxf.apache.org/jaxws"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://cxf.apache.org/jaxws http://cxf.apache.org/jaxws">

<!-- 引cxf的一些核心配置 -->
   <import resource="classpath:META-INF/cxf/cxf.xml" /> 
   <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 

<jaxws:endpoint 
     id="orderWS" 
     implementor="day01_ws_cxf_spring.ws.OrderWSImpl" 
     address="/orderws">
     
     <jaxws:inInterceptors>
     		<bean class="day01_ws.interceptor.CheckUserInterceptor"></bean>
     	</jaxws:inInterceptors>
    </jaxws:endpoint>

</beans>

六、Spring整合WebService服务器

编写对应的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jaxws="http://cxf.apache.org/jaxws"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
   	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/jaxws">
	<jaxws:client id="orderClient" 
		serviceClass= "day01_ws_cxf_spring.ws.OrderWS" 
		address= "http://localhost:8080/day02_ws_cxf_spring/orderws">
		
		
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
			<bean class="day01_ws.ws.interceptor.AddUserInterceptor">
				<constructor-arg name="name" value="xfzhang"/>
				<constructor-arg name="password" value="123456"/>
			</bean>
		</jaxws:outInterceptors>
	</jaxws:client>
</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值