原文:https://docs.jboss.org/author/display/AS7/Java+API+for+RESTful+Web+Services+(JAX-RS)

Content

Tutorial Overview

This chapter describes the Java API for RESTful web services (JAX-RS, defined in JSR331). RESTEasy is an portable implementation of this specification which can run in any Servlet container. Tight integration with JBoss Application Server is available for optimal user experience in that environment. While JAX-RS is only a server-side specification, RESTeasy has innovated to bring JAX-RS to the client through the RESTEasy JAX-RS Client Framework.

Detailed documentation on RESTEasy is available here.

The source for this tutorial is in github repository git://github.com/tdiesler/javaee-tutorial.git

OpenShift, is a portfolio of portable cloud services for deploying and managing applications in the cloud. This tutorial shows how to deploy a RESTful web service on the free OpenShift Express JavaEE cartridge that runsJBossAS 7.

An application running on Android shows how to leverage JBoss technology on mobile devices. Specifically, we show how use the RESTEasy client API from an Android device to integrate with a RESTful service running on a JBossAS 7 instance in the cloud.

The following topics are addressed

What are RESTful web services

Creating a RESTful server endpoint

Deploying a RESTful endpoint to a JBossAS instance in the cloud

RESTEasy client running on an Android mobile device

What are RESTful Web Services?

information.gif

Coming Soon

This section is still under development.

RESTful web services are designed to expose APIs on the web. REST stands for Representational State Transfer. It aims to provide better performance, scalability, and flexibility than traditinoal web services, by allowing clients to access data and resources using predictable URLs. Many well-known public web services expose RESTful APIs.

