您的位置:首页 > 运维架构 > Apache

Apache CXF实战之一 Hello World Web Service

2015-09-19 15:06 816 查看
1.使用CXF创建服务的基本方法(使用CXF自带的jetty容器

分4步:

① 设置build环境

② 写服务

③ 发布服务

④ 访问服务

1)设置build环境

创建一个新web项目,将apache-cxf-2.2.10.zip中lib目录中的下列文件添加到Build Path:其中与spring集成的包如果不用spring可以先不用导入。

2)写服务

1.创建HelloWorld 接口类

这里的接口需要使用Annotation @WebService 描述,接口中的方法如果有参数,需要使用@WebParam来修饰,并使用属性name来定义名字,不然发布后,参数会使用默 认的args0

package com.jing.cxf.helloworld;

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebResult;

import javax.jws.WebService;

@WebService

public interface HelloWorld {

@WebMethod

String sayHi(@WebParam String text);

}

2.创建HelloWorld实现类

package com.jing.cxf.helloworld;

public class HelloWorldImpl implements HelloWorld {

public String sayHi(String name) {

String msg = "Hello " + name + "!";

System.out.println(">>>>>>>>>>"+msg );

return msg;

}

3)发布服务(CXF自带Jetty服务器,所以无需Tomcat就可发布,只要导入jetty包就可以了)

package com.jing.cxf;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class ServerPublish {
public static void main(String[] args) throws Exception {
// 创建WebService服务工厂
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
// 注册服务类
factory.setServiceClass(HelloWorldImpl.class);
//发布地址 访问:http://localhost:9000/HelloWorld?wsdl,cxf本身可以不起tomcat服务而依据jetty
// 发布,只要导入jetty相关jar包就可以了。
factory.setAddress("http://localhost:9000/ws/HelloWorld");
factory.create();  // 创建WebService

}
}
通过http://localhost:9000/HelloWorld?wsdl就可以访问服务了。

4)java客户端访问服务接口

package com.jing.cxf;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class ClientFangWen {
public static void main(String[] args) {
//jaxws代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//访问服务类接口 ,注意:必须是接口
factory.setServiceClass(HelloWorld.class);
//访问地址
factory.setAddress("http://localhost:9000/ws/HelloWorld");
//得到服务类
HelloWorld helloworld = (HelloWorld) factory.create();
String str = helloworld.sayHi("kongxx");
System.out.println("返回信息:"+str);
}
}


至此一个cxf流程完毕,首先通过ServerPublish类启动服务,访问http://localhost:9000/ws/HelloWorld?wsdl就可以看到信息了

通过http://localhost:9000/ws/HelloWorld/sayHi?arg0=jing访问一个特定方法

(如果服务接口方法参数没用webParam修饰,访问时就是arg0=jing这种了)

集成Sping与Web容器

修改web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:**/applicationContext.xml</param-value>
</context-param>

</web-app>
applicationContext.xml中配置cxf服务:

<?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"> <!-- 导入cxf的jar自带的三个xml -->
<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" />

<!--声明一个服务,address就是这个服务的访问地址,起什么都行  -->
<jaxws:endpoint id="helloworld"
implementor="com.jing.cxf.HelloWorldImpl" address="/HelloWorld" />

</beans>
至此完成。

通过http://localhost:8081/项目上下文/ws/HelloWorld?wsdl访问这个服务的所有对外方法

通过http://localhost:8081/项目上下文/ws/HelloWorld/sayHi?arg0=jing访问一个特定方法,

其中arg0=jing的arg0对应@WebParm定义的name值,如果没给就是默认的arg0

远程客户端访问:

package com.jing.cxf;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
/**
* @author jingguoqiang
* @desc  org.apache.cxf jar包写的客户端
*/
public class ClientFangWen {
public static void main(String[] args) {
//jaxws代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//访问服务类接口 ,注意:必须是接口
factory.setServiceClass(HelloWorld.class);
//访问地址
// factory.setAddress("http://localhost:9000/ws/HelloWorld");
factory.setAddress("http://localhost:8081/Zt/ws/HelloWorld");
//得到服务类接口
HelloWorld helloworld = (HelloWorld) factory.create();
String str = helloworld.sayHi("XIAOJING");
System.out.println("返回信息:"+str);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: