NutzCN Logo
精华 nutz集成cxf
发布于 3100天前 作者 ecoolper 4724 次浏览 复制 上一个帖子 下一个帖子
标签: 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();
			}
		}

	}
}

35 回复

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

按这个配置报错了,请问可能是什么原因造成的。。

Exception in thread "main" javax.xml.ws.WebServiceException: Could not send Message.
	at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:150)
	at com.sun.proxy.$Proxy35.hi(Unknown Source)
	at cn.wizzer.common.cxf.CXFClient.runCxfClient(CXFClient.java:17)
	at cn.wizzer.common.cxf.CXFClient.main(CXFClient.java:8)
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '404: null' when communicating with http://127.0.0.1:8080/ws/test
	at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.doProcessResponseCode(HTTPConduit.java:1608)
	at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1615)
	at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1559)
	at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1356)
	at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
	at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:653)
	at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
	at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
	at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:514)
	at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:423)
	at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:324)
	at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:277)
	at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
	at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:139)
	... 3 more
2017-08-01 17:57:17,204 org.apache.cxf.common.logging.LogUtils.doLog(LogUtils.java:478) WARN  - Interceptor for {http://cxf.common.wizzer.cn/}TestServiceService#{http://cxf.common.wizzer.cn/}hi has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Could not send Message.
	at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
	at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
	at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:514)
	at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:423)
	at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:324)
	at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:277)
	at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
	at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:139)
	at com.sun.proxy.$Proxy35.hi(Unknown Source)
	at cn.wizzer.common.cxf.CXFClient.runCxfClient(CXFClient.java:17)
	at cn.wizzer.common.cxf.CXFClient.main(CXFClient.java:8)
Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '404: null' when communicating with http://127.0.0.1:8080/ws/test
	at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.doProcessResponseCode(HTTPConduit.java:1608)
	at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1615)
	at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1559)
	at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1356)
	at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
	at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:653)
	at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
	... 10 more
http://127.0.0.1:8080/ws/test

404了?

这个是客户端 factoryBean.setAddress() 的地址啊

有空我也做个demo看看

发现原因是servlet根本没有被执行

用更简单更好的方式集成, 挂载为一个入口方法

https://github.com/wendal/nutz-book-project/tree/v3.x/nutzcn-cxfdemo

大佬 我按照 你 demo里面的 例子
配置后 404 找不到 对应的 wsdl

是否需要在 其他地方添加呢??

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>singleSignOn</display-name>
  <filter>
    <filter-name>nutz</filter-name>
    <filter-class>com.gwxa.framework.servlet.FilterUtil</filter-class>
    <init-param>
      <param-name>modules</param-name>
      <param-value>com.gwxa.framework.modules.MainModule</param-value>
    </init-param>
    <init-param>
    	<param-name>exclusions</param-name>
    	<param-value>/cxf/*</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>nutz</filter-name>
    <url-pattern>*.do</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.json</url-pattern>
    <url-pattern>*.xml</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>/WEB-INF/index.jsp</welcome-file>
  </welcome-file-list>
  <error-page>
    <error-code>500</error-code>
    <location>/source/500.html</location>
  </error-page>
  <error-page>
    <error-code>404</error-code>
    <location>/source/404.html</location>
  </error-page>
</web-app>
package com.gwxa.sso.cxf;

import org.nutz.integration.cxf.AbstractCxfModule;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;

@IocBean(create ="_init",depose = "depose")
@At("/cxf/")
public class CxfModule extends AbstractCxfModule{
	
	public CxfModule(){
		pathROOT ="/cxf/";
		pkg = "com.gwxa.sso.cxf.webservice";
	}
	
	@At("/*")
	@Ok("void")
	@Fail("void")
	public void service() throws Exception{
		super.service();
	}
}

package com.gwxa.framework.modules;

import org.nutz.mvc.annotation.Encoding;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.IocBy;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.SetupBy;
import org.nutz.mvc.annotation.Views;
import org.nutz.mvc.ioc.provider.ComboIocProvider;
import org.nutz.mvc.view.DefaultViewMaker;

import com.gwxa.framework.setUp.NutzSetup;

@Modules(scanPackage = true,packages={"com.gwxa.login","com.gwxa.sso","com.gwxa.oauth2"})
@IocBy(type = ComboIocProvider.class, 
	args = { "*org.nutz.ioc.loader.json.JsonLoader", "ioc/", 
		"*org.nutz.ioc.loader.annotation.AnnotationIocLoader","jedis",
		"com.gwxa" ,
        "*tx", // 事务拦截 aop
        "*async"}) // 异步执行aop
@SetupBy(NutzSetup.class)
@Fail("http:500")
@Encoding(input = "UTF-8", output = "UTF-8")
@Views(DefaultViewMaker.class)
public class MainModule {}

补充下

package com.gwxa.sso.cxf.webservice;

import javax.jws.WebService;

/**
 * 定义公共接口
 * @author rockw
 *
 */
@WebService
public interface SsoWebService {
	
	String testRe();
	
	                             
}

package com.gwxa.sso.cxf.webservice;

import javax.jws.WebService;

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

@WebService(endpointInterface="com.gwxa.sso.cxf.webservice.SsoWebService",serviceName="SsoWebService")
@IocBean(name = "ssoWebService")
public class SsoWebServiceImpl implements SsoWebService{

	@Override
	public String testRe() {
		return "123123";
	}

}

