webservice-Apache CXF环境搭建及测试
发布时间:2020-12-17 00:14:41 所属栏目:安全 来源:网络整理
导读:1.去官方下载对应的jar包:http://cxf.apache.org/ 2.将lib目录下的jar包放置在项目的lib目录下,并构建路径 3.配置web.xml文件,添加spring和cxf的配置 listener listener-classorg.springframework.web.context.ContextLoaderListener/listener-class/list
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")); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |