Jersey the RESTful Web Services in Java

來源:互聯網
上載者:User

標籤:

Jersey 是一個JAX-RS的實現, JAX-RS即Java API for RESTful Web Services, 支援按照具象狀態傳輸(REST)架構風格建立Web服務. REST 中最重要的概念是資源(resources),使用Global ID (通常使用 URI)標識. 用戶端應用程式使用 HTTP 方法 GET/ POST/ PUT/ DELETE 操作資源或資源集. RESTful Web 服務是使用 HTTP 和 REST 原理實現的 Web 服務. 通常, RESTful Web 服務應該定義以下方面:

Web 服務的基/根 URI,比如 http://host/<appcontext>/resources
支援 MIME 類型的響應資料,包括 JSON/XML/ATOM 等等
服務支援的操作集合, 例如 POST, GET, PUT 或 DELET.


JAX-RS 提供一個部署檢測的抽象類別Application用於聲明根資源和提供對應的類, 在支援Servlet 3.0及以上的容器上, 可以不需要web.xml來部署. 僅需要在某個class上使用@ApplicationPath, 並擴充Application

@ApplicationPath("/*")public class MyApplication extends Application {    @Override    public Set<Class<?>> getClasses() {        Set<Class<?>> s = new HashSet<Class<?>>();        s.add(HelloWorldResource.class);        return s;    }    ...}

而Jersey提供了自己實現的方式, 可以更便利地使用其進階特性

@ApplicationPath("resources")public class MyApplication extends ResourceConfig {    public MyApplication() {        packages("org.foo.rest;org.bar.rest");    }}

此時要在pom.xml中添加 failOnMissingWebXml 以免構建中出現丟失web.xml的錯誤

<plugins>    ...    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-war-plugin</artifactId>        <version>2.3</version>        <configuration>            <failOnMissingWebXml>false</failOnMissingWebXml>        </configuration>    </plugin>    ...</plugins>

在MyApplication()的初始化方法中,

packages("com.aa.bb", "com.aa.cc");可以提供多個用於自動掃描的包路徑. 這些路徑下, 就是標註了 @Path, @Produces, @GET等資訊的資源類.

register()方法用於註冊各種組件

 

Jersey the RESTful Web Services in Java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.