CXF(2.7.10) - RESTful Services
1. 定义 JavaBean。注意 @XmlRootElement 注解,作用是将 JavaBean 映射成 XML 元素。
package com.huey.demo.bean; import javax.xml.bind.annotation.XmlRootElement; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
public class Book { private String title;
private String author;
private String publisher;
private String isbn; }
package com.huey.demo.bean; import javax.xml.bind.annotation.XmlRootElement; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
public class ResultMsg { private String resultCode;
private String message; }
2. 定义服务接口。注意各个注解的作用。
package com.huey.demo.ws; import java.util.List; import javax.jws.WebService;
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.core.MediaType; import com.huey.demo.bean.Book;
import com.huey.demo.bean.ResultMsg; @WebService
public interface BookService { @GET // 指定请求方式
@Path("/book/{isbn}") // 指定资源的 URI
@Produces( { MediaType.APPLICATION_XML } ) // 指定请求/响应的媒体类型
public Book getBook(@PathParam("isbn") String isbn); @GET
@Path("/books")
@Produces( { MediaType.APPLICATION_XML } )
public List<Book> getBooks(); @POST
@Path("/book")
@Produces( { MediaType.APPLICATION_XML } )
public ResultMsg addBook(Book book); @PUT
@Path("/book/{isbn}")
@Produces( { MediaType.APPLICATION_XML } )
public ResultMsg updateBook(@PathParam("isbn") String isbn, Book book); @DELETE
@Path("/book/{isbn}")
@Produces( { MediaType.APPLICATION_XML } )
public ResultMsg deleteBook(@PathParam("isbn") String isbn);
}
3. 实现服务接口。
package com.huey.demo.ws.impl; import java.util.ArrayList;
import java.util.List; import javax.jws.WebService; import org.apache.commons.lang.StringUtils; import com.huey.demo.bean.Book;
import com.huey.demo.bean.ResultMsg;
import com.huey.demo.ws.BookService;
import com.sun.org.apache.commons.beanutils.BeanUtils; @WebService
public class BookServiceImpl implements BookService { List<Book> books = new ArrayList<Book>(); public BookServiceImpl() {
books.add(new Book("嫌疑人X的献身", "东野圭吾", "南海出版公司", "9787544245555"));
books.add(new Book("追风筝的人", "卡勒德·胡赛尼 ", "上海人民出版社", "9787208061644"));
books.add(new Book("看见", "柴静 ", "广西师范大学出版社", "9787549529322"));
books.add(new Book("白夜行", "东野圭吾", "南海出版公司", "9787544242516"));
} public Book getBook(String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
return book;
}
}
return null;
} public List<Book> getBooks() {
return books;
} public ResultMsg addBook(Book book) {
if (book == null || StringUtils.isEmpty(book.getIsbn())) {
return new ResultMsg("FAIL", "参数不正确!");
}
if (getBook(book.getIsbn()) != null) {
return new ResultMsg("FAIL", "该书籍已存在!");
}
books.add(book);
return new ResultMsg("SUCCESS", "添加成功!");
} public ResultMsg updateBook(String isbn, Book book) {
Book target = getBook(isbn);
if (target != null) {
ResultMsg resultMsg = new ResultMsg("SUCCESS", "更新成功!");
try {
BeanUtils.copyProperties(target, book);
} catch (Exception e) {
resultMsg = new ResultMsg("FAIL", "未知错误!");
}
return resultMsg;
}
return new ResultMsg("FAIL", "该书籍不存在!");
} public ResultMsg deleteBook(String isbn) {
Book book = getBook(isbn);
if (book != null) {
books.remove(book);
return new ResultMsg("SUCCESS", "删除成功!");
}
return new ResultMsg("FAIL", "该书籍不存在!");
}
}
4. Spring 配置。
<?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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
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
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <bean id="bookRestService" class="com.huey.demo.ws.impl.BookServiceImpl" /> <jaxrs:server id="bookService" address="/rest">
<jaxrs:serviceBeans>
<ref bean="bookRestService" />
</jaxrs:serviceBeans>
</jaxrs:server> </beans>
5. web.xml 配置。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
6. 启动 Tomcat 运行 web 工程。
7. 测试。
a) getBooks

b) getBook
c) addBook

d) updateBook

e) deleteBook

