Jersey2.x框架搭建簡單的Restful API

Jersey2.x框架搭建簡單的Restful API

最近想用Jersey框架搭建一個Restful API項目,上網找了一下教程,發現網上的demo都是直接使用class定義API,而我的的想法是使用interface定義API接口,具體業務實現都放在實現類中,折騰了一個上午,終於找到解決的方式。java

具體步驟以下:git

  • 建立Web項目(Maven)

image

  • 添加Jersey依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.cent.demo</groupId>
    <artifactId>jersey</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>jersey Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <jersey.version>2.23.2</jersey.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- jersey dependencies -->
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>jersey</finalName>
    </build>
</project>
  • 定義API接口

定義Restful API接口,在接口類中定義好訪問路徑、http方式、數據類型、請求參數等web

package org.cent.demo.jersey.api;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 * Created by cent on 2016/9/18.
 */
@Path("/demo")
public interface JerseyDemoApi {

    @Path("hello")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String hello(@QueryParam("name") String name);
}
  • API接口實現類
package org.cent.demo.jersey.api.impl;

import org.cent.demo.jersey.api.JerseyDemoApi;

/**
 * Created by cent on 2016/9/18.
 */
public class JerserDemoApiImpl implements JerseyDemoApi {

    public String hello(String name) {
        return "Hello " + name + "!";
    }
}
  • 自定義ResourceConfig類

這個很關鍵,必須使用自定義的ResourceConfig類,裏面註冊API接口的實現類。apache

package org.cent.demo.jersey.config;

import org.cent.demo.jersey.api.impl.JerserDemoApiImpl;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;

/**
 * Created by cent on 2016/9/18.
 */
public class JerseyResourceConfig extends ResourceConfig {

    public JerseyResourceConfig() {
        //注意:這裏註冊的是接口實現類!!!
        register(JerserDemoApiImpl.class);

        //註冊請求日誌Filter,已過期,未找到替換方式
        register(LoggingFilter.class);
    }
}
  • 配置web.xml

配置Jersey servlet時,指定javax.ws.rs.Application參數值爲自定義的ResourceConfig類。json

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Jersey Demo</display-name>

    <servlet>
        <servlet-name>jerseyServlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <!-- 指定ResourceConfig初始化參數 -->
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>org.cent.demo.jersey.config.JerseyResourceConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jerseyServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>
  • 大功告成

Demo項目文件結構以下:api

image

  • 運行&測試

部署到Tomcat容器中運行,調用RestfulApi效果以下:app

image

  • 示例源碼

示例源碼地址:https://code.aliyun.com/cent/jersey-demo.git框架

相關文章
相關標籤/搜索