码迷,mamicode.com
首页 > 编程语言 > 详细

Spring中使用WebService

时间:2015-03-03 13:44:34      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:webservice

Server端和Client端的Web工程截图:

技术分享


技术分享


Server代码:

package com.wiseweb.bean;

public class Order {

	private int id ;
	private String name ;
	private double price ;
	
	public Order() {
		super();
	}
	
	public Order(int id, String name, double price) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Order [id=" + id + ", name=" + name + ", price=" + price + "]";
	}
	
	
}

package com.wiseweb.ws;

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

import com.wiseweb.bean.Order;

@WebService
public interface OrderProcess {
	@WebMethod
	Order getMessById(int id) ;
}

package com.wiseweb.ws;

import com.wiseweb.bean.Order;

public class OrderProcessImpl implements OrderProcess {

	
	public OrderProcessImpl() {
		System.out.println("OrderProcessImpl()");
	}

	@Override
	public Order getMessById(int id) {
		System.out.println("server :" + id);
		return new Order(id,"飞机",100000);
	}

}

package com.wiseweb.ws.interceptor;

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.w3c.dom.Element;

public class CheckUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

	public CheckUserInterceptor() {
		super(Phase.PRE_PROTOCOL);
		System.out.println("CheckUserInterceptor.CheckUserInterceptor()");
	}

	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		Header header = message.getHeader(new QName("wiseweb")) ;
		if(header != null) {
			Element element = (Element)header.getObject() ;
			String username = element.getElementsByTagName("username").item(0).getTextContent() ;
			String password = element.getElementsByTagName("password").item(0).getTextContent() ;
			if(username.equals("wuhaixu") && password.equals("123456")) {
				System.out.println("用户名与密码正确,通过验证!");
				return ;
			}else {
				throw new Fault(new RuntimeException("请输入正确的用户名和密码!")) ;
			}
		}else {
			throw new Fault(new RuntimeException("请输入用户名和密码!")) ;
		}
	}

}

beans.xml

<?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/schemas/jaxws.xsd">
  	 
      <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="orderProcess"
        implementor="com.wiseweb.ws.OrderProcessImpl"
        address="/orderprocess">
        <jaxws:inInterceptors>
        	<bean class="com.wiseweb.ws.interceptor.CheckUserInterceptor">
        	</bean>
        </jaxws:inInterceptors>
      </jaxws:endpoint>
</beans>

Client:

package com.wiseweb.ws.interceptor;

import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

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.w3c.dom.Document;
import org.w3c.dom.Element;

public class AddUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{

	private String username ;
	private String password ;
	
	public AddUserInterceptor(String username, String password) {
		super(Phase.PRE_PROTOCOL);
		this.username = username ;
		this.password = password ;
		System.out.println("AddUserInterceptor()...");
	}

	@Override
	public void handleMessage(SoapMessage message) throws Fault {
		List<Header> headers = message.getHeaders() ;
		
		DocumentBuilder builder = null ;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		Document document = builder.newDocument() ;
		Element root = document.createElement("wiseweb") ;
		Element username = document.createElement("username") ;
		username.setTextContent(this.username);
		Element password = document.createElement("password") ;
		password.setTextContent(this.password);
		root.appendChild(username) ;
		root.appendChild(password) ;
		headers.add(new Header(new QName("wiseweb"), root)) ;
	}

}

package com.wiseweb.ws.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wiseweb.ws.Order;
import com.wiseweb.ws.OrderProcess;

public class ClientTest {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]
		                     {"client-beans.xml"});
		OrderProcess orderProcess = (OrderProcess)context.getBean("orderClient") ;
		Order order = orderProcess.getMessById(230) ;
		System.out.println(order);
	}
}

Client-beans.xml

<?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/schemas/jaxws.xsd">
   
   	<jaxws:client id="orderClient" 
   		serviceClass="com.wiseweb.ws.OrderProcess"	
   		address="http://localhost:8087/day02_ws_cxf_spring/orderprocess">
   		<jaxws:outInterceptors>
   			<bean class="com.wiseweb.ws.interceptor.AddUserInterceptor">
   				<constructor-arg name="username" value="wuhaixu" />
   				<constructor-arg name="password" value="1234567" />
   			</bean>
   		</jaxws:outInterceptors>
	</jaxws:client>
</beans>

把Server端的项目部署并运行,运行Client端。结果为:

Server:

用户名与密码正确,通过验证!
server :230


Client:

AddUserInterceptor()...
Order [id=230, name=飞机, price=100000.0]


Spring中使用WebService

标签:webservice

原文地址:http://blog.csdn.net/benjamin_whx/article/details/44035799

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!