cxf创建WebService
发布时间:2020-12-16 23:02:27  所属栏目:安全  来源:网络整理 
            导读:使用java创建webService可以使用axis ?也可以使用 cxf,axis需要打包成 arr cxf可以直接发布 首先引用相应的jar包 ?以及 spring相关的jar包 在spring 配置文件的命名空间中引入cxf相关的包路径 xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation=
                
                
                
            | 
                         
           使用java创建webService可以使用axis ?也可以使用 cxf,axis需要打包成 arr cxf可以直接发布 
 
            
        	首先引用相应的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"));
	}   
}
  ?
        (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
相关内容
- Scala – 中缀和点符号
 - List(1,2,3)如何在Scala中运行?
 - angularjs – AngularAMD ui-router动态控制器名称?
 - tcpdump提取源IP
 - 解决linux下openoffice word文件转PDF中文乱码的问题
 - twitter bootstrap自动完成下拉/组合框与Knockoutjs
 - 管理员表的设计;webservice用于内网提供服务端安全性较高
 - 調用webservice時出現HTTP狀態417:Expectation Failed的解決
 - Vim:缩进一个空格(不是shiftwidth空格)
 - MVC5 + EF6 + Bootstrap3 (8) HtmlHelper用法大全(上)
 
