cxf实现webservice服务端

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/coderjoy/article/details/52598980

开发环境: spring-3.2.12.RELEASE + apache-cxf-3.0.9 + jdk6.0

服务端接口

package com.ws.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface IWSCall {

	@WebMethod
	public String call(String param);
}

服务端实现

package com.ws.service;
public class WSCallImpl implements IWSCall{
    @Override
    public String call(String param) {
        // TODO Auto-generated method stub
        return param + " hello!";
    }
}

服务端密码回调

package com.ws.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.wss4j.common.ext.WSPasswordCallback;

public class ServerPwdCallback implements CallbackHandler
{
    private Map<String,String> manager = new HashMap<String,String>();

    public ServerPwdCallback()
    {
        manager.put("admin", "123");
        manager.put("test", "test");
    }
    
    public void handle(Callback[] callbacks)
        throws IOException,    UnsupportedCallbackException
    {
        for(int i=0;i<callbacks.length;i++)
        {
            WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
            String identifier = pc.getIdentifier();
            int usage = pc.getUsage();
            if (usage == WSPasswordCallback.USERNAME_TOKEN)
            {
                if(!manager.containsKey(identifier))
                    throw new SecurityException("Username not exist");
                pc.setPassword(manager.get(identifier));                
            }
        }        
    }
}

server.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">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    
    <jaxws:endpoint 
	    id="wSCall" 
	    implementor="com.ws.service.WSCallImpl" 
	    address="/WSCall">
	    <jaxws:inInterceptors>
			<bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
    			<constructor-arg><map>
    					<entry key="action" value="UsernameToken"/>
    					<entry key="passwordType" value="PasswordText"/>
    					<entry key="passwordCallbackClass" value="com.ws.service.ServerPwdCallback"/>
    			</map></constructor-arg>
    		</bean>
    	</jaxws:inInterceptors>
    </jaxws:endpoint>

</beans>

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>cxf_websvc</display-name>
 
	<!-- 上下文参数配置 -->	
	<context-param>
	     <param-name>contextConfigLocation</param-name>
	     <param-value>/WEB-INF/config/applicationContext.xml</param-value>
	</context-param>
	
	<!-- 监听器配置 -->	
	<listener>    
	    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- WS请求配置 -->
  	<servlet>
		<servlet-name>webservice</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>webservice</servlet-name>
		<url-pattern>/service/*</url-pattern>
	</servlet-mapping>  
	
</web-app> 

依赖jar包





展开阅读全文

CXF webservice服务端查询数据库出错

07-23

准备用webservice做一个单点登录系统,对外发布了一个webservice,服务端与客户端的通讯没有问题,但是服务端实现类访问数据库的时候会出现invocation target exception,就像spring没有注入一样。rnrn接口如下:rn[code=java]rnpackage com.x.member0.service;rnimport javax.jws.WebService;rnimport com.yuanhua.member0.entity.User;rn@WebServicernpublic interface IUserService rn /**rn * 验证用户信息 getAuthenticationrn * rn * rn *@param itemrn * rn */rn public User getAuthentication(User item);rn[/code]rnrnrn服务端实现类如下:rn[code=java]rnpackage com.x.member0.service.impl;rnrnimport java.util.List;rnrnimport javax.jws.WebService;rnrnimport com.x.common.dao.ICommonDao;rnimport com.x.common.dao.sqlimpl.CommonDaoSqlImpl;rnimport com.x.common.service.BaseService;rnimport com.x.member0.entity.User;rnimport com.x.member0.service.IUserService;rnrn/**rn * 用户信息dao UserDaorn * rn * rn * rn */rn@WebService(endpointInterface = "com.x.member0.service.IUserService")rnpublic class UserServiceImpl extends BaseService implements IUserService rn rnrn /*rn * (non-Javadoc)rn * @seern * com.yuanhua.member1.service.impl.IUserDao#getAuthentication(com.yuanhuarn * .member0.entity.User)rn */rn public User getAuthentication(User item) rn String hql = "from User where loginName='" + item.getLoginName()rn + "' and userPwd='" + item.getUserPwd() + "' and isDeleted=0";rn rn User u = (User) this.getCommonDao().obj(hql);//无法访问dao层rn return item;rn rn[/code]rnrn客户端:rn[code=java]rnpackage aa;rnrnimport org.springframework.context.ApplicationContext;rnimport org.springframework.context.support.FileSystemXmlApplicationContext;rnrnimport com.x.member0.entity.User;rnimport com.x.member0.service.IUserService;rnrnpublic class TestClient rnrn public static void main(String[] args) rn testWebserviceClient();rn rnrn public static void testWebserviceClient() rn // 加载客户端配置文件rn ApplicationContext context = new FileSystemXmlApplicationContext(rn "src/applicationContext.xml");rn // 获取webservice接口rn IUserService service = (IUserService) contextrn .getBean("yhProtalWebservice");rn User item = new User();rn item.setLoginName("admin");rn item.setUserPwd("admin");rn User u = service.getAuthentication(item);rn System.out.println(u);rn[/code]rnrn运行后,客户端的值可以很顺利的传到服务端,无论传字符串、对象、集合都可以。rn服务端实现类里面写死一个值,也可以顺利的传回客户端,webservice通讯应该是没有问题。rn但是一旦到了访问数据库的,就是调用dao层的方法,就会出现invocation target exception,就像spring没有注入一样。但是spring中我肯定注入过这个bean。rnrn控制台报错如下rn服务端rn[code=text]rn[yuanhua] WARN [http-8080-1] org.apache.cxf.common.logging.LogUtils.doLog(371) | Application http://impl.service.member0.yuanhua.com/UserServiceImplService#http://service.member0.yuanhua.com/getAuthentication has thrown exception, unwinding nowrnorg.apache.cxf.interceptor.Faultrn at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:155)rn at org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.createFault(AbstractJAXWSMethodInvoker.java:86)rn at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:121)rn at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:61)rn at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:75)rn at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)rn at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)rn at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)rn at java.util.concurrent.FutureTask.run(FutureTask.java:138)rn at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)rn at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:106)rn at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)rn at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)rn at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:206)rn at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:218)rn at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:200)rn at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:114)rn at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:184)rn at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:107)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)rn at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:163)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)rn at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)rn at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)rn at java.lang.Thread.run(Thread.java:662)rnCaused by: java.lang.NullPointerExceptionrn at com.yuanhua.member0.service.impl.UserServiceImpl.getAuthentication(UserServiceImpl.java:33)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:597)rn at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:173)rn at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:89)rn ... 38 morernrn[/code]rn客户端:rn[code=text]rn信息: Creating Service http://service.member0.yuanhua.com/IUserServiceService from class com.yuanhua.member0.service.IUserServicernException in thread "main" javax.xml.ws.soap.SOAPFaultException: Fault occurred while processing.rn at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:156)rn at $Proxy26.getAuthentication(Unknown Source)rn at aa.TestClient.testWebserviceClient(TestClient.java:25)rn at aa.TestClient.main(TestClient.java:12)rnCaused by: org.apache.cxf.binding.soap.SoapFault: Fault occurred while processing.rn at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.unmarshalFault(Soap11FaultInInterceptor.java:75)rn at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:46)rn at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:35)rn at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)rn at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:105)rn at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:69)rn at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:34)rn at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)rn at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:797)rn at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1618)rn at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1491)rn at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1399)rn at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)rn at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:646)rn at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)rn at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)rn at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:533)rn at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:463)rn at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:366)rn at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:319)rn at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:88)rn at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)rn ... 3 morern[/code]rn请各位给看一看了,谢谢 论坛

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