加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读