webservice-Apache CXF环境搭建及测试

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/guoquanyou/article/details/9287581
1.去官方下载对应的jar包:http://cxf.apache.org/
2.将lib目录下的jar包放置在项目的lib目录下,并构建路径
3.配置web.xml文件,添加spring和cxf的配置
	<listener>
	    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 设置Spring容器加载配置文件路径 -->
	<context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>classpath*:applicationContext*.xml</param-value>
	</context-param>

  	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/webservice/*</url-pattern>
	</servlet-mapping>
4.创建服务接口
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface UserService {
	//加入WebParam注解,以保证xml文件中参数名字的正确性 
	//如果没有加注解,参数将被命名为arg0
	public String checkUser(@WebParam(name = "userName")String userName,
			@WebParam(name = "userPwd")String userPwd);
}
5.完成实现类
import javax.jws.WebParam;
import javax.jws.WebService;
import com.dh.webservice.UserService;

//@WebService注解让CXF知道我们希望使用哪个接口来创建WSDL,,本例中就是UserService接口。
@WebService(endpointInterface = "com.dh.webservice.UserService", serviceName = "UserService")
public class UserServiceImpl implements UserService {

	@Override
	public String checkUser(@WebParam(name = "userName")String userName,
			@WebParam(name = "userPwd")String userPwd) {
		if("abc".equals(userName)&&"123".equals(userPwd)){
			return "登陆成功";
		}
		return "失败";
	}
}
6.添加spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	 http://www.springframework.org/schema/aop 
	 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	 http://www.springframework.org/schema/tx 
	 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
	 http://cxf.apache.org/jaxws 
	 http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- web service配置部分开始 begin -->
	<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" />
	
	<bean id="refUserService" class="com.dh.webservice.impl.UserServiceImpl">
	</bean>
	<jaxws:endpoint id="userService" address="/UserService">
		<jaxws:implementor ref="refUserService" />
	</jaxws:endpoint>
</beans>
7.发布,并运行服务器进行测试http://localhost:8080/MyWebService/webservice/UserService?wsdl

8.通过Main()方法进行测试
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.dh.webservice.UserService;
public class ServiceTest {

	public static void main(String[] args) throws Exception {
		  JaxWsProxyFactoryBean webService = new JaxWsProxyFactoryBean();
		  webService.setServiceClass(UserService.class);
		  webService.setAddress("http://localhost:8080/MyWebService/webservice/UserService");
          UserService userService = (UserService) webService.create();
          System.out.println(userService.checkUser("abc", "abc"));
          //System.out.println(userService.checkUser("abc", "123"));
	}
}
阅读更多
换一批

CXF测试

09-26

测试代码: rnrnJaxWsServerFactoryBean factory=new JaxWsServerFactoryBean();rn rn factory.setAddress("http://localhost:8080/helloService");rn factory.setServiceClass(HelloServiceImpl.class);rn factory.getInInterceptors().add(new LoggingInInterceptor());rn factory.getOutInterceptors().add(new LoggingOutInterceptor());rn Server server=factory.create();rn server.start();rnrnrn出错rn警告: method annotated by @PostConstruct throws exception when invokedrnjava.lang.reflect.InvocationTargetExceptionrn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn at java.lang.reflect.Method.invoke(Unknown Source)rn at org.apache.cxf.common.injection.ResourceInjector.invokePostConstruct(ResourceInjector.java:302)rn at org.apache.cxf.common.injection.ResourceInjector.construct(ResourceInjector.java:86)rn at org.apache.cxf.bus.spring.Jsr250BeanPostProcessor.postProcessAfterInitialization(Jsr250BeanPostProcessor.java:71)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:404)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1407)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)rn at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)rn at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)rn at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)rn at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)rn at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)rn at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)rn at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)rn at org.apache.cxf.bus.spring.BusApplicationContext.(BusApplicationContext.java:91)rn at org.apache.cxf.bus.spring.SpringBusFactory.createApplicationContext(SpringBusFactory.java:102)rn at org.apache.cxf.bus.spring.SpringBusFactory.createBus(SpringBusFactory.java:93)rn at org.apache.cxf.bus.spring.SpringBusFactory.createBus(SpringBusFactory.java:86)rn at org.apache.cxf.bus.spring.SpringBusFactory.createBus(SpringBusFactory.java:64)rn at org.apache.cxf.bus.spring.SpringBusFactory.createBus(SpringBusFactory.java:53)rn at org.apache.cxf.BusFactory.getDefaultBus(BusFactory.java:69)rn at org.apache.cxf.BusFactory.getThreadDefaultBus(BusFactory.java:106)rn at org.apache.cxf.BusFactory.getThreadDefaultBus(BusFactory.java:97)rn at org.apache.cxf.endpoint.AbstractEndpointFactory.getBus(AbstractEndpointFactory.java:73)rn at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.initializeServiceFactory(AbstractWSDLBasedEndpointFactory.java:228)rn at org.apache.cxf.frontend.ServerFactoryBean.initializeServiceFactory(ServerFactoryBean.java:157)rn at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:99)rn at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:117)rn at org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:168)rn at gwp.test.service.SoapServer.main(SoapServer.java:22)rnCaused by: java.lang.NoSuchMethodError: org.apache.cxf.ws.policy.AssertionBuilderRegistry.register(Lorg/apache/cxf/ws/policy/AssertionBuilder;)Vrn at org.apache.cxf.ws.security.policy.WSSecurityPolicyLoader.registerBuilders(WSSecurityPolicyLoader.java:91)rn at org.apache.cxf.ws.security.policy.WSSecurityPolicyLoader.register(WSSecurityPolicyLoader.java:75)rn ... 34 morernException in thread "main" java.lang.NoClassDefFoundError: javax/servlet/ServletOutputStreamrn at org.apache.cxf.transport.http_jetty.JettyHTTPTransportFactory.createDestination(JettyHTTPTransportFactory.java:116)rn at org.apache.cxf.transport.http_jetty.JettyHTTPTransportFactory.getDestination(JettyHTTPTransportFactory.java:103)rn at org.apache.cxf.endpoint.ServerImpl.initDestination(ServerImpl.java:90)rn at org.apache.cxf.endpoint.ServerImpl.(ServerImpl.java:69)rn at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:118)rn at org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:168)rn at gwp.test.service.SoapServer.main(SoapServer.java:22)rn

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