Spring4+CXF实战例⼦
Spring4 + CXF3.1.5 RESTful Web Service 实战例⼦(调试通过)
RESTful 简化了 web service 的设计,它不再需要 wsdl ,也不再需要 soap 协议,⽽是通过最简单的 http 协议传输数据 ( 包括 xml 或 json) 。既简化了设计,也减少了⽹络传输量(因为只传输代表数据的 xml 或 json ,没有额外的 xml 包装),JAX-RS ,全称 Java API for RESTfulWebService。该规范定义了⼀系列的注解。
下⾯为⼤家介绍使⽤ cxf 开发 RESTfulWebService
Cxf3.1.5 实现 jax-rs 的全套规范。在使⽤它时,最好全部包含它的lib(在它安装⽬录下的lib下⾯),从其官⽹上下载apache-cxf-3.1.5.zip,解压后,在解压的lib⽬录就是运⾏时需要的⽂件。
服务端
Spring4 +cxf3.1.5 开发 RESTfulweb service。
在eclipse上,选择开发动态web 应⽤项⽬,然后选择tomcat8作为web server。
然后,在项⽬视图中选择build path,去添加spring4,cxf3.1.5的lib (为了省事,直接把cxf下⾯的lib全部包含过来就可以了),如图:
同时,你得把这些libs都copy到webcontent|web-inf|lib下⾯去,这样,eclipse在制作WAR⽂件时,会把这些.jar⽂件copy到war⽂件中去。
否则,tomcat会因为不到这些jar⽂件⽽⽆法启动this web service.
本例⼦中,开发环境:
服务器端:eclipse + tomcat8 + CXF3.1.5 + Sring4
⾃⼰得去下载Spring4的jar包和CXF3.1.5的jar包,放到eclipse这个项⽬的web-content | web-info| lib下⾯
Client端: eclipse + CXF3.1.5 + httpclient + JSON + Java SE8
在tomcat上⾯的运⾏的server代码
EmployeeWSRestFulService.java (interface)
packagecxf.server;
importjava.util.*;
importjavax.*;
import javax.ws.rs.*;
/**
*
* @author bigtree
* restful URI 匹配原理,它是咋构成的? access the below page
*www.doczj/doc/dc2f78f231126edb6f1a10e2.html /bigtree_3721/article/details/51158758 *
*/
@Path("/")
public interface EmployeeWSRestFulService {
/**
* JSON提交
* url:
* localhost:8080/MyWebserviceRestfulSpringServer /WSRest/userws/addUser
* Jsonformat:{"user":{"id":123,"name":"newBook"}}
* @param book
* @return
*/
@Path("/addUser/")
@POST
@Consumes({"application/json","application/xml"})
Response addUser(User user);
@Path("/delUser/{id}/")
@DELETE
@Consumes({"application/json","application/xml"})
Response delUser(@PathParam("id") String id);
@Path("/updateUser/")
@PUT
@Consumes({"application/json","application/xml"})
Response updateUser(User user);
/* localhost:8080/MyWebserviceRestfulSpringServ er/WSRest/userws/getUserById/1/ * JSON: response content as below
*
* HTTP/1.1 200 OK
* {"user":{"id":1,"name":"bigtree"}}
*/
/*
* XML response content as below
*
* HTTP/1.1 200 OK
1bigtree
*
*/
@Path("/getUserById/{id}/")
@GET
@Produces({"application/json","application/xml"}) //json is high priority
User getUserById(@PathParam("id") String id);
/* localhost:8080/MyWebserviceRestfulSpringServ er/WSRest/userws/
* response:
* HTTP/1.1 200 OK
* {"user":[{"id":1,"name":"bigtree"},{"id":2,"name":" jackie"}]}
*
*/
@Path("/")
@GET
//json is high priority, default is application/xml
@Produces({"application/json","application/xml"})
ListfindAllUsers();
}
spring frameworkMyServiceImpl.java (server 的实现类)
package cxf.server;
import java.util.*;
import javax.ws.rs.*;
import javax.*;
import www.doczj/doc/dc2f78f231126edb6f1a10e2.html .URI; import javax.Response.*;
public class MyServiceImpl implements EmployeeWSRestFulService { private void init(){
User user = new User();
user.setId("1");
user.setName("bigtree");
users.Id(), user);
user = new User();
user.setId("2");
user.setName("jackie");
users.Id(), user);
}
private HashMap users = new HashMap();
public MyServiceImpl(){
init();
}
public Response addUser(User user) {
users.Id(), user);
System.out.println("User entity to add: user id= "+Id()+" name="+Name()); System.out.println("adding user succeed");
System.out.println("total users in pool="+users.size());
return Response.ok().build();
}
public Response delUser(String id) {
User ve(id);
if(muser == null) {
// this user with this id not exist
System.out.println("delUser(): no user entry found to delete");
return Response.status(Status.BAD_REQUEST).build();
}
else
return Response.ok().build();
}
public Response updateUser(User user) {
users.Id(), user);
System.out.("1").getName());
return Response.ok().build();
}
public User getUserById(String id) {
User (id);
if(muser==null)
System.out.println("getUserById(): no user entry found");
return muser;
}
public ListfindAllUsers() {
Listuserlist = new ArrayList();
Iterator userit = users.keySet().iterator();
while(userit.hasNext()){
userlist.(()));
}
return userlist;
}
}
Spring 配置⽂件 l 配置内容如下, (applicationContext是 Spring
从tomcat启动需要的缺省配置⽂件)
xmlns="www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/beans" xmlns:a
op="www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/aop"
xmlns:context="www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/contex t"
xmlns:tx="www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/tx"
xmlns:cache="www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/cache" x
mlns:p="www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/p"
xmlns:jaxws="www.doczj/doc/dc2f78f231126edb6f1a10e2.html /jaxws" xmlns:jaxrs="http:
//www.doczj/doc/dc2f78f231126edb6f1a10e2.html /jaxrs"
xsi:schemaLocation=
"www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/beans
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/beans/spring-beans -4.0.xsd
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/aop
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/aop/spring-aop-4.0. xsd
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/context
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/context/spring-con text-4.0.xsd
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/tx
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/tx/spring-tx-4.0.x sd
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/cache
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schema/cache/spring-cache -4.0.xsd
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /jaxrs
www.doczj/doc/dc2f78f231126edb6f1a10e2.html /schemas/jaxrs.xsd
">
org.t.ContextLoaderL istener
CXF315
f.transport.servlet.CXFServlet
1
CXF315
/WSRest/*
上⾯绿⾊部分需要⾃⼰写的。
然后在eclipse中以输出上⾯的项⽬(eclipse会产⽣⼀个.war⽂件),启动tomcat,然后从进⼊tomcat 管理页⾯(
缺省是localhost:8080),点击"manager app",然后选择“WAR file to deploy”,选择上⾯产⽣的war⽂件,即可部署该web service服务了。
客户端
因为 RESTful 就是利⽤最原始的 http 协议传输数据,所以客户端其实就是⼀个 http客户端,有以下⼏种实现⽅式
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论