去掉这个参数

    <init-param>
    	<param-name>exclusions</param-name>
    	<param-value>/cxf/*</param-value>
    </init-param>

我重写过 filter

应该没影响吧??

还是不行..

我看到 log 已经扫描到 这个类的

LOG4J DEV:[localhost-startStop-1] 255 INFO 2018-02-01 15:17:07,972 org.nutz.resource.Scans Resource can't map to Class, Resource framework/web/XStreamXmlView.class
LOG4J DEV:[localhost-startStop-1] 306 INFO 2018-02-01 15:17:08,023 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'baseServiceImpl                         ' - com.gwxa.framework.base.BaseServiceImpl
LOG4J DEV:[localhost-startStop-1] 319 INFO 2018-02-01 15:17:08,036 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'accountController                       ' - com.gwxa.login.controller.AccountController
LOG4J DEV:[localhost-startStop-1] 322 INFO 2018-02-01 15:17:08,039 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'dashbordController                      ' - com.gwxa.login.controller.DashbordController
LOG4J DEV:[localhost-startStop-1] 325 INFO 2018-02-01 15:17:08,042 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'accountServiceImpl                      ' - com.gwxa.login.service.impl.AccountServiceImpl
LOG4J DEV:[localhost-startStop-1] 326 INFO 2018-02-01 15:17:08,043 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'privServiceImpl                         ' - com.gwxa.login.service.impl.PrivServiceImpl
LOG4J DEV:[localhost-startStop-1] 327 INFO 2018-02-01 15:17:08,044 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'accessTokenController                   ' - com.gwxa.oauth2.controller.AccessTokenController
LOG4J DEV:[localhost-startStop-1] 329 INFO 2018-02-01 15:17:08,046 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'authorizeController                     ' - com.gwxa.oauth2.controller.AuthorizeController
LOG4J DEV:[localhost-startStop-1] 330 INFO 2018-02-01 15:17:08,047 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'oAuthServiceImpl                        ' - com.gwxa.oauth2.service.impl.OAuthServiceImpl
LOG4J DEV:[localhost-startStop-1] 330 INFO 2018-02-01 15:17:08,047 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'cacheUtil                               ' - com.gwxa.oauth2.util.CacheUtil
LOG4J DEV:[localhost-startStop-1] 336 INFO 2018-02-01 15:17:08,053 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'jedisUtil                               ' - com.gwxa.oauth2.util.JedisUtil
LOG4J DEV:[localhost-startStop-1] 337 INFO 2018-02-01 15:17:08,054 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'logAdvice                               ' - com.gwxa.sso.advice.LogAdvice
LOG4J DEV:[localhost-startStop-1] 338 INFO 2018-02-01 15:17:08,055 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'caozrzController                        ' - com.gwxa.sso.caozrz.controller.CaozrzController
LOG4J DEV:[localhost-startStop-1] 339 INFO 2018-02-01 15:17:08,056 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'caozrzServiceImpl                       ' - com.gwxa.sso.caozrz.service.impl.CaozrzServiceImpl
LOG4J DEV:[localhost-startStop-1] 339 INFO 2018-02-01 15:17:08,056 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'cxfModule                               ' - com.gwxa.sso.cxf.CxfModule
LOG4J DEV:[localhost-startStop-1] 350 INFO 2018-02-01 15:17:08,067 org.nutz.ioc.loader.annotation.AnnotationIocLoader    > add 'ssoWebService                           ' - com.gwxa.sso.cxf.webservice.SsoWebServiceImpl

是看映射日志里面有没有/cxf/*

有的...

LOG4J DEV:[localhost-startStop-1] 780 DEBUG 2018-02-01 15:20:37,717 org.nutz.mvc.impl.UrlMappingImpl    '/cxf//*'                   >> (CxfModule.java:24).service           : void       | @Ok(void ) @Fail(void ) | by 0 Filters | (I:UTF-8/O:UTF-8)
LOG4J DEV:[localhost-startStop-1] 784 DEBUG 2018-02-01 15:20:37,721 org.nutz.mvc.impl.UrlMappingImpl    '/sysOrgan//index'          >> (SysOrganController.java:83).index    : void       | @Ok(jsp:views.sso.sysOrgan.index) @Fail(http:500) | by 2 Filters | (I:UTF-8/O:UTF-8)
LOG4J DEV:[localhost-startStop-1] 785 DEBUG 2018-02-01 15:20:37,722 org.nutz.mvc.impl.UrlMappingImpl    '/sysOrgan//add'            >> (SysOrganController.java:87).add      : void       | @Ok(jsp:views.sso.sysOrgan.add) @Fail(http:500) | by 2 Filters | (I:UTF-8/O:UTF-8)
@At("/cxf/") 改成 @At("/cxf")

然后访问一下, 看看后台有什么日志

修改后 仍然 是 404 错误....

日志, 后台的日志, 有没有日志出来

没有 日志 ... 直接就是 404

那我先想想 如何出日志

你访问的地址是什么?? 忘记加conetext path?

项目访问路径 + /cxf/SsoWebService?wsdl

是不是shiro之类的拦截了?debug一下你的Filter类吧

原因 找到了.. 蛋疼...

<filter-mapping>
    <filter-name>nutz</filter-name>
    <url-pattern>*.do</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.json</url-pattern>
    <url-pattern>*.xml</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
  </filter-mapping>

集成 需要 哪些jar呢???
有个报错 Caused by:java.lang.NoClassDefFoundError:/org/apcha/cxf/binding/soap/SoapBindingConfiguration
当前已经用了:

org.nutz
nutz-integration-cxf
1.r.63-SNAPSHOT


org.apache.cxf
cxf-core
3.1.12


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


org.apache.cxf
cxf-rt-frontend-simple
3.1.12


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

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