java官方 jax rs_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

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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/488458.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

研究揭示大脑在工作记忆中存储信息的神经机制

来源:中国科学院脑科学与智能技术卓越创新中心(神经科学研究所)3月5日,《神经元》期刊在线发表了题为《无颗粒岛叶皮层瞬时性神经元活动调控学习新任务时的工作记忆存储》的研究论文。该研究由中国科学院脑科学与智能技术卓越创新…

[Jmeter] 基本使用的总结

转载于:https://www.cnblogs.com/mytianying/p/6793461.html

java 仿qq登录界面7.1_安卓开发学习笔记(七):仿写腾讯QQ登录注册界面

这段代码的关键主要是在我们的相对布局以及线性布局上面,我们首先在总体布局里设置为线性布局,然后再在里面设置为相对布局,这是一个十分常见的XML布局模式。废话不多说,直接上代码:一.activity.xml>android:layout…

【前沿科技】云计算军事运用有啥特点

来源: 军语开源情报研究所云计算技术被视为继大型计算机、个人计算机、互联网之后的第四次信息技术产业革命。云计算是一种围绕分布式共享计算资源的创新应用模式,资源提供者可以方便而快速地提供计算资源,而无处不在的资源需求者可以便利地使…

js原型和原型链_JS 构造函数与原型链

JavaScript 对象体系是基于构造函数和原型链的。继承不通过类,而是通过原型对象实现,原型对象的所有属性和方法,都能被实例对象共享。构造函数(constructor)在 JS 中想要生成可重用、可继承的对象就要使用构造函数&…

全球制造业的未来

来源:航空简报2020年3月4日,Brahima Coulibaly和Karim Foda在美国布鲁金斯学会官网刊文,分析了全球制造业的未来,提出了几个鲜明的观点:1.“比较优势”将发生转移,中等收入国家尤其是许多亚洲新兴经济体&am…

mybatis 批量修改_解放双手,不写SQL!一个开源 MyBatis 神器!!

什么是通用 Mapper?它是一个可以方便的使用 Mybatis 进行单表的增删改查优秀开源产品。它使用拦截器来实现具体的执行 Sql,完全使用原生的 Mybatis 进行操作。在 Github 上标星 9.6K!为什么要用 Mapper?它提供了所有单表的基本增删…

论文速读:AI能从人类的愚蠢中学到什么?

来源:混沌巡洋舰本文来自对下面论文的编译和解读:导读:随着机器在某些认知问题上超越人类,人机协作将会带来越来越显著的影响。造成人类偏见的三个主要原因(小而不完整的数据集,从自己的决策结果中学习&…

struts2的核心和工作原理

在学习struts2之前,首先我们要明确使用struts2的目的是什么?它能给我们带来什么样的优点? 设计目标 Struts设计的第一目标就是使MVC模式应用于web程序设计。在这儿MVC模式的优点就不在提了。技术优势 Struts2有双方面的技术优势,一…

mysql允许root远程连接_西部数码使用指南:远程桌面之终端服务器超出了最大允许连接数解决...

版权归西部数码所有,原文链接:https://www.west.cn/faq/list.asp?unid739出现这种情况的原因和解决办法。 原因:用远程桌面链接登录到终端服务器时经常会遇到“终端服务器超出最大允许链接数”诸如此类错误导致无法正常登录终端服务器,引起该…

关于征集2020重大科学问题和工程技术难题的通知

来源:中国指挥与控制学会学会全体会员:为研判未来科技发展趋势、前瞻谋划和布局前沿科技领域与方向,瞄准世界科技前沿,推进世界科技强国建设,根据《中国科协办公厅关于征集2020重大科学问题和工程技术难题的通知》精神…

怎么在别人网站注入js脚本_别人的网站是怎么实现引流的?这些站外SEO技巧是关键...

点击上方蓝字关注我们!因为分享,我们相遇在SEO路上“网站上线一段时间了,为什么没有流量?为什么没有询盘?”对于做网络营销的企业而言,网站流量与询盘是建立网站的根本目的,可是为什么操作了一段…

比尔盖茨NEJM发文:新冠肺炎是百年一遇的流行病!全世界应该如何应对?

来源:生物谷面对任何危机,政府都有两个同等重要的责任:解决眼前的问题,并防止它再次发生。COVID-19大流行就是一个恰当的例子。我们现在需要拯救生命,同时也需要改善我们应对疫情的方式。第一点更为紧迫,但…

java实现上传图片代码_Java图片上传实现代码

本文实例为大家分享了java图片上传代码,供大家参考,具体内容如下import java.io.*;import java.net.*;/**发送端*/class picsend{public static void main(String[] args) throws Exception{if(args.length!1){System.out.println("请选择一张.jpg图…

中国数学相比与西方数学为什么会处于劣势?

来源:数学职业家虽然中国人更习惯【中国数学相比与西方数学为什么会处于劣势?】的视角,但私以为问【西欧数学为何可以独步天下】更合适。因为曾经辉煌过的阿拉伯数学、印度数学都落寞了。也没有其他任何地区的文明能达成西欧的成就。另外&…

java的地位和优势,Java语言之所以能持续占领霸主地位 这些优势功不可没

java作为一个真正面向对象语言,驰骋IT界二十余载,一直独占编程语言排行榜榜首,成为广泛使用的开发编程语言,为什么java就能够持续占领霸主地位呢?有哪些必然的优势呢?这首要的优势就是:既然是真…

WebBrowser,挖坑,跳坑,填坑

最近在 C# Asp.net 平台上的一个项目中用到了 WebBrowser 控件。自然而然就进入了 一连串的坑了。用网络上一同行的话“用WebBrowse,就是在给自己挖坑”。 道术太浅,这个坑我还是跳了。 需求:截取网页中的一部分,生成图片。 咣当咣…

你可能会错过的3个重要AI趋势

来源:雷锋网以下3个趋势,目前可能尚未引起注意,但长期来看会产生重大影响。根据Gartner的一项调查,到2020年底,全球48%的CIO将部署AI。尽管人们对AI和ML持乐观态度,但我仍然持怀疑态度。在可以预…

线性代数知识点总结_线性代数导读+笔记

一些学习线性代数的心得和资源分享,供大家参考。资源Introduction to Linear Algebra, 5th Edition​math.mit.edu学线性代数主要的参考书,Strang 教授也算是网红了,讲课讲得十分浅显易懂,网上有配套的video,强烈推荐。…

5G通信网络专题报告:新一轮移动通信网络建设迎来高潮

报告来源:山西证券1. 移动通信网络概述1.1 移动通信网络行业界定异地间人与人、人与物、物与物进行信息的传递和交换称为通信。通信以获取信息为目的,实现信息传输所需的一切设备和传输媒介构成通信系统,通信系统大体包括终端和通信网络两部分…