- 浏览: 545863 次
- 性别:
- 来自: 西安
文章分类
- 全部博客 (251)
- UML/RUP/软件工程 (0)
- DDD:领域驱动设计 (1)
- IaaS/Paas/SaaS (1)
- Hadoop/YARN (10)
- PBDP项目 (2)
- OSGi-基础 (7)
- OSGi-Aries (2)
- OSGi-SpringDM (32)
- Oracle/MySQL/SS (8)
- Felix/Equinox/Bnd (14)
- Virgo Server/Gemini (7)
- Weblogic/JBoss/Tomcat (10)
- ActiveMQ (14)
- Camel (1)
- Spring Roo/Eclipse (7)
- Java/JSP/JSF (10)
- Maven (19)
- ESB-Mule (1)
- ESB-ServiceMix (18)
- SOA/SCA/SDO (12)
- WebService/RESTful (17)
- JS/jQuery/ExtJS (4)
- Spring/JPA/MVC (15)
- SpringSecurity (5)
- BPM/jBPM (3)
- Hudson/SVN/CI (0)
- LinuxOS/虚拟化 (25)
- Office/OpenOffice (1)
- 项目管理 (5)
- POI/jFreeChart (1)
最新评论
-
panamera:
请问JMS Transport 发布的webservice 是 ...
CXF 提供的Service Transport-JMS Transpor -
jianyi296:
where is attachment.
CXF WebService Dynamic Client -
hj01kkk:
您好,我用jdbc-user-service为什么每次验证时都 ...
SpringSecurity笔记3-Authenticating Users:Authenticaton Strategy -
wufenglin1231:
你好,我在实现Mapping exceptions to re ...
RESTful 异常处理 -
xchd:
[echo] Project: common
[echo ...
Hive安装配置学习笔记
下面已具体的例子演示开发End-to-End RESTful WebService。
完整代码参考http://springsfeng.iteye.com/blog/1634753附件。
网络资源:http://suhuanzheng7784877.iteye.com/blog/1090370
1. 创建用于Request和Reponse的对象
import java.util.Collection; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Category") public class Category { private String categoryId; private String categoryName; private Collection<Book> books; //get和set方法 ...... }
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Book") public class Book { private String bookId; private String bookISBNnumber; private String bookName; private String author; //get和set方法 ...... }
2. 创建服务实现类
//JAX-RS Imports import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; /* * CategoryService class - Add/Removes category for books */ @Path("/categoryservice") @Produces({"application/json","application/xml"}) public class CategoryService { private CategoryDAO categoryDAO = new CategoryDAO(); public CategoryDAO getCategoryDAO() { return categoryDAO; } public void setCategoryDAO(CategoryDAO categoryDAO) { this.categoryDAO = categoryDAO; } @GET @Path("/category/{id}") @Produces({"application/json","application/xml"}) public Category getCategory(@PathParam("id") String id) { System.out.println("getCategory called with category id: " + id); Category cat = (Category) getCategoryDAO().getCategory(id); if (cat == null) { ResponseBuilder builder = Response.status(Status.BAD_REQUEST); builder.type("application/xml"); builder.entity("<error>Category Not Found</error>"); throw new WebApplicationException(builder.build()); } else { return cat; } } @POST @Path("/category") @Consumes({"application/json","application/xml"}) public Response addCategory(Category category) { System.out.println("addCategory called"); Category cat = (Category) getCategoryDAO().getCategory(category.getCategoryId()); if (cat != null) { return Response.status(Status.BAD_REQUEST).build(); } else { getCategoryDAO().addCategory(category); return Response.ok(category).build(); } } @DELETE @Path("/category/{id}") @Consumes({"application/json","application/xml"}) public Response deleteCategory(@PathParam("id") String id) { System.out.println("deleteCategory with category id : " + id); Category cat = (Category) getCategoryDAO().getCategory(id); if (cat == null) { return Response.status(Status.BAD_REQUEST).build(); } else { getCategoryDAO().deleteCategory(id); return Response.ok().build(); } } @PUT @Path("/category") @Consumes({"application/json","application/xml"}) public Response updateCategory(Category category) { System.out.println("updateCategory with category id : " + category.getCategoryId()); Category cat = (Category) getCategoryDAO().getCategory(category.getCategoryId()); if (cat == null) { return Response.status(Status.BAD_REQUEST).build(); } else { getCategoryDAO().updateCategory(category); return Response.ok(category).build(); } } @POST @Path("/category/book") @Consumes({"application/json","application/xml"}) public Response addBooks(Category category) { System.out.println("addBooks with category id : " + category.getCategoryId()); Category cat = (Category) getCategoryDAO().getCategory(category.getCategoryId()); if (cat == null) { return Response.status(Status.NOT_FOUND).build(); } else { getCategoryDAO().addBook(category); return Response.ok(category).build(); } } @GET @Path("/category/{id}/books") @Consumes({"application/json","application/xml"}) public Response getBooks(@PathParam("id") String id) { System.out.println("getBooks called with category id : " + id); Category cat = (Category) getCategoryDAO().getCategory(id); if (cat == null) { return Response.status(Status.NOT_FOUND).build(); } else { cat.setBooks(getCategoryDAO().getBooks(id)); return Response.ok(cat).build(); } } }
3. 创建Server端,使用内嵌的Jetty启动:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import org.pbdp.sample.rest.Category; import org.pbdp.sample.rest.CategoryService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CategoryServerStart { public static void main(String[] args) { ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( new String[] { "org/pbdp/sample/rest/restapp.xml" }); CategoryService categoryService = (CategoryService) appContext.getBean("categoryService"); // Service instance JAXRSServerFactoryBean restServer = new JAXRSServerFactoryBean(); restServer.setResourceClasses(Category.class); restServer.setServiceBean(categoryService); restServer.setAddress("http://localhost:9000/"); restServer.create(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { br.readLine(); } catch (IOException e) { } System.out.println("Server Stopped"); System.exit(0); } }
4. 创建Client端
import java.util.Iterator; import org.apache.cxf.jaxrs.client.WebClient; import org.pbdp.sample.rest.Book; import org.pbdp.sample.rest.Category; public class CategoryServiceClient { public static void main(String[] args) { // Service instance WebClient client = WebClient.create("http://localhost:9000/"); Category category = client.path("categoryservice/category/001").accept("application/xml").get(Category.class); System.out.println("Category details from REST service."); System.out.println("Category Name " + category.getCategoryName()); System.out.println("Category Id " + category.getCategoryId()); System.out.println("Book details for Category " + category.getCategoryId() + " from REST service"); WebClient clientBook = WebClient.create("http://localhost:9000/"); Category categoryBooks = clientBook.path("categoryservice/category/001/books").accept("application/xml").get(Category.class); Iterator<Book> iterator = categoryBooks.getBooks().iterator(); while (iterator.hasNext()) { Book book = iterator.next(); System.out.println("Book Name " + book.getBookName()); System.out.println("Book ISBN " + book.getBookISBNnumber()); System.out.println("Book ID " + book.getBookId()); System.out.println("Book Author " + book.getAuthor()); } } }
5. 配置REST WebService在Tomcat中启动
<jaxrs:server id="categoryRESTService" address="/"> <jaxrs:features> <cxf:logging /> </jaxrs:features> <jaxrs:serviceBeans> <ref bean="categoryService" /> </jaxrs:serviceBeans> </jaxrs:server>
6. 运行测试
(1) 运行CategoryServerStart;
(2) 运行CategoryServiceClient
Client输出:
Category details from REST service. Category Name Java Category Id 001 Book details for Category 001 from REST service Book Name Spring Series Book ISBN ISB001 Book ID 001 Book Author Naveen Balani Book Name CXF Series Book ISBN ISB002 Book ID 002 Book Author Rajeev Hathi
Server输出:
getCategory called with category id: 001 getBooks called with category id : 001
(3) 浏览器访问,如:
http://localhost:9000/categoryservice/category/001
http://localhost:9000/categoryservice/category/001/books
7. CXF RESTful WebService异常处理
JAX-RS提供了WebApplicationException(继承自RuntimeException), WebApplicationException采用HTTP状态
码或javax.ws.rs.core.Response作为给客户端的响应。如CategoryService中的代码:
ResponseBuilder builder = Response.status(Status.BAD_REQUEST); builder.type("application/xml"); builder.entity("<error>Category Not Found</error>"); throw new WebApplicationException(builder.build());
发表评论
-
测试RESTful WebService的客户端工具-Poster
2012-08-24 10:54 1443Poster是一款用于与WebService副去其端进行交互并 ... -
CXF 提供的Service Transport-JMS Transpor
2012-08-22 09:33 1422下面以一个例子来展示JMS Transport的功能: 1. ... -
CXF 提供的Service Transport-HTTPS Transpor
2012-08-20 11:22 1102HTTPS是HTTP和安全协议的联合,用于访问敏感的信息。HT ... -
CXF 提供的Service Transport-HTTP Transport
2012-08-20 09:32 1412完整代码参考http://springsfeng.ite ... -
基于Simple Fronted的WebService开发
2012-08-20 08:54 986采用Simple frontend方式开发WebService ... -
Web Service Context示例
2012-08-17 11:28 1354Web service context指Endpoint之间传 ... -
CXF Provider and Dispatch Services
2012-08-17 10:22 1523Provider and Dispatch services用 ... -
CXF WebService Dynamic Client
2012-08-17 09:48 5493即在在运行期间在内存中动态的生成Client(SEI和Data ... -
Contract-First方式开发WebService及WSDL2Java工具
2012-08-16 10:26 1850WSDL文件:参考附件OrderP ... -
CXF 架构
2012-08-15 10:36 1596CXF Architecture 建立在下 ... -
WebService定义及SOAP通信方式
2012-08-14 09:47 1610W3C对WebService的定义: A Web ... -
RESTful 异常处理
2012-07-27 11:42 20251. Using WebApplicaitonExceptio ... -
返回信息给Consumer
2012-07-27 10:17 1659return four types of Java const ... -
传输信息至Resource Classes and Methods
2012-07-27 08:52 1505运行期间,Parameters, fields, and be ... -
RESTful 中创建"资源"
2012-07-26 09:19 1583RESTful 中所有的请求都是由资源来处理的。JAX-RS ... -
RESTful Web Services理论简介
2012-07-26 08:48 1200Representational State Transfer ...
相关推荐
赠送jar包:cxf-rt-transports-http-3.0.1.jar; 赠送原API文档:cxf-rt-transports-http-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-transports-http-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-transports-...
赠送jar包:cxf-rt-frontend-simple-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-simple-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-simple-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-...
赠送jar包:cxf-rt-bindings-soap-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-soap-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-soap-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-soap-...
赠送jar包:cxf-rt-frontend-jaxws-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxws-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxws-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxws...
赠送jar包:cxf-core-3.0.1.jar; 赠送原API文档:cxf-core-3.0.1-javadoc.jar; 赠送源代码:cxf-core-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-core-3.0.1.pom; 包含翻译后的API文档:cxf-core-3.0.1-...
赠送jar包:cxf-rt-frontend-jaxrs-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxrs-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxrs-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxrs...
<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"/>
cxf-rt-frontend-jaxws-3.0.16.jar jar包下载3.0.16版本下载
赠送jar包:cxf-rt-rs-client-3.0.1.jar; 赠送原API文档:cxf-rt-rs-client-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-client-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-rs-client-3.0.1.pom; 包含...
赠送jar包:cxf-rt-bindings-xml-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-xml-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-xml-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-xml-3.0.1....
赠送jar包:cxf-rt-ws-addr-3.0.1.jar; 赠送原API文档:cxf-rt-ws-addr-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-ws-addr-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-ws-addr-3.0.1.pom; 包含翻译后的API...
赠送jar包:cxf-rt-rs-extension-providers-3.0.1.jar; 赠送原API文档:cxf-rt-rs-extension-providers-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-extension-providers-3.0.1-sources.jar; 赠送Maven依赖信息...
使用了cxf 同时实现了 RESTful WebService --项目启动后访问地址 http://localhost:8080/springMVC/services/rest/equipQuery/getUserById/1?_type=json 本人没有一一列明 xml json 以及post get请求 大家可以在...
cxf-dosgi-ri-singlebundle-distribution-1.4.0.jar
apache-cxf-2.7.18-src.zip
cxf-core-3.1.16版本jar包,用于java开发使用
cxf-core-3.0.0.jar,cxf-rt-bindings-soap-3.0.0.jar,cxf-rt-databinding-jaxb-3.0.0.jar,cxf-rt-frontend-jaxws-3.0.0.jar,cxf-rt-frontend-simple-3.0.0.jar,cxf-rt-transports-http-3.0.0.jar,cxf-rt-...
//第三方系统的webservice Client client = dcf.createClient("http://localhost:9010/CxfWebservice?wsdl"); //第三方系统的webservice参数个数 Object[] obj = new Object[2]; obj[0]="CxfWebservice"; obj...
赠送jar包:cxf-rt-databinding-jaxb-3.0.1.jar; 赠送原API文档:cxf-rt-databinding-jaxb-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-databinding-jaxb-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-...
赠送jar包:cxf-rt-ws-policy-3.0.1.jar; 赠送原API文档:cxf-rt-ws-policy-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-ws-policy-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-ws-policy-3.0.1.pom; 包含...