NutzCN Logo
精华 nutz集成cxf
发布于 241天前 作者 ecoolper 300 次浏览 复制 上一个帖子 下一个帖子
标签: cxf webservice

CXFServlet

package com.my.servlet;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import org.nutz.ioc.Ioc;
import org.nutz.lang.Strings;
import org.nutz.mvc.Mvcs;

import javax.jws.WebService;
import javax.servlet.ServletConfig;

public class CXFServlet extends CXFNonSpringServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3741408930953142845L;

	@Override
	protected void loadBus(ServletConfig sc) {
		super.loadBus(sc);
		Bus b = getBus();
		BusFactory.setDefaultBus(b);

		Ioc ioc = Mvcs.ctx().getDefaultIoc();
		for (String name : ioc.getNames()) {
			Object obj = ioc.get(null, name);
			if (!obj.getClass().getPackage().getName().equals("com.my.webService.impl")) {
				continue;
			}
			if (obj.getClass().getAnnotation(WebService.class) == null)
				continue;

			try {
				Class<?> face = Class.forName(obj.getClass().getPackage().getName() + "." + Strings.upperFirst(name));
				// 接口类
				ServerFactoryBean sfb = new ServerFactoryBean();
				// 设置服务接口类
				sfb.setServiceClass(face);
				// 服务请求路径
				sfb.setAddress("/" +name.substring(0, name.indexOf("Service")));
				// 设置服务实现类
				sfb.setServiceBean(obj);
				sfb.create();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}

	}
}

9 回复

HelloWorldService

package com.my.webService;

import javax.jws.WebService;

@WebService
public interface HelloWorldService {
	String sayHi(String text);
}

HelloWorldServiceImpl

package com.my.webService.impl;

import javax.jws.WebService;

import org.nutz.ioc.loader.annotation.IocBean;

import com.jsptpd.webService.HelloWorldService;

@IocBean
@WebService(endpointInterface = "com.my.webService.HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {

	@Override
	public String sayHi(String text) {
		return "Hello " + text;
	}

}

web.xml

	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>com.my.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/ws/*</url-pattern>
	</servlet-mapping>

请问客户端怎么写? 怎么调用HelloWorldServiceImpl 中的sayHi方法,怎么传值?谢谢!

@qq_9cc84e4e 找个webservice客户端

```
public static void main(String[] args) {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(HelloWorldService.class);
factoryBean.setAddress("http://localhost:8080/ws/helloWorld");

    HelloWorldService readerService = (HelloWorldService)factoryBean.create();  
    String result = readerService.sayHi("hi!");
    System.out.println("result:" + result);  
}
```
客户端代码如上, 
服务端的sayHi方法,text参数接收到的值是NULL
报错如下:
org.apache.cxf.interceptor.Fault: Unmarshalling Error: 意外的元素 (uri:"http://server.service.cxf.support.app.com/", local:"return")。所需元素为<{}return> 
at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:907)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:711)
at org.apache.cxf.jaxb.io.DataReaderImpl.read(DataReaderImpl.java:172)
at org.apache.cxf.interceptor.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:107)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:849)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1642)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1520)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1317)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:632)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:572)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:481)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:382)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:335)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:136)
at com.sun.proxy.$Proxy37.sayHi(Unknown Source)
at com.wongtian.support.cxf.service.client.Test.main(Test.java:15)

Caused by: javax.xml.bind.UnmarshalException: null
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:435)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:372)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:349)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.doUnmarshal(JAXBEncoderDecoder.java:857)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.access$100(JAXBEncoderDecoder.java:101)
at org.apache.cxf.jaxb.JAXBEncoderDecoder$2.run(JAXBEncoderDecoder.java:896)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:894)
... 20 common frames omitted

看上去cxf生成的xml不对? 版本不匹配?

版本不匹配,指的是CXF的版本么?

org.apache.cxf
cxf-api
2.7.15


org.apache.cxf
cxf-rt-frontend-jaxws
2.7.15


org.apache.cxf
cxf-rt-bindings-soap
2.7.15


org.apache.cxf
cxf-rt-transports-http
2.7.15


org.apache.cxf
cxf-rt-ws-security
2.7.15

添加回复
请先登陆
回到顶部