使用java创建webService可以使用axis 也可以使用 cxf,axis需要打包成 arr cxf可以直接发布
首先引用相应的jar包 以及 spring相关的jar包
编写相应的接口类
实现类
在 spring 配置文件中添加:
在web.xml中添加配置
至此服务端配置完成
首先引用相应的jar包 以及 spring相关的jar包
在spring 配置文件的命名空间中引入cxf相关的包路径
xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
编写相应的接口类
@WebService public interface GreetingService { public String greeting(String userName); }
实现类
import java.util.Calendar; import javax.jws.WebService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClasspathXmlApplicationContext; import com.company.cxf.service.GreetingService; import com.company.cxf.dao.*; @WebService(endpointInterface = "com.company.cxf.service.GreetingService") public class GreetingServiceImpl implements GreetingService { public String greeting(String userName){ //servletConfig.getServletContext().getRealPath("/"); ApplicationContext context = new ClasspathXmlApplicationContext("applicationContext.xml"); UserMapper us=(UserMapper)context.getBean("UserMapper"); return "Hello " + us.getUserName(userName) + ",currentTime is " + Calendar.getInstance().getTime(); } }
在 spring 配置文件中添加:
<jaxws:endpoint id="greetingService" implementor="com.company.cxf.service.impl.GreetingServiceImpl" address="/GreetingService" />
在web.xml中添加配置
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>/*</url-pattern> </servlet-mapping>
至此服务端配置完成
调用类
package com.company.cxf.client; import org.apache.cxf.jaxws.JaxWsProxyfactorybean; import com.company.cxf.service.GreetingService; public class TestGreetingService { public static void main(String[] args) { // 创建WebService客户端代理工厂 JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean(); // 注册WebService接口 factory.setServiceClass(GreetingService.class); // 设置WebService地址 factory.setAddress("http://localhost:8080/cxf_webservice/GreetingService"); GreetingService greetingService = (GreetingService) factory.create(); System.out.println("invoke webservice..."); System.out.println("message context is:" + greetingService.greeting("SEX")); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。