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的更多相关文章

  1. CXF(2.7.10) - RESTful Services, JSON Support

    在 CXF(2.7.10) - RESTful Services 介绍了 REST 风格的 WebService 服务,数据传输是基于 XML 格式的.如果要基于 JSON 格式传输数据,仅需要将注解 ...

  2. Python 和 Flask实现RESTful services

    使用Flask建立web services超级简单. 当然,也有很多Flask extensions可以帮助建立RESTful services,但是这个例实在太简单了,不需要使用任何扩展. 这个we ...

  3. CXF框架构建和开发 Services

    Apache CXF 是一个开源的 Services 框架,CXF 帮助您来构建和开发 Services 这些 Services 可以支持多种协议,比如:SOAP.POST/HTTP.RESTful ...

  4. 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 ...

  5. [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 ...

  6. Spring Security(三十二):10. Core Services

    Now that we have a high-level overview of the Spring Security architecture and its core classes, let ...

  7. CXF+Spring+Hibernate实现RESTful webservice服务端实例

    1.RESTful API接口定义 /* * Copyright 2016-2017 WitPool.org All Rights Reserved. * * You may not use this ...

  8. Apache CXF实战之四 构建RESTful Web Service

    Apache CXF实战之一 Hello World Web Service Apache CXF实战之二 集成Sping与Web容器 Apache CXF实战之三 传输Java对象 这篇文章介绍一下 ...

  9. 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 ...

随机推荐

  1. IOS Note - View Controller(视图控制器)

    Application Delegate(应用程序委托) Application Name: SingleView SingleViewAppDelegate.h #import <UIKit/ ...

  2. Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树

    C. Day at the Beach Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599/p ...

  3. QProcess调用外部程序方式的差异

    众所周知QProcess类的作用是启动一个外部的程序并与之交互它有三种方式调用外部程序: 1. execute 2. start 3. startDetached 从调用上看: execute是阻塞调 ...

  4. NDK环境配置

    1.下载安装插件:com.android.ide.eclipse.ndk_23.0.2.1259578.jar      copy到E:\eclipse\adt-bundle-windows-x86- ...

  5. 为CentOS 加入�本地源

    首先把光盘中的Packages文件夹复制到本地. [arm@Jarvis Packages]$ pwd /home/Packages 安装用于创建安装包依赖关系的软件createrepo. [arm@ ...

  6. du 和 df命令的区别(超赞)

    du和df命令都被用于获得文件系统大小的信息:df用于报告文件系统的总块数及剩余块数,du -s /<filesystem>用于报告文件系统使用的块数.但是,我们可以发现从df命令算出的文 ...

  7. Word2010 清除样式

    使用场景         有时候我们在网页上面粘贴一些精华文章或者从去整理别人已经完成的word的时候,会发现它自带的格式,可能并不是我们所理想的格式,所以此时就不得不去重新编辑其格式,但是word里 ...

  8. l​i​n​u​x添加​修​改​用​户​名​密​码

    语 法: useradd [-mMnr][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数>][-g <群组> ...

  9. java_log4j多文件配置

    今天配置了log4j中写多个文件的内容,配置了半天才搞出来,为了避免类似问题,写个博客吧. 首先说一下需求,每天要在7个文件夹中生成文件,文件格式为xxx.log.2000.01.01,自己开发个写文 ...

  10. Java中的DeskTop类

        在Jdk1.6以后新增加了一个类--DeskTop,在JDK中它的解释是这样的: The Desktop class allows a Java application to launch a ...