CXF(2.7.10) - RESTful Services的更多相关文章
- CXF(2.7.10) - RESTful Services, JSON Support
在 CXF(2.7.10) - RESTful Services 介绍了 REST 风格的 WebService 服务,数据传输是基于 XML 格式的.如果要基于 JSON 格式传输数据,仅需要将注解 ...
- Python 和 Flask实现RESTful services
使用Flask建立web services超级简单. 当然,也有很多Flask extensions可以帮助建立RESTful services,但是这个例实在太简单了,不需要使用任何扩展. 这个we ...
- CXF框架构建和开发 Services
Apache CXF 是一个开源的 Services 框架,CXF 帮助您来构建和开发 Services 这些 Services 可以支持多种协议,比如:SOAP.POST/HTTP.RESTful ...
- Service Station - An Introduction To RESTful Services With WCF
Learning about REST An Abstract Example Why Should You Care about REST? WCF and REST WebGetAttribute ...
- [EXP]Drupal < 8.5.11 / < 8.6.10 - RESTful Web Services unserialize() Remote Command Execution (Metasploit)
## # This module requires Metasploit: https://metasploit.com/download # Current source: https://gith ...
- Spring Security(三十二):10. Core Services
Now that we have a high-level overview of the Spring Security architecture and its core classes, let ...
- CXF+Spring+Hibernate实现RESTful webservice服务端实例
1.RESTful API接口定义 /* * Copyright 2016-2017 WitPool.org All Rights Reserved. * * You may not use this ...
- Apache CXF实战之四 构建RESTful Web Service
Apache CXF实战之一 Hello World Web Service Apache CXF实战之二 集成Sping与Web容器 Apache CXF实战之三 传输Java对象 这篇文章介绍一下 ...
- CXF(2.7.10) - A simple JAX-WS service
1. 下载 apache-cxf-x.x.x.zip,在工程导入依赖的 jar 包.也可以基于 Maven 构建工程. 2. 定义服务接口. package com.huey.demo.ws; imp ...
随机推荐
- Contest 7.21(贪心专练)
这一次都主要是贪心练习 练习地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26733#overview Problem APOJ 13 ...
- HDU 4432 Sum of divisors (水题,进制转换)
题意:给定 n,m,把 n 的所有因数转 m 进制,再把各都平方,求和. 析:按它的要求做就好,注意的是,是因数,不可能有重复的...比如4的因数只有一个2,还有就是输出10进制以上的,要用AB.. ...
- Objective-c setObject:forKey:和setValue:forKey:的区别
setObject:forKey: 是NSMutableDictionary类的方法 key参数类型可以是任意类型对象 ...
- /boot/grub/menu.lst详解
基本概念menu.lst有时候也叫grub.conf,但是/boot/grub/下会有一个名叫menu.lst的符号链接指向它.它是grub引导系统的配置文件.基本选项default 0timeout ...
- JavaEE通过response实现请求重定向
请求重定向指的是一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之为请求重定向.302状态码和location头即可实现重定向. 请求重定向最常见的应用场景就是用户登录. 下面 ...
- QM课程03-采购中的质量管理
QM模块被包含于采购过程的下列决策制定阶段:查询.供应商选择.采购订单.货物订单.收货.收到检查和收货数量的下达. 供应商下达 质量部门为一种被指定的物料下达一个供应商,它可以限制或限定下达的数量.如 ...
- [Angular 2] Understanding OpaqueToken
When using provider string tokens, there’s a chance they collide with other third-party tokens. Angu ...
- Unity3D脚本--经常使用代码集
1. 訪问其他物体 1) 使用Find()和FindWithTag()命令 Find和FindWithTag是很耗费时间的命令,要避免在Update()中和每一帧都被调用的函数中使用.在Start() ...
- hibernate 实体关系映射笔记
@经常使用属性说明: @Entity:实体类 @Table:指定相应数据表 @Id:主键,使用能够为null值的类型,假设实体类没有保存到数据库是一个暂时状态 @Col ...
- 一个想法(续二):换个角度思考如何解决IT企业招聘难的问题!
前言: 上一篇文章:一个想法:成立草根技术联盟对开发人员进行技术定级解决企业员工招聘难问题! 当时写文的思维,是从一个公益组织的角度的思考. 因此,有不少关于从利出发的反方观点,的确是值的思考! 任何 ...