Introduction to Jersey-jersey Basics _java

Source: Internet
Author: User
Tags http post netbeans
The details of rest are not discussed here, but in general rest is a way for clients to interact with server segments by sending and receiving display resources, where it is necessary to explain:

Fielding is one of the core authors of the HTTP protocol and the Apache Web server. As an example:
An HTTP GET request can receive a resource
An HTTP POST request can generate a resource.

This description, may be too low-level for Java developers, in fact, the vast majority of EE applications, especially JavaServer faces application is not allowed rest such a style framework, so we will carefully compare the back JavaServer The difference between faces and rest applications. JSR311 describes how to practice rest architectural style in Java.

There are already a few frameworks for implementing JSR311, such as the jersey to be introduced today. Jersey is now a more mature, widely used and open source framework for Oracle Support.

First, use NetBeans JavaServer Faces to build an example application

This example is a system of voting on the article, an article that needs to have the content of the article, the author, the voting three entities, is actually the resource we mentioned earlier (for rest).
The contents of the article, the author, and the voting entity are mapped into classes at the code level.
In this test application, we only build a crud (Create,read,update,delete) base function on three entities, albeit simple, but the basics are available.
But there's a big problem with this program:
The view layer of the program can only use JavaServer Faces, and restful programs can use a variety of formats to show state information about the application resources, such as Xml,pdf,javascript Object notation (JSON).

Second, establish a rest based client

NetBeans, can help you automate the entire build process.
1, right click on your project, and then proceed to the following select New > RESTful Web Services from Entity Classes
2, select the entity that was established in the previous.
3, for the conversion selection package Fr.responcia.otn.articleevaluator.rest.converter
For resource selection Package Fr.responcia.otn.articleevaluator.rest.resource
4, select Create Default REST servlet adaptor in Web.xml
NetBeans actually generates two sets of classes, a set of classes for mapping entity classes to XML documents, through the use of JAXB technology.
The other group is a resource, and each resource includes all instances of a class of entities. To facilitate the operation of the client and on the server and the client exchange.
We can use rest interfaces to manipulate entities, and in the resource package, you can see the URLs and how the display layer for each entity is performed by looking at the class.
Built.
Because we implemented the entity instance of two authors in the front, so when you visit the following address Http://localhost:8080/ArticleEvaluator/resources/authors, you
You can see the following interface:
You will see a list of two authors (images omitted), of course, you can also visit a specific author through the following address:
http://localhost:8080/ArticleEvaluator/resources/authors/1/
Testing REST Applications
Using a Web browser alone is not a comprehensive test of rest applications, because we also use JSON objects, which are widely used in rest applications.

JSON is better than pure XML format and can be accessed by JS scripts. Can be used in conjunction with jquery.

To test the rest application, there are two ways to do this:

1, using the test framework provided by Jsersey
2, use the underlying libraries, such as Apache Commons httpclient, to manually process HTTP Request.
Usually the first method is more efficient, because it is the framework, but to know more details you need to use Method 2, you can go to http://hc.apache.org/httpclient-3.x/to download
HttpClient.

The test method usually consists of three:

Createarticle ()--generating article
Testresourceasxml ()--testing the format of XML
Testresourceasjson ()

Compare different types of clients under JavaServer Faces and a REST two:

1, both types of clients can use NetBeans to help build.
2, both fronts use the concept of entity objects.
3, two front-end can be used at the same time, there will be no conflict.

Here are some of the more different places:

The 1,JSF application state is saved on the server side, the user's operating state is saved to the httpsession and rest is saving the state to the client.
Using REST URLs to interact with resources is why JSF is easier to encode.
2,JSF provides a wealth of user interface components, and if you need interfaces in your application, JSF is more efficient, which has no interface component and can only be used in conjunction with jquery.
3,rest has better-looking URLs that can be bookmarked by the user, and is also handy for search engines to collect.
  4,rest provides the possibility of displaying multiple formats for the same resource, such as Xml,pdf,jdon.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.