WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单

现在的项目中需要用到SOA概念的地方越来越多,最近我接手的一个项目中就提出了这样的业务要求,需要在.net开发的客户端系统中访问java开发的web系统,这样的业务需求自然需要通过WebService进行信息数据的操作。下面就将我们在开发中摸索的一点经验教训总结以下,以供大家参考.

WebService开发笔记 2 -- VS 2005 访问WebServcie更简单中作一个跨平台访问WebServcie服务的例子....

WebService开发笔记 3 -- 增强访问 WebService 的安全性通过一个简单的用户口令验证机制来加强一下WebService的安全性....

我们项目的整个架构使用的比较流行的WSH MVC组合,即webwork2 + Spring + Hibernate;
1.首先集成Apacha CXF WebService 到 Spring 框架中;
apache cxf 下载地址:http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip
在spring context配置文件中引入以下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" />

在web.xml中添加过滤器:
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>


2.开发服务端WebService接口:
/**
 * WebService接口定义类.
 * 
 * 使用@WebService将接口中的所有方法输出为Web Service.
 * 可用annotation对设置方法、参数和返回值在WSDL中的定义.
 */
@WebService
public interface WebServiceSample {


	/**
	 * 一个简单的方法,返回一个字符串
	 * @param hello
	 * @return
	 */
	String say(String hello);
	
	/**
	 * 稍微复杂一些的方法,传递一个对象给服务端处理
	 * @param user
	 * @return
	 */
	String sayUserName(
			@WebParam(name = "user") 
			UserDTO user);
	
	/**
	 * 最复杂的方法,返回一个List封装的对象集合
	 * @return
	 */
	public 
	@WebResult(partName="o")
	ListObject findUsers();

}

由简单到复杂定义了三个接口,模拟业务需求;

3.实现接口
/**
 * WebService实现类.
 * 
 * 使用@WebService指向Interface定义类即可.
 */
@WebService(endpointInterface = "cn.org.coral.biz.examples.webservice.WebServiceSample")
public class WebServiceSampleImpl implements WebServiceSample {

	public String sayUserName(UserDTO user) {
		return "hello "+user.getName();
	}

	public String say(String hello) {
		return "hello "+hello;
	}

	public ListObject findUsers() {
		ArrayList<Object> list = new ArrayList<Object>();
		
		list.add(instancUser(1,"lib"));
		list.add(instancUser(2,"mld"));
		list.add(instancUser(3,"lq"));
		list.add(instancUser(4,"gj"));
		ListObject o = new ListObject();
		o.setList(list);
		return o;
	}
	
	private UserDTO instancUser(Integer id,String name){
		UserDTO user = new UserDTO();
		user.setId(id);
		user.setName(name);
		return user;
	}
}


4.依赖的两个类:用户对象与List对象
/**
 * Web Service传输User信息的DTO.
 * 
 * 分离entity类与web service接口间的耦合,隔绝entity类的修改对接口的影响.
 * 使用JAXB 2.0的annotation标注JAVA-XML映射,尽量使用默认约定.
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User")
public class UserDTO {

	protected Integer id;

	protected String name;

	public Integer getId() {
		return id;
	}

	public void setId(Integer value) {
		id = value;
	}

	public String getName() {
		return name;
	}

	public void setName(String value) {
		name = value;
	}
}

关于List对象,参照了有关JWS的一个问题中的描述:DK6.0 自带的WebService中 WebMethod的参数好像不能是ArrayList 或者其他List
传递List需要将List 包装在其他对象内部才行 (个人理解 如有不对请指出) ,我在实践中也遇到了此类问题.通过以下封装的对象即可以传递List对象.
/**
 * <p>Java class for listObject complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * <complexType name="listObject">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <sequence>
 *         <element name="list" type="{http://www.w3.org/2001/XMLSchema}anyType" maxOccurs="unbounded" minOccurs="0"/>
 *       </sequence>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "listObject", propOrder = { "list" })
public class ListObject {

	@XmlElement(nillable = true)
	protected List<Object> list;

	/**
	 * Gets the value of the list property.
	 * 
	 * <p>
	 * This accessor method returns a reference to the live list,
	 * not a snapshot. Therefore any modification you make to the
	 * returned list will be present inside the JAXB object.
	 * This is why there is not a <CODE>set</CODE> method for the list property.
	 * 
	 * <p>
	 * For example, to add a new item, do as follows:
	 * <pre>
	 *    getList().add(newItem);
	 * </pre>
	 * 
	 * 
	 * <p>
	 * Objects of the following type(s) are allowed in the list
	 * {@link Object }
	 * 
	 * 
	 */
	public List<Object> getList() {
		if (list == null) {
			list = new ArrayList<Object>();
		}
		return this.list;
	}

	public void setList(ArrayList<Object> list) {
		this.list = list;
	}

}


