微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ws问题总结三

1、WS_AXIS            :只要加上@WebService即可对外暴露多个方法 

     WS_AXIS_WSS  :只要不加@WebMethod(exclude = true)即可对外暴露多个方法

     WS_CXF             :在接口中加上@WebService,不需要加@WebMethod即可对外暴露多个方法

 

方法参数可以是基本数据类型,或者List<Class>等

package t;

import java.util.List;

import javax.jws.WebService;

@WebService
public interface ITests {
	public String test(String name);
	public String test2(List<TestPojo> name);
}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------分割---------------------------分割---------------------------分割---------------------------分割---------------------------分割---------------------------分割---------------------------分割----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

2、ws客户端

WS_CXF_CLIENT     :b

WS_CXF_CLIENT2   :b

WS_CLIENT_JAVA   :a

 

 

a  >  需要cxf的jar包

package client;

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyfactorybean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicclientFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClasspathXmlApplicationContext;

import t.ITests;
import t.TestPojo;

public class Test {
	ApplicationContext ctx = new ClasspathXmlApplicationContext("applicationContext.xml");
	
	/**
	 * wsdl地址,接口和接口的路径
	 */
	public static void clientToXML(){
		JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean();
		factory.setServiceClass(ITests.class);
		factory.setAddress("http://192.168.1.26:8089/WS_CXF/CxfTest");
		ITests service = (ITests) factory.create();
		TestPojo tp = new TestPojo();
		tp.setAge("22岁");
		tp.setHigh("185米");
		tp.setName("lvxianchao");
		List<TestPojo> list = new ArrayList<TestPojo>();
		list.add(tp);
		list.add(tp);
		list.add(tp);
		String str = service.test2(list);
		System.out.println("str: "+str);
		
		String str2 = service.test("lvxianchao");
		System.out.println("str2: "+str2);

	}
	
	/**
	 * 只有wsdl地址的写法,还有方法名和参数
	 */
	public static void noImplClass(){
		JaxWsDynamicclientFactory dcf = JaxWsDynamicclientFactory.newInstance();  
		String wsUrl = "http://192.168.1.26:8089/WS_CXF/CxfTest?wsdl"; 
		String method = "test";  
		Client client = dcf.createClient(wsUrl); 
		Object[] res = null; 
		try {
			res = client.invoke(method,"lvxianchao");
		} catch (Exception e) {
			e.printstacktrace();
		}  
		System.out.println("res: "+res.length+"\n"+res[0]);

	}
	
	public static void main(String[] args) {
//	    clientToXML();
		noImplClass();
	}
}


 

b  >  cxf客户端的5个核心jar包,spring+cxf

<jaxws:client id="cxfClient" serviceClass="t.ITest" 
        address="http://192.168.1.26:8085/WS_CXF/CxfTest"/>


 

 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------分割---------------------------分割---------------------------分割---------------------------分割---------------------------分割---------------------------分割---------------------------分割---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------3、服务器端(4中好用的)

WS_AXIS:axis自动化程度比较高

WS_AXIS_WSS:axis手写,比较好用

WS_CXF:总结一中写的,最先接触的spring+cxf的服务器端写法(后来又在这个项目中添加了另一种spring+cxf的服务器端写法,可以配置拦截器,区别于最先接触的那种只有applicationContext中的bean组件的配置有些不同,见下↓)

<?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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
						http://www.springframework.org/schema/context 
        				http://www.springframework.org/schema/context/spring-context-3.1.xsd
        				http://www.springframework.org/schema/tx
     					http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
     					http://www.springframework.org/schema/aop 
     					http://www.springframework.org/schema/aop/spring-aop-3.1.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"/>
	
	<!-- 第一种 ==============begin-->
	<bean id="testImpl" class="t.TestImpl">
	</bean>
	<jaxws:endpoint id="cxftest" 
		implementorClass="t.ITests"
		implementor="#testImpl" 
		address="/CxfTest" >
	</jaxws:endpoint> 
	<!-- 第一种  =============end-->
	
	
	<span style="color:#ff0000;"><!-- jaxws-server方式配置cxf服务器端 ===================begin-->
	<bean id="serviceImpl" class="t2.ServiceImpl">
	</bean>
	<bean id="inMessageInterceptor" class="t2.MessageInterceptor">
		<constructor-arg value="receive"></constructor-arg>
	</bean>
	<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
	
	<jaxws:server id="servicer" serviceBean="#serviceImpl" address="/newservice">
		<jaxws:inInterceptors>
			<ref bean="inMessageInterceptor"/>
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor"/>
		</jaxws:outInterceptors>
	</jaxws:server>
	<!-- jaxws-server方式配置cxf服务器端 =====================end-->
</span>
</beans>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