java用cxf开发webService接口

前提:jre和jdk为1.8开发环境;

第一步:准备maven依赖

	<!-- apache cxf开发webService依赖 -->
	<dependency>  
      	<groupId>org.apache.cxf</groupId>  
      	<artifactId>cxf-rt-frontend-jaxws</artifactId>  
       	<version>3.1.3</version>  
	</dependency>  
	<dependency>
		<groupId>org.apache.cxf</groupId>  
		<artifactId>cxf-rt-transports-http</artifactId>  
		<version>3.1.3</version>
	</dependency>

第二步:准备接口DealService.java;

package com.szy.project.webservice;

import java.util.Map;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface DealService {

	/**执行操作*/
	String executeOperation(@WebParam(name="param")Map<String,String> param);
	
}

准备实现类:DealServiceImpl.java;

package com.szy.project.webservice;

import java.util.Map;

import javax.jws.WebService;

@WebService(endpointInterface = "com.szy.project.webservice.DealService",serviceName = "executeOperation")
public class DealServiceImpl implements DealService{

	public String executeOperation(Map<String, String> param) {
		int operationType = Integer.parseInt(param.get("operationType"));
		if(operationType==1){
			return sayHello(param);
		}
		return null;
	}
	
	public String sayHello(Map<String,String>param){
		return "Hello "+param.get("name");
	}

}

第三步:在web。xml中配置:

	<servlet>
		<servlet-name>cxf</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>cxf</servlet-name>
		<url-pattern>/webService/*</url-pattern>
	</servlet-mapping>

第四步:配置application;

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xmlns:jaxws="http://cxf.apache.org/jaxws"  
 xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
    <import resource="classpath:META-INF/cxf/cxf.xml"/>  
    <jaxws:endpoint id="helloWorld" implementor="com.sky.springmvc.webservice.HelloWorldImpl" address="/helloWorld"/>  
</beans>

第五步:部署项目到tomcat中,启动服务:

第六步:客户端调用;

在xml中做配置:

<jaxws:client id="executeClient"  
                  serviceClass="com.szy.project.webservice.DealService"  
                  address="http://localhost:9090/Project/webService/executeOperation" />

第七步:实例dome;

package com.szy.project.test.webservicetest;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.szy.project.webservice.DealService;

public class TestCxfWebservice {

	@Test
	public void test1(){
		ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");  
		DealService dealService = (DealService)app.getBean("executeClient");  
		Map<String, String> param = new HashMap<String,String>();
		param.put("operationType", "1");
		param.put("name", "sunzy");
		String callBack = dealService.executeOperation(param);
		System.out.println(callBack);
	}
	
}

第八步:返回信息:

log4j:WARN Continuable parsing error 20 and column 13
log4j:WARN 元素类型为 "appender" 的内容必须匹配 "(errorHandler?,param*,rollingPolicy?,triggeringPolicy?,connectionSource?,layout?,filter*,appender-ref*)"。
log4j:WARN Unrecognized element logger
2018-06-04 15:51:24  [INFO]  org.springframework.context.support.ClassPathXmlApplicationContext {510}:【Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@35bbe5e8: startup date [Mon Jun 04 15:51:24 CST 2018]; root of context hierarchy】
2018-06-04 15:51:24  [INFO]  org.springframework.beans.factory.xml.XmlBeanDefinitionReader {317}:【Loading XML bean definitions from class path resource [applicationContext.xml]】
2018-06-04 15:51:24  [INFO]  org.springframework.beans.factory.xml.XmlBeanDefinitionReader {317}:【Loading XML bean definitions from class path resource [META-INF/cxf/cxf.xml]】
六月 04, 2018 3:51:25 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webservice.project.szy.com/}executeOperation from class com.szy.project.webservice.DealService
六月 04, 2018 3:51:25 下午 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be /executeOperation
六月 04, 2018 3:51:25 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webservice.project.szy.com/}DealServiceService from class com.szy.project.webservice.DealService
Hello sunzy

第九步:通过wsimport 命令获得客户端进行调用;

第十步:通过apache cxf api调用DealService:

@Test
	public void test2(){
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.getInInterceptors().add(new LoggingInInterceptor());
		factory.getOutInterceptors().add(new LoggingInInterceptor());
		factory.setServiceClass(DealService.class);
		factory.setAddress("http://localhost:9090/Project/webService/executeOperation");
		DealService dealService = (DealService)factory.create();
		Map<String, String> param = new HashMap<String,String>();
		param.put("operationType", "1");
		param.put("name", "sunzy");
		String callBack = dealService.executeOperation(param);
		System.out.println(callBack);
	}

返回信息:

ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[245], content-type=[text/xml;charset=UTF-8], Date=[Mon, 04 Jun 2018 08:34:16 GMT]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:executeOperationResponse xmlns:ns2="http://webservice.project.szy.com/"><return>Hello sunzy</return></ns2:executeOperationResponse></soap:Body></soap:Envelope>
--------------------------------------
Hello sunzy

注意:客户端用配置文件或apache cxf api调用时需要添加maven依赖和服务端接口类;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值