5.WebService 服务端 spring 配置文件 ws-context.xml
<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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd"
	default-autowire="byName" default-lazy-init="true">
	
	<jaxws:endpoint id="webServiceSample"
		address="/WebServiceSample" implementor="cn.org.coral.biz.examples.webservice.WebServiceSampleImpl"/>

</beans>


WebService 客户端 spring 配置文件 wsclient-context.xml
<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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd"
	default-autowire="byName" default-lazy-init="true">

	<!-- ws client -->
	<bean id="identityValidateServiceClient" class="cn.org.coral.admin.service.IdentityValidateService"
		factory-bean="identityValidateServiceClientFactory" factory-method="create" />

	<bean id="identityValidateServiceClientFactory"
		class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass"
			value="cn.org.coral.admin.service.IdentityValidateService" />
		<property name="address"
			value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/>
	</bean>
	
</beans>

6.发布到tomcat服务器以后通过以下地址即可查看自定义的webservice接口生成的wsdl:
http://88.148.29.54:8080/aio/services/WebServiceSample?wsdl

7.调用WebService接口的Junit单元测试程序
package test.coral.sample;

import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

import cn.org.coral.biz.examples.webservice.WebServiceSample;
import cn.org.coral.biz.examples.webservice.dto.UserDTO;

public class TestWebServiceSample extends
		AbstractDependencyInjectionSpringContextTests {
	WebServiceSample webServiceSampleClient;

	public void setWebServiceSampleClient(WebServiceSample webServiceSampleClient) {
		this.webServiceSampleClient = webServiceSampleClient;
	}
	
	@Override
	protected String[] getConfigLocations() {
		setAutowireMode(AUTOWIRE_BY_NAME);
                  //spring 客户端配置文件保存位置
		return new String[] { "classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml" };
	}
	
	public void testWSClinet(){
		Assert.hasText(webServiceSampleClient.say(" world"));
	}
}

  • 10563c25-450f-3b59-9125-f0d191cec4c8-thumb
  • 描述:
  • 大小: 152.6 KB
