spring cxf 注解和头部验证

转载自:http://canken007.iteye.com/blog/1729547     http://www.blogjava.net/zljpp/archive/2012/04/15/374372.html


我想吐槽下国内的有些人,全是标题党,不会吗就不要写,乱七八槽,找个问题百度一出来,一大堆,找了一个不行,有个家伙说cxf注解详解,进去之后就几行字,还要了七分,我想说何必要那么装逼。忙活了一下午终于搞定Spring,cxf注解的试验,如果你有问题可以直接电话或者邮件给我。

注解:

1、 @WebService annotation 的元素 name,serviceName 和 targetNamespace 成员用来描述 
   wsdl:portType, wsdl:service ,和 targetNameSpace 生成 WebService 中的 WSDL 文件。 
2、 @SOAPBinding 是一个用来描述 SOAP 格式和 RPC 的协议的绑定 Annotation 。 
3、 @WebMethod Annotation 的 operationName 成员描述了 wsdl:operation ,而且它的操作描  
   述了WSDL 文档中的 SOAPAction 头部。这是客户端必须要放入到 SQAPHeader 中的数 
   值,SOAP 1.1 中的一种约束。 
4、 @WebParam Annotation 的 partName 成员描述了 WSDL 文档中的 wsdl:part 。 
5、 @WebResult Annotation 的 partName 成员描述了 wsdl:part 用来返回 WSDL 文档的值。 
@WebService(targetNamespace="http://com.shenhua.servicecxf/") 
public interface  Hello {
public String hello(@WebParam(name = "username")String username);


}


