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 ...
随机推荐
- AngularJS 学习笔记二
AngularJS指令 指令 描述 讲解 ng_app 定义应用程序的根元素. 指令 ng_bind 绑定 HTML 元素到应用程序数据. 简介 ng_click 定义元素被单击时的行为. HTML ...
- osx升级到10.10后,用pod install报错最终解决办法
转载自:http://blog.csdn.net/liuyujinglove/article/details/40582197 http://blog.csdn.net/dqjyong/article ...
- CSS基础(02)
CSS 选择器 1.CSS3 选择器简介 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. 语法: 下面中"CSS" 列指示该属性是在哪个 CSS 版本中定义的.(C ...
- iOS开发-为程序添加应用设置
一.设置捆绑包 设置捆绑包是应用自带的一组文件,用于告诉设置该应用期望得到用户的哪些偏好设置. 新建设置捆绑包:Command+N,在iOS部分中的Resource,选择Settings Bundle ...
- Python3爬虫学习
学了几天python3,发现目前学到的与爬虫还是关系不大,所以现在准备爬虫和语言同步学习. 2016.8.9晚 先从最简单的开始,爬取指定url的所有内容: #encoding:UTF-8 impor ...
- GMT、UTC、PDT 时间是什么?Linux下如何调整时区
今天碰到一个时区配置问题,如果服务器时区配置不对,很可能在使用date相关函数时会出现莫名其妙的错误,现将相关时区说明及LINUX下调整时区方法记录如下,以做备忘. GMT GMT 是 Gree ...
- 教你50招提升ASP.NET性能(十五):解决性能问题时不要低估UI的价值
(26)Don’t underestimate the value of the UI when tackling performance problems 招数26: 解决性能问题时不要低估UI的价 ...
- codeforces Gym 100500H H. ICPC Quest 水题
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- HttpClient 设置代理方式
HttpClient httpClient = new HttpClient(); //设置代理服务器的ip地址和端口 httpClient.getHostConfiguration().setPro ...
- 使用C#: 自动切换鼠标的左右手习惯
不知道我得的是鼠标手,还是肩周炎. 长时间右手(或者左手)使用鼠标的话,那只胳膊便会不自在. 于是便有了切换鼠标主次要键的需求. [控制面板->鼠标]有更改它的设置,可点来点去让我觉得不够方便. ...