评论
tiyi 2008-04-23
居然每个方法的请求和返回值都包装成类型……太变态了
gigi_ly180 2008-04-22
异常如上
gigi_ly180 2008-04-22
信息: Interceptor has thrown exception, unwinding now
org.apache.cxf.binding.soap.SoapFault: Error writing to XMLStreamWriter.
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor.handleMessage(SoapOutInterceptor.java:244)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor.handleMessage(SoapOutInterceptor.java:226)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:207)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:254)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:205)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:135)
at $Proxy18.sayHelloWorld(Unknown Source)
at net.oupu.test.Test.main(Test.java:30)
Caused by: com.ctc.wstx.exc.WstxIOException: Connection refused: connect
at com.ctc.wstx.sw.BaseStreamWriter.finishDocument(BaseStreamWriter.java:1687)
at com.ctc.wstx.sw.BaseStreamWriter.writeEndDocument(BaseStreamWriter.java:585)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor.handleMessage(SoapOutInterceptor.java:239)
... 8 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.Socket.connect(Socket.java:519)
at sun.net.NetworkClient.doConnect(NetworkClient.java:152)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:382)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:509)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:231)
at sun.net.www.http.HttpClient.New(HttpClient.java:304)
at sun.net.www.http.HttpClient.New(HttpClient.java:316)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:813)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:765)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:690)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:857)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleHeadersTrustCaching(HTTPConduit.java:1787)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.onFirstWrite(HTTPConduit.java:1755)
at org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:42)
at com.ctc.wstx.io.UTF8Writer.flush(UTF8Writer.java:96)
at com.ctc.wstx.sw.BufferingXmlWriter.flush(BufferingXmlWriter.java:214)
at com.ctc.wstx.sw.BufferingXmlWriter.close(BufferingXmlWriter.java:194)
at com.ctc.wstx.sw.BaseStreamWriter.finishDocument(BaseStreamWriter.java:1685)
... 10 more
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Error writing to XMLStreamWriter.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:169)
at $Proxy18.sayHelloWorld(Unknown Source)
at net.oupu.test.Test.main(Test.java:30)
Caused by: org.apache.cxf.binding.soap.SoapFault: Error writing to XMLStreamWriter.
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor.handleMessage(SoapOutInterceptor.java:244)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor.handleMessage(SoapOutInterceptor.java:226)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:207)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:254)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:205)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:135)
... 2 more
Caused by: com.ctc.wstx.exc.WstxIOException: Connection refused: connect
at com.ctc.wstx.sw.BaseStreamWriter.finishDocument(BaseStreamWriter.java:1687)
at com.ctc.wstx.sw.BaseStreamWriter.writeEndDocument(BaseStreamWriter.java:585)
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor.handleMessage(SoapOutInterceptor.java:239)
... 8 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.Socket.connect(Socket.java:519)
at sun.net.NetworkClient.doConnect(NetworkClient.java:152)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:382)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:509)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:231)
at sun.net.www.http.HttpClient.New(HttpClient.java:304)
at sun.net.www.http.HttpClient.New(HttpClient.java:316)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:813)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:765)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:690)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:857)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleHeadersTrustCaching(HTTPConduit.java:1787)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.onFirstWrite(HTTPConduit.java:1755)
at org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:42)
at com.ctc.wstx.io.UTF8Writer.flush(UTF8Writer.java:96)
at com.ctc.wstx.sw.BufferingXmlWriter.flush(BufferingXmlWriter.java:214)
at com.ctc.wstx.sw.BufferingXmlWriter.close(BufferingXmlWriter.java:194)
at com.ctc.wstx.sw.BaseStreamWriter.finishDocument(BaseStreamWriter.java:1685)
... 10 more
gigi_ly180 2008-04-22
我之前对这个理解错误了,目前我的实践问题如下,如果实现一个返回List类型的接口,可以获得list的大小,但是list里的对象都为空;如果用楼主的listobject来封装,在执行listobject.getList()方法时,取得的为空
不知道这个应该怎么解决,谢谢!
alexma 2008-04-22
引用

或者是将bus配置到JaxWsClientFactory中去
<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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd"
	default-autowire="byName" default-lazy-init="true">
     <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-extension-http.xml"/> 
	<!-- ws client -->
	<bean id="identityValidateServiceClient" class="cn.org.coral.admin.service.IdentityValidateService"
		factory-bean="identityValidateServiceClientFactory" factory-method="create">
        <property name="bus" value="#cxf" />
        </bean> 

	<bean id="identityValidateServiceClientFactory"
		class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass"
			value="cn.org.coral.admin.service.IdentityValidateService" />
		<property name="address"
			value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/>
	</bean>
	
</beans>





多谢楼上回复,按照你的配置,问题已经解决。但是你这段配置有一个错误
<property name="bus" value="#cxf" />
这一行应该配置在 identityValidateServiceClientFactory 这个 bean 中,如果配置在 identityValidateServiceClient 这个 bean ,会报找不到 property 的错误。
另外,应该写成 <property name="bus" ref="cxf">
按照你的写法会出现 classcast 错误:不能把 String 转成 Bus 对象。


我修改了你的配置,如下:

<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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd"
	default-autowire="byName" default-lazy-init="true">
     <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-extension-http.xml"/> 
	<!-- ws client -->
	<bean id="identityValidateServiceClient" class="cn.org.coral.admin.service.IdentityValidateService"
		factory-bean="identityValidateServiceClientFactory" factory-method="create">
        </bean> 

	<bean id="identityValidateServiceClientFactory"
		class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass"
			value="cn.org.coral.admin.service.IdentityValidateService" />
		<property name="address"
			value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/>
                <property name="bus" ref="cxf" />
	</bean>
	
