您的位置:首页 > 其它

CXF Web Service简单应用

2016-09-27 19:09 399 查看
        工欲善其事,必先利其器。在开始CXF Web Service 开始之前,先进性CXF的下载,配置,我比较崇拜官网,所以给出的下载地址既是官网地址,下载地址:http://cxf.apache.org/download.html。        接下来在Eclipse中配置CXF ,配制方法:       Windows——>preferences——>Web Services——>CXF 2.x Preferences——>Runtime下添加你的cxf的路径,有雨我已经配置过,所以配置后结果如图:完成CXF的配置后,开始一个简单的Web Service开发。1.创建一个简单的web项目,并将中的jar包导入项目。可以找到你的cxf-2.6.1文件下lib文件中的jar包把需要的jar复制粘贴到你项目的lib下,所需jar如图所示:项目右键——>Build Path——>Configure Build Path...——>Libraries——>Add Libraries——>CXF Runtime.项目结构如图:实体类如下
import java.io.Serializable;

public class User implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

private String id;
private String name;
private String age;
private String description;
public User() {
super();
}
public User(String id, String name, String age, String description) {
super();
this.id = id;
this.name = name;
this.age = age;
this.description = description;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public String getDescription() {
return description;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setDescription(String description) {
this.description = description;
}

}
创建Web Service对外暴漏的接口
import java.util.List;

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

import com.lvdun.model.User;
/*
*
*
* 无论是xml + tomact
*
* 还是发布方式,都需要
*
* @WebService
*
* */
@WebService(targetNamespace="http://com.lvdun.service/")
public interface HelloWorld {

String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List<User> userList);

}
创建服务接口的实现
import java.util.List;

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

import com.lvdun.model.User;
/*
*
*
* 无论是xml + tomact
*
* 还是发布方式,都需要
*
* @WebService
*
* */
@WebService(targetNamespace="http://com.lvdun.service/")
public interface HelloWorld {

String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List<User> userList);

}
服务端的启动
import javax.xml.ws.Endpoint;

import com.lvdun.serviceImpl.HelloWorldImpl;

/*
*
* http://blog.csdn.net/mafan121/article/details/43271153 *
*
* http://blog.csdn.net/hu_shengyang/article/details/38384597 *
* */
public class WebServiceApp {

public static void main(String[] args) {

System.out.println("web service start");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:8080/WebService/services/WebService";
Endpoint.publish(address, implementor);
System.out.println("web service started");

}

}
选中服务端运行类,右键--run application,在浏览其中输入:
<span style="font-size:24px;">http://localhost:8080/WebService/services/WebService</span><span style="font-size: 24px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">?wsdl,若看到如下效果则说明该WebService发布成功。</span>
<span style="font-size: 24px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><p><span style="font-size: 24px;">客户端访问</span></p><p><span style="font-size: 24px;">
</span></p><p><span style="font-size: 24px;">新建客户端项目,java web或者java项目都可以,导入的jar包如下</span></p><p><span style="font-size: 24px;">
</span></p><p><span style="font-size: 24px;">客户端项目结构如图</span></p><p><span style="font-size: 24px;">
</span></p><p><span style="font-size: 24px;">客户端启动如下</span></p><p></p><pre code_snippet_id="1903566" snippet_file_name="blog_20160927_6_1171960" name="code" class="java">import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.lvdun.model.User;
import com.lvdun.service.HelloWorld;

public class WebServiceClient {

public static void main(String[] args) {
JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();
jwpfb.setServiceClass(HelloWorld.class);
jwpfb.setAddress("http://localhost:8080/WebService/services/WebService");
HelloWorld hw = (HelloWorld) jwpfb.create();
User user = new User();
user.setName("马克思");
user.setDescription("怀念马克思");
System.out.println(hw.sayHiToUser(user)+"=======");

}

}
</pre><pre code_snippet_id="1903566" snippet_file_name="blog_20160927_8_15718" name="code" class="java">以上服务器端是通过 Endpoint.publish(address, implementor);发布的,若不喜欢这种方式,也可以
部署到tomact中,在tomact中部署需要进行一下配置
首先在web-inf下新建cxf-servlet.xml,如下:
<pre name="code" class="html"><?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"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<!-- ;服务接口 -->
<!-- address为服务发布二级地址 完整地址为 /项目发布名称/cfx拦截地址/address   (cfx拦截地址在web.xml中url-pattern标签中配置)  -->
<!--服务实现类 -->
<jaxws:server id="jaxwsService" serviceClass="com.lvdun.service.HelloWorld" address="/WebService" >
<jaxws:serviceBean >
<bean class="com.lvdun.serviceImpl.HelloWorldImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
</pre>还需要在web.xml中配置如下<p></p><pre>
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>WebService</display-name>

<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>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
</pre><pre code_snippet_id="1903566" snippet_file_name="blog_20160927_15_8117153" name="code" class="html">客户端的请求方式也包括很多种,<pre name="code" class="java">import java.lang.reflect.Method;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class JaxWsDynamicClient {

public static void main(String[] args) throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/WebService/services/WebService?wsdl");

//这种方式调用model要和service接口放在一个package中
Object user = Thread.currentThread().getContextClassLoader().loadClass("com.lvdun.service.User").newInstance();
// Object user = Class.forName("com.lvdun.model.User").newInstance();

Method m1 = user.getClass().getMethod("setName", String.class);
Method m2 = user.getClass().getMethod("setDescription", String.class);

m1.invoke(user, "马克思");
m2.invoke(user, "怀念马克思");

Object[] response = client.invoke("sayHi", "ceshi");
Object[] response1 = client.invoke("sayHiToUser", user);
System.out.println("Response is " + response[0]);
System.out.println("Response is " + response1[0]);

}

}
不过这种请求方式需要修改自己Eclipse的编译方式是jdk而不是jre,我就是在这上面浪费了
很多时间,还有其他请求方式如,ajax等等,这些我还没有做出来,就不放代
码了。
这是源代码地址 :<a target=_blank href="http://download.csdn.net/detail/u012577528/9641484">CXF Web Service & client  </a></span>
可能有些人已经发现我的demo和网上的很多例子相同,但是网上的例子很多
关键点没有讲到,会给新学者造成一定的困惑,我就是踩了很多坑,才把网上
的例子跑出来。
由于我的表述能力较弱,可能很多知识都没有体现,希望学习的朋友多多思考
如各个注解是什么意思。
其中我主要参考的博客已经在代码注释中体现,现在我再把参考的博客放在最
后,供大家参考:
<a target=_blank href="http://blog.csdn.net/mafan121/article/details/43271153">WebService——CXF方式创建WebService服务端 </a>
<a target=_blank href="http://blog.csdn.net/hu_shengyang/article/details/38384597">CXF实现webService服务(一) </a>
<a target=_blank href="http://blog.csdn.net/look85927/article/details/13000117"> webservice之CXF注解实现(一) </a>
<a target=_blank href="http://reymont.iteye.com/blog/1511176">开发CXF JAVA客户端</a>
遇到问题不要着急,多思考,喜欢看stack overflow。
</pre><pre code_snippet_id="1903566" snippet_file_name="blog_20160927_38_8136081" name="code" class="java">

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web service cxf