CXF(2.7.10)发布RESTful服务的简单示例

1. 在eclipse中建立web工程CxfRest。


2. 实体:
package com.huey.dream.bean;

import javax.xml.bind.annotation.XmlRootElement;

/**
 * 实体
 * @author huey2672
 * @version 1.0
 * @created 2014-8-12
 */
@XmlRootElement	// 将Java类映射到XML元素
public class Book {

	private Integer id;
	private String title;
	private String author;
	private String publisher;
	
	public Integer getId() {
		return id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	/**
	 * 需要一个无参的构造函数
	 */
	public Book() {
		super();
	}

	public Book(Integer id, String title, String author, String publisher) {
		super();
		this.id = id;
		this.title = title;
		this.author = author;
		this.publisher = publisher;
	}

	@Override
	public String toString() {
		return "Book [id=" + id + ", title=" + title + ", author=" + author
				+ ", publisher=" + publisher + "]";
	}
}

3. 服务接口:
package com.huey.dream.ws;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.huey.dream.bean.Book;

/**
 * 服务接口
 * @author huey2672
 * @version 1.0
 * @created 2014-8-12
 */
public interface BookRestService {

	@GET
	@Path("/book/{id}")	// 指定资源的URI
	@Produces( { MediaType.APPLICATION_XML } )	// 指定请求/响应的媒体类型
	public Book getBook(@PathParam("id") int id);
	
	@GET
	@Path("/books")	// 指定资源的URI
	@Produces( { MediaType.APPLICATION_XML } )	// 指定请求/响应的媒体类型
	public List<Book> getBooks();

}

4. 服务实现:
package com.huey.dream.ws.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebMethod;

import com.huey.dream.bean.Book;
import com.huey.dream.ws.BookRestService;

/**
 * 服务实现
 * @author huey2672
 * @version 1.0
 * @created 2014-8-12
 */
public class BookRestServiceImpl implements BookRestService {
	
	List<Book> books = new ArrayList<Book>();
	
	public BookRestServiceImpl() {
		books.add(new Book(1001, "嫌疑人X的献身", "东野圭吾", "南海出版公司"));
		books.add(new Book(1002, "追风筝的人", "卡勒德·胡赛尼 ", "上海人民出版社"));
		books.add(new Book(1003, "看见", "柴静 ", "广西师范大学出版社"));
		books.add(new Book(1004, "白夜行", "东野圭吾", "南海出版公司"));
	}
	
	@WebMethod
	public Book getBook(int id) {
		for (Book book : books) {
			if (id == book.getId()) {
				return book;
			}
		}
		return null;
	}
	
	@WebMethod
	public List<Book> getBooks() {	
		return books;
	}

}

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

	<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" />

	<bean id="bookRestService" class="com.huey.dream.ws.impl.BookRestServiceImpl" />

	<jaxrs:server id="bookService" address="/rest">
		<jaxrs:serviceBeans>
			<ref bean="bookRestService" />
		</jaxrs:serviceBeans>
	</jaxrs:server>
	
</beans>

6. 配置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>/*</url-pattern>
	</servlet-mapping>
	
</web-app>


7. 启动tomcat运行web工程,在浏览器中键入http://localhost:8080/CxfRest/rest/books,结果如下:
<?xml version="1.0"?>
<books>
    <book>
        <author>东野圭吾</author>
        <id>1001</id>
        <publisher>南海出版公司</publisher>
        <title>嫌疑人X的献身</title>
    </book>
    <book>
        <author>卡勒德·胡赛尼</author>
        <id>1002</id>
        <publisher>上海人民出版社</publisher>
        <title>追风筝的人</title>
    </book>
    <book>
        <author>柴静</author>
        <id>1003</id>
        <publisher>广西师范大学出版社</publisher>
        <title>看见</title>
    </book>
    <book>
        <author>东野圭吾</author>
        <id>1004</id>
        <publisher>南海出版公司</publisher>
        <title>白夜行</title>
    </book>
</books>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值