</beans>

jnn 2008-04-22
alexma 写道
NPE 的错误似乎是由于 bus 没有被注入。但是按照楼主的帖子,并不需要注入这个才对。另外,我把 war 部署到 Jboss 4.2,启动服务器的时候,有这样的错误:
09:36:08,607 ERROR [STDERR] 2008-4-22 9:36:08 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
。。。。
09:36:09,588 ERROR [STDERR] 2008-4-22 9:36:09 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
。。。。
09:36:10,079 ERROR [STDERR] 2008-4-22 9:36:10 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be /MoscoService

这些信息不是错误信息,这是因为CXF使用的是j.u.l 而不是Jboss使用的log4j。这样CXF的日志输出就被定向到了错误输出流里面了。 你看到这些日志应该是 INFO level的。
对于NPE,的确是楼主在配置client factory的过程中没有制定bus 而造成的。
如果不想指定bus你可以使用 来定义客户端。
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jaxws="http://cxf.apache.org/jaxws"
      xmlns:soap="http://cxf.apache.org/bindings/soap"
      xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<jaxws:client id="wsdlLocation" 
    serviceClass="org.apache.hello_world_soap_http.Greeter"
    serviceName="s:SOAPService"
    endpointName="s:SoapPort"
    xmlns:s="http://apache.org/hello_world_soap_http"
    address="http://localhost:8080/simpleWithAddress"
    wsdlLocation="wsdl/hello_world.wsdl"/>



或者是将bus配置到JaxWsClientFactory中去
<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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd"
	default-autowire="byName" default-lazy-init="true">
     <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-extension-http.xml"/> 
	<!-- ws client -->
	<bean id="identityValidateServiceClient" class="cn.org.coral.admin.service.IdentityValidateService"
		factory-bean="identityValidateServiceClientFactory" factory-method="create">
        <property name="bus" ref="cxf" />
        </bean> 

	<bean id="identityValidateServiceClientFactory"
		class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass"
			value="cn.org.coral.admin.service.IdentityValidateService" />
		<property name="address"
			value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/>
	</bean>
	
</beans>

jnn 2008-04-22
gigi_ly180 写道
我想问下,这个要在http里怎么请求,在网上找了个HELLOWORLD的例子,但是只能在java运行,没有http请求路径(http://localhost:8080/cxf/ws/helloWorld?wsdl后面如何把参数传进去)


不明白你的问题是什么,你是问在调用的client的时候如何将wsdl 的url 传给client吗?
alexma 2008-04-22
NPE 的错误似乎是由于 bus 没有被注入。但是按照楼主的帖子,并不需要注入这个才对。另外,我把 war 部署到 Jboss 4.2,启动服务器的时候,有这样的错误:

09:36:08,607 ERROR [STDERR] 2008-4-22 9:36:08 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
。。。。

09:36:09,588 ERROR [STDERR] 2008-4-22 9:36:09 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
。。。。

09:36:10,079 ERROR [STDERR] 2008-4-22 9:36:10 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be /MoscoService
gigi_ly180 2008-04-21
我想问下,这个要在http里怎么请求,在网上找了个HELLOWORLD的例子,但是只能在java运行,没有http请求路径(http://localhost:8080/cxf/ws/helloWorld?wsdl后面如何把参数传进去)
alexma 2008-04-21
我用的就是 cxf2.0.5 + spring2.5.2 ,还是有NPE
alexma 2008-04-18
我也考虑换 xfire 了,不知道 xfire 对 jax-ws 的系列标准支持的怎么样。
peihexian 2008-04-17
如果只是要开发web serivices的话用xfire就够了吧?cxf 50M的运行库吓死人。
alexma 2008-04-17
我也遇到了同样的问题。
p.s.我用的是 spring 2.5.2 + cxf 2.0.5 bundle
jacally 2008-03-27
引用

Java代码
信息: Interceptor has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Marshalling Error: class com.color.webterminal.ws.dto.UserDTO nor any of its super class is known to this context.
at org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:207)
at org.apache.cxf.jaxb.io.DataWriterImpl.write(DataWriterImpl.java:63)

信息: Interceptor has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Marshalling Error: class com.color.webterminal.ws.dto.UserDTO nor any of its super class is known to this context.
at org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:207)
at org.apache.cxf.jaxb.io.DataWriterImpl.write(DataWriterImpl.java:63)