@WebService(serviceName = "/webserviceHello",endpointInterface = "com.server.dao.Hello",targetNamespace="http://com.shenhua.servicecxf/")
@SOAPBinding(style = Style.RPC) 
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public class HelloImpl implements Hello  {


result我没有定义。



主要是配置文件,

web.xml

<?xml version="1.0" encoding="UTF-8"?>


<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee


    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <welcome-file-list>


       <welcome-file>index.jsp</welcome-file>


    </welcome-file-list>


 


    <servlet>


       <description>apache cxf 配置 webservice 服务</description>


       <servlet-name>cxf</servlet-name>


       <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>


       <load-on-startup>1</load-on-startup>


    </servlet>


    <servlet-mapping>


       <servlet-name>cxf</servlet-name>


       <url-pattern>/services/*</url-pattern>


    </servlet-mapping>


 


    <listener>


       <description>spring 的监听</description>


       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>


    </listener>


    <context-param>


       <description>spring 的配置文件加载路径</description>


       <param-name>contextConfigLocation</param-name>


       <param-value>classpath*:applicationContext*.xml</param-value>


    </context-param>


</web-app>

第二个配置文件--有头部拦截器

applicationContext.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">
       <description>Spring公共配置 </description>  


    <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" />
<bean id="authIntercetpr" class="org.web.soapHeader.AuthIntercetpr"></bean>
    <jaxws:endpoint id="service" implementor="com.server.service.HelloImpl" address="/webserviceHello">
<!-- 在此配置调用当前ws所触发的拦截器-->
<jaxws:inInterceptors><ref bean="authIntercetpr" />

<!--或者直接在这里写<bean  class="unitTest.AuthIntercetpr"></bean>-->
</jaxws:inInterceptors>
 
</jaxws:endpoint>
   


</beans>

soapui请求的报文

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:com="http://com.shenhua.servicecxf/">
<soap:Header>
<auth:authentication xmlns:auth="http://gd.chinamobile.com//authentication">
<auth:systemID>1</auth:systemID>
<auth:userID>test</auth:userID>
<auth:password>test</auth:password>
</auth:authentication>
</soap:Header>
   <soap:Body>
      <com:hello>
         <!--Optional:-->
         <username>1</username>
      </com:hello>
   </soap:Body>
</soap:Envelope>



三,这边同样创建一个拦截器,实现org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.helpers.XMLUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class AddSoapHeader extends AbstractSoapInterceptor {

	public static final String xml_namespaceUR_att = "http://gd.chinamobile.com//authentication";
	public static final String xml_header_el = "soap:Header";
	public static final String xml_authentication_el = "auth:authentication";
	public static final String xml_systemID_el = "auth:systemID";
	public static final String xml_userID_el = "auth:userID";
	public static final String xml_password_el = "auth:password";

	public AddSoapHeader() {
	// 指定该拦截器在哪个阶段被激发
		super(Phase.WRITE);
	}

	public void handleMessage(SoapMessage message) throws Fault {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String time = sd.format(date);
		String userId = "test";
		String sysId = "1";
		String password = "test";

		QName qname = new QName("RequestSOAPHeader");//这个值暂时不清楚具体做什么用,可以随便写

		Document doc = (Document) DOMUtils.createDocument();
		Element root = doc.createElement(xml_header_el);
		Element eSysId = doc.createElement(xml_systemID_el);
		eSysId.setTextContent(sysId);
		Element eUserId = doc.createElement(xml_userID_el);
		eUserId.setTextContent(userId);
		Element ePwd = doc.createElement(xml_password_el);
		ePwd.setTextContent(password);
		Element child = doc.createElementNS(xml_namespaceUR_att,
				xml_authentication_el);
		child.appendChild(eSysId);
		child.appendChild(eUserId);
		child.appendChild(ePwd);
		root.appendChild(child);
		XMLUtils.printDOM(root);// 只是打印xml内容到控制台,可删除
		SoapHeader head = new SoapHeader(qname, root);
		List<Header> headers = message.getHeaders();
		headers.add(head);
		
	}

}

四,具体调用ws的类代码


	private static final String webServiceConTimeout = "6000";
	private static final String webServiceRevTimeout = "6000";
	。。。。。。。
        HelloWorldServiceImplService hello = new HelloWorldServiceImplService();  
        HelloWorldService service = hello.getHelloWorldServiceImplPort();
        //以上什么意思请参考:http://learning.iteye.com/admin/blogs/1333223
		Client clientProxy = ClientProxy.getClient(service);//通过目标ws获取代理
		//注入拦截器,getOutInterceptors代表调用服务端时触发,getInInterceptors就是被调用才触发
		clientProxy.getOutInterceptors().add(ash);
		// 超时时间设置
		HTTPConduit http = (HTTPConduit) clientProxy.getConduit();
		HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
		httpClientPolicy.setConnectionTimeout(Integer
				.valueOf(webServiceConTimeout));
		httpClientPolicy.setReceiveTimeout(Integer
				.valueOf(webServiceRevTimeout));
		httpClientPolicy.setAllowChunking(false);
		http.setClient(httpClientPolicy);
        //以上插入点超时设置方式
		//下面这行代码是具体调用服务段的deleteTeskTask()
		CallResult cResult = service.deleteTeskTask("1223");
				
		。
		。
		
客户端代码到此结束

五,还有一种方式是通过JaxWsProxyFactoryBean方式,注册拦截器及实例化ws,代码如下:

private static final JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

AddSoapHeader ash = new AddSoapHeader();
		ArrayList list = new ArrayList();
		// 添加soap header 信息
		list.add(ash);
		//注入拦截器,getOutInterceptors代表调用服务端时触发,getInInterceptors就是被调用才触发
		 factory.setOutInterceptors(list);
		 factory.setServiceClass(HelloWorldService.class);//实例化ws
		 factory.setAddress("http://xxx.xxx.xxx.xxx:8004/services/IHelloService");
		 Object obj = factory.create();
		 HelloWorldService service = (HelloWorldService) obj;
		 //下面这行代码是具体调用服务段的deleteTeskTask()
		CallResult cResult = service.deleteTeskTask("1223");
		
##########这段代码可替代步骤(四)#####

到此全部工作结束
具体一些概念还请自己baidu/google


先写到这里了,快下班了

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页