The Java 6 Enterprise Edition specification for RESTful services is JAX-RS. It is covered by JSR-311 (http://jcp.org/jsr/detail/311.jsp). In the REST model, the server exposes APIs through specific URIs (typically URLs), and clients access those URIs to query or modify data. REST uses a stateless communication protocol. Typically, this is HTTP.

The following is a summary of RESTful design principles:

A URL is tied to a resource using the @Path annotation. Clients access the resource using the URL.

Create, Read, Update, and Delete (CRUD) operations are accessed via PUT, GET, POST, and DELETE requests in the HTTP protocol.

PUT creates a new resource.

DELETE deletes a resource.

GET retrieves the current state of a resource.

POST updates a resources's state.

Resources are decoupled from their representation, so that clients can request the data in a variety of different formats.

Stateful interactions require explicit state transfer, in the form of URL rewriting, cookies, and hidden form fields. State can also be embedded in response messages.

Creating a RESTful endpoint

A RESTful endpoint is deployed as JavaEE web archive (WAR). For this tutorial we use a simple library application to manage some books. There are two classes in this application:

Library

Book

The Book is a plain old Java object (POJO) with two attributes. This is a simple Java representation of a RESTful entity.

The Library is the RESTful Root Resource. Here we use a set of standard JAX-RS annotations to define

The root path to the library resource

The wire representation of the data (MIME type)

The Http methods and corresponding paths

The Library root resource uses these JAX-RS annotations:

AnnotationDescription

@Path

Identifies the URI path that a resource class or class method will serve requests for

@Consumes

Defines the media types that the methods of a resource class can accept

@Produces

Defines the media type(s) that the methods of a resource class can produce

@GET

Indicates that the annotated method responds to HTTP GET requests

@PUT

Indicates that the annotated method responds to HTTP PUT requests

@POST

Indicates that the annotated method responds to HTTP POST requests

@DELETE

Indicates that the annotated method responds to HTTP DELETE requests

For a full description of the available JAX-RS annotations, see the JAX-RS API documentation.

Package and build the endpoint

To package the endpoint we create a simple web archive and include a web.xml with the following content

warning.gif

Review

AS7-1674 Remove or explain why web.xml is needed for RESTful endpoints

The root context is defined in jboss-web.xml

The code for the JAX-RS part of this tutorial is available on https://github.com/tdiesler/javaee-tutorial/tree/master/jaxrs. In this step we clone the repository and build the endpoint using maven. There are a number of JAX-RS client tests that run against a local JBossAS 7 instance. Before we build the project, we set the JBOSS_HOME environment variable accordingly.

Arquillian, the test framework we use throughout this tutorial, can manage server startup/shutdown. It is however also possible to startup the server instance manually before you run the tests. The latter allows you to look at the console and see what log output the deployment phase and JAX-RS endpoint invocations produce.

Deploy the endpoint to OpenShift

First we need to create a free OpenShift Express account and select the JavaEE cartridge that runs JBossAS 7. Once we have received the confirmation email from OpenShift we can continue to create our subdomain and deploy the RESTful endpoint. A series of videos on the OpenShift Express page shows you how to do this. There is also an excellent quick start document that you have access to after login.

For this tutorial we assume you have done the above and that we can continue by creating the OpenShift application. This step sets up your JBossAS 7 instance in the cloud. Additionally a Git repository is configured that gives access to your deployed application.

Next, we can clone the remote Git repository to our local workspace

Because we want to deploy an already existing web application, which we'll build in the next step, we can safely remove the source artefacts from the repository.

Now we copy the JAX-RS endpoint webapp that we build above to the 'deployments' folder and commit the changes.

You can now use curl or your browser to see the JAX-RS endpoint in action. The following URL lists the books that are currently registered in the library.

Building the mobile client

The source associated with this tutorial contains a fully working mobile client application for the Android framework. If not done so already please follow steps described in Installing the SDK. In addition to the Android SDK, I recommend installing the m2eclipse and the EGit plugin to Eclipse.

First, go to File|Import... and choose "Existing Maven Projects" to import the tutorial sources

ImportExistingMavenProject.png?version=1&modificationDate=1314691563000

You project view should look like this

ProjectExplorerA.png?version=1&modificationDate=1314691803000

Then go to File|New|Android Project and fill out the first wizard page like this

NewAndroidProject.png?version=1&modificationDate=1314692113000

Click Finish. Next, go to Project|Properties|Build Path|Libraries and add these external libraries to your android project.

AndroidLibraries.png?version=1&modificationDate=1314692381000

You final project view should look like this

ProjectExplorerB.png?version=1&modificationDate=1314692714000

To run the application in the emulator, we need an Android Virtual Device (AVD). Go to Window|Android SDK and AVD Manager and create a new AVD like this

CreateAVD+.png?version=1&modificationDate=1314693103000

Now go to Run|Configuration to create a new run configuration for the client app.

RunConfiguration.png?version=1&modificationDate=1314693302000

Now you should be able to launch the application in the debugger. Right click on the javaee-tutorial-jaxrs-android project and select Debug As|Android Application. This should launch the emulator, which now goes though a series of boot screens until it eventually displays the Android home screen. This will take a minute or two if you do this for the first time.

2_2_HVGA_Initial.png?version=1&modificationDate=1314693984000

2_2_HVGA_Next.png?version=1&modificationDate=1314693991000

2_2_HVGA_Final.png?version=1&modificationDate=1314694001000

When you unlock the home screen by dragging the little green lock to the right. You should see the the running JAX-RS client application.

NoBooks.png?version=1&modificationDate=1314694139000

Finally, you need to configure the host that the client app connects to. This would be the same as you used above to curl the library list. In the emulator click Menu|Host Settings and enter the host address of your OpenShift application.

HostSettings.png?version=1&modificationDate=1314694408000

When going back to the application using the little back arrow next to Menu, you should see a list of books.

ListOfBooks.png?version=1&modificationDate=1314694485000

You can now add, edit and delete books and switch between your browser and the emulator to verify that the client app is not cheating and that the books are in fact in the cloud on your JBossAS 7 instance.

In Eclipse you can go to the Debug perspective and click on the little Android robot in the lower right corner. This will display the LogCat view, which should display log output from that Android system as well as from this client app

Exploring the mobile client

There is a lot to writing high quality mobile applications. The goal of this little application is to get you started with JBossAS 7 / Android integration. There is also a portable approach to writing mobile applications. A popular one would be through PhoneGap. With PhoneGap you write your application in HTML+CSS+Java Script. It then runs in the browser of your mobile device. Naturally, not the full set of mobile platform APIs would be available through this approach.

The JAX-RS client application uses an annotated library client interface

There are two implementations of this interface available.

LibraryHttpclient

LibraryResteasyClient

The first uses APIs that are available in the Android SDK natively. The code is much more involved, but there would be no need to add external libraries (i.e. resteasy, jackson, etc). The effect is that the total size of the application is considerably smaller in size (i.e. 40k)

The second implementation uses the fabulous RESTEasy client proxy to interact with the JAX-RS endpoint. The details of Http connectivity and JSON data binding is transparently handled by RESTEasy. The total size of the application is considerably bigger in size (i.e. 400k)

Stay tuned for an update on a much more optimized version of the RESTEasy mobile client. Feasible is also a RESTEasy JavaScript library that would enable the portable PhoneGap approach.

java官方 jax rs_jboss7 Java API for RESTful Web Services (JAX-RS) 官方文档相关推荐

  1. JAX-RS(Java API for RESTful Web Services)常用注解

    为什么80%的码农都做不了架构师?>>>    概述 JAX-RS(Java API for RESTful Web Services)是Java 提供用于开发RESTful Web ...

  2. jboss7 Java API for RESTful Web Services (JAX-RS) 官方文档

    原文:https://docs.jboss.org/author/display/AS7/Java+API+for+RESTful+Web+Services+(JAX-RS) Content Tuto ...

  3. 【Java 官方API】在哪里看JDK11官方文档

    Java® Platform, Standard Edition & Java Development Kit Version 11 API Specification https://doc ...

  4. 深入理解Java 8 Lambda表达式(Oracle官方文档版)

    Java 8 问世三年了,9马上也要问世了,所以,嗯,我要开始学8了-- 官方文档:http://docs.oracle.com/javase/tutorial/java/javaOO/lambdae ...

  5. Spring 4 官方文档学习 Spring与Java EE技术的集成

    本部分覆盖了以下内容: Chapter 28, Remoting and web services using Spring -- 使用Spring进行远程和web服务 Chapter 29, Ent ...

  6. 【Java基础】2020如何查看Java官方文档

    一.首先百度搜索oracle进入oracle公司官网(注意是官网). 二.点击首页最下角的developers(开发者). 三.点击开发者页面中间部分的technologies(技术),点击java. ...

  7. 查看和学习Java官方文档

    目录 为什么要学习Java官方文档? 去哪里找Java官方文档? 怎么下载Java官方文档? 怎么查阅和学习Java文档? 为什么要学习Java官方文档? 主要原因是Java体系十分庞大, 市面上的书 ...

  8. Java官方文档索引

    之前一直感觉Java的官方文档有些杂乱,最近特意整理了一下,仅供参考. 入口 Oracle官方文档入口:http://docs.oracle.com/.下级页面这边只整理了JavaEE跟JavaSE的 ...

  9. ElasticSearch Java High level Rest Client 官方文档中文翻译(一)

    ElasticSearch Java High level Rest Client 官方文档中文翻译 一 纯粹记录自己在看官网的es rest high level api 时的翻译笔记,可以对照着官 ...

最新文章

  1. 解决gradle下载慢的问题
  2. 使用代码创建ABAP transparent table
  3. MFC 双击控件 提示重载函数已存在
  4. Android之Content和activity、service、Application关系和attachBaseContext函数调用的时候
  5. 【转】GitHub 从单机到联机:玩转 Pull Request
  6. Linux sed命令高级用法精讲
  7. html cellpadding属性,HTML table标签 cellpadding 属性
  8. [考试]20151008
  9. python卸载_技术 | Python 包安装和卸载的几种方式
  10. 世界人工智能大赛 Top1 方案!手写体 OCR 识别
  11. HTML+CSS(PC端+移动端)
  12. 五邑大学、广东工业大学教务系统一键评教代码分享及技术简易剖析
  13. pyspark groupby 后将遍历的每一行转成pandas df
  14. 攻防世界-MISC-练习区-06(坚持60s)
  15. 2014年多益网络春季校园招聘机试题
  16. 神经网络原理与实例精解,神经网络计算工作原理
  17. Vue启动报错This is probably not a problem with npm. There is likely additional logging output above.
  18. Java内存之本地内存分析神器: NMT 和 pmap
  19. 总结篇:系统及应用监控的综合思路
  20. 【正点原子MP157连载】第十九章 Buildroot根文件系统构建-摘自【正点原子】STM32MP1嵌入式Linux驱动开发指南V1.7

热门文章

  1. 2016年10月计算机网络技术,2016年10月自考计算机网络技术练习题及答案(2)
  2. html 拍照旋转了90度_华为Mate X2概念图:可旋转正反三屏幕,单颗镜头在转轴上...
  3. vs中html如何设计分页_如何在电路设计中使用负载线(Load Line)?
  4. python调用shell该引用到什么类_python调用shell, shell 引用python
  5. ACL20 | 让笨重的BERT问答匹配模型变快!
  6. 以太网和路由设置,内网和外网同时上
  7. Android官方开发文档Training系列课程中文版:使用Fragment构建动态UI之Fragment创建
  8. 《Science》日本科学家利用干细胞诱导成功了大鼠生殖细胞
  9. 牛津大学最新调研:AI面临基准危机,NLP集中“攻关”推理测试
  10. 函数式编程让你忘记设计模式