报上面错误啊,按照楼主的做的代码,Help me


有没有用annotation标注JAVA-XML映射呀?
/**  
 * Web Service传输User信息的DTO.  
 *   
 * 分离entity类与web service接口间的耦合,隔绝entity类的修改对接口的影响.  
 * 使用JAXB 2.0的annotation标注JAVA-XML映射,尽量使用默认约定.  
 *   
 */  
@XmlAccessorType(XmlAccessType.FIELD)   
@XmlType(name = "User")   
public class UserDTO {   
  
    protected Integer id;   
  
    protected String name;   
  
    public Integer getId() {   
        return id;   
    }   
  
    public void setId(Integer value) {   
        id = value;   
    }   
  
    public String getName() {   
        return name;   
    }   
  
    public void setName(String value) {   
        name = value;   
    }   
} 
ken2211 2008-03-23
信息: Interceptor has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Marshalling Error: class com.color.webterminal.ws.dto.UserDTO nor any of its super class is known to this context.
	at org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:207)
	at org.apache.cxf.jaxb.io.DataWriterImpl.write(DataWriterImpl.java:63)


报上面错误啊,按照楼主的做的代码,Help me
jnn 2008-03-20
quaff 写道
jnn 写道
如果一定要用Spring 2.5.2 你也可以下载CXF2.0.5 snapshot[1] 试验一下。 我记得这个bug应该是修正了的。
[1] http://people.apache.org/repo/m2-snapshot-repository/org/apache/cxf/apache-cxf/2.0.5-incubator-SNAPSHOT/apache-cxf-2.0.5-incubator-20080319.024727-10.zip

用2.0.5是通过的,但是jira上面写的是2.0.4就fixed

Spring 2.5.2 是在2008年2月底发布的,而CXF 2.0.4 是在2008年1月底发布的。 JIRA上说2.0.4 fixed 的bug 应该是与Spring 2.5相关的bug。
quaff 2008-03-20
jnn 写道
如果一定要用Spring 2.5.2 你也可以下载CXF2.0.5 snapshot[1] 试验一下。 我记得这个bug应该是修正了的。
[1] http://people.apache.org/repo/m2-snapshot-repository/org/apache/cxf/apache-cxf/2.0.5-incubator-SNAPSHOT/apache-cxf-2.0.5-incubator-20080319.024727-10.zip

用2.0.5是通过的,但是jira上面写的是2.0.4就fixed
jnn 2008-03-20
如果一定要用Spring 2.5.2 你也可以下载CXF2.0.5 snapshot[1] 试验一下。 我记得这个bug应该是修正了的。
[1] http://people.apache.org/repo/m2-snapshot-repository/org/apache/cxf/apache-cxf/2.0.5-incubator-SNAPSHOT/apache-cxf-2.0.5-incubator-20080319.024727-10.zip
quaff 2008-03-20
java_code 写道
WSDL 我成功发布了.
但是客户端调用的时候,出现了下面的异常,能否帮忙看看.

调用代码:
public  String testServiceMethod() throws Exception {
	        // START SNIPPET: client
		 	System.out.println("test");
	        ClassPathXmlApplicationContext context 
	            = new ClassPathXmlApplicationContext(new String[] {"com/ems/test/client-beans.xml"});
	        
	        //org.apache.cxf.jaxws.JaxWsProxyFactoryBean clientFac = (org.apache.cxf.jaxws.JaxWsProxyFactoryBean)context.getBean("clientFactory");
	        
	        //com.gdcn.bpaf.service.IDictionaryTypeService client = (IDictionaryTypeService)clientFac.create();
	        com.gdcn.bpaf.service.IDictionaryTypeService client = (IDictionaryTypeService)context.getBean("client");

	        com.gdcn.bpaf.model.BPAFDictionaryType response = client.getDictionaryTypeByCode("test");
	        System.out.println("Response: " + response.getDictionary_name());
	        return response.getDictionary_name();
	        // END SNIPPET: client
	    }


