微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

webservice-Apache CXF环境搭建及测试

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"));
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