码迷,mamicode.com
首页 > 其他好文 > 详细

Jersey(1.19.1) - Hello World

时间:2016-04-11 00:14:17      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:

1. Maven Dependency

技术分享
<properties>
    <jersey.version>1.19.1</jersey.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.4</version>
    </dependency>
</dependencies>
View Code

 

2. JavaBean

package com.huey.hello.jersey.bean;

import javax.xml.bind.annotation.XmlRootElement;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
@XmlRootElement
public class Book {
    
    private String title;
    private String[] author;
    private String publisher;
    private double price;
    private String isbn;
    
}

 

3. RESTful Web Services

package com.huey.hello.jersey.service;

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

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
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.MediaType;

import com.huey.hello.jersey.bean.Book;

@Path("/books")
public class BookService {

    private static List<Book> books;
    
    static {
        books = new ArrayList<Book>();
        books.add(new Book("高性能MySQL", 
                new String[]{"Baron Schwartz", "Peter Zaitsev"},
                "电子工业出版社", 
                128.00,
                "9787121198854"));
        books.add(new Book("HTTP权威指南", 
                new String[]{"David Gourley", "Brian Totty"},
                "人民邮电出版社", 
                109.00,
                "9787115281487"));
    }
    
    @GET
    @Produces( {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML} )
    public List<Book> getBooks() {
        return books;
    }
    
    @GET
    @Path("/{isbn}")
    @Produces( {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML} )
    public Book getBook(@PathParam("isbn") String isbn) {
        for (Book book : books) {
            if (book.getIsbn().equals(isbn)) {
                return book;
            }
        }
        throw new WebApplicationException(404);
    }
    
    @POST
    @Consumes( {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML} )
    public void addBook(Book book) {
        books.add(book);
    }
    
}

 

4. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <display-name>hello-jersey</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>    </welcome-file-list>

    <servlet>
        <servlet-name>JerseyRESTService</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.huey.hello.jersey.service</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JerseyRESTService</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

 

5. 部署启动 Web 工程。

 

6. 测试。

 

Jersey(1.19.1) - Hello World

标签:

原文地址:http://www.cnblogs.com/huey/p/5376424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!