client-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
	Licensed to the Apache Software Foundation (ASF) under one
	or more contributor license agreements. See the NOTICE file
	distributed with this work for additional information
	regarding copyright ownership. The ASF licenses this file
	to you under the Apache License, Version 2.0 (the
	"License"); you may not use this file except in compliance
	with the License. You may obtain a copy of the License at
	
	http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing,
	software distributed under the License is distributed on an
	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
	KIND, either express or implied. See the License for the
	specific language governing permissions and limitations
	under the License.
-->
<!-- START SNIPPET: beans -->
<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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
	
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
	  <property name="serviceClass" value="com.gdcn.bpaf.service.IDictionaryTypeService"/>
	  <property name="address" value="http://localhost:8080/EMSTest/DictionaryType"/>
	</bean>
	
    <bean id="client" class="com.gdcn.bpaf.service.IDictionaryTypeService" 
      factory-bean="clientFactory" factory-method="create"/>
    
	
	  
</beans>
<!-- END SNIPPET: beans -->



调用的时候异常为:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'client' defined in class path resource [com/ems/test/client-beans.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public java.lang.Object org.apache.cxf.frontend.ClientProxyFactoryBean.create()] threw exception; nested exception is java.lang.NullPointerException
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:423)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:871)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:785)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:437)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:404)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:375)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:263)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:170)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:260)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:184)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:163)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:430)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
	at com.ems.test.testUI.testServiceMethod(testUI.java:62)
	at com.ems.test.testUI.createContents(testUI.java:53)
	at com.ems.test.testUI.open(testUI.java:34)
	at com.ems.test.testUI.main(testUI.java:22)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public java.lang.Object org.apache.cxf.frontend.ClientProxyFactoryBean.create()] threw exception; nested exception is java.lang.NullPointerException
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:127)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:414)
	... 20 more
Caused by: java.lang.NullPointerException
	at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.setConfigLocations(AbstractRefreshableConfigApplicationContext.java:78)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:137)
	at org.apache.cxf.bus.spring.BusApplicationContext.<init>(BusApplicationContext.java:68)
	at org.apache.cxf.bus.spring.SpringBusFactory.createBus(SpringBusFactory.java:84)
	at org.apache.cxf.bus.spring.SpringBusFactory.createBus(SpringBusFactory.java:65)
	at org.apache.cxf.bus.spring.SpringBusFactory.createBus(SpringBusFactory.java:54)
	at org.apache.cxf.BusFactory.getDefaultBus(BusFactory.java:69)
	at org.apache.cxf.BusFactory.getThreadDefaultBus(BusFactory.java:106)
	at org.apache.cxf.BusFactory.getThreadDefaultBus(BusFactory.java:97)
	at org.apache.cxf.endpoint.AbstractEndpointFactory.getBus(AbstractEndpointFactory.java:73)
	at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.initializeServiceFactory(AbstractWSDLBasedEndpointFactory.java:143)
	at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:73)
	at org.apache.cxf.frontend.ClientFactoryBean.create(ClientFactoryBean.java:51)
	at org.apache.cxf.frontend.ClientProxyFactoryBean.create(ClientProxyFactoryBean.java:92)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:115)
	... 21 more

用的是spring-2.5.x吧,先用spring-2.0.x顶着,等cxf修复bug
jacally 2008-03-20
spring创建bean失败,不知这个 client bean 中有没有引用其它的bean?如果有需要在加载context时引入相应的配置文件.
 <bean id="client" class="com.gdcn.bpaf.service.IDictionaryTypeService"    
      factory-bean="clientFactory" factory-method="create"/> 
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

jacally
搜索本博客
我的相册
5225c084-f830-3bfb-89b4-e391c07b2221-thumb
flexlib6-1.jpg
共 27 张
最近加入圈子
存档
最新评论