Introduction to Jersey the basics of Jersey entry

  • 2020-04-01 01:31:57
  • OfStack

I'm not going to discuss the details of REST here, but in general, REST is about letting the client interact with the server segment by sending and receiving display resources. It's worth noting that:

Fielding is one of the core authors of the HTTP protocol and apache web server. For 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 a JAVA developer, but the fact is that most J2EE applications, especially javaserver faces applications, do not allow for a REST style framework, so we will carefully compare the differences between javaserver faces and REST applications. JSR311 describes how to practice REST's architectural style in JAVA.

There are already some frameworks to implement JSR311, such as Jersey, which will be introduced today. Jersey is the most widely used open source framework in development and is supported by oracle.

First, use NetBeans JavaServer Faces to build an example application

This example is about an article voting system, an article that requires three entities: the article content, the author, and the vote, which are actually the resources we talked about earlier (for REST).
Note that the article content, authors, and voting entities are all mapped to classes at the code level.
In the test application, we are only in three entities generated on a CRUD (create, read, update, delete) basis function, although simple, but the base is available.
But there is a big problem with such programs:
The VIEW layer of the program can only use JavaServer Faces, while rest-style programs can use a variety of formats to display the state information of application resources, such as XML,PDF, JavaScript Object Notation (JSON), and so on.

Second, build a rest-based client

NetBeans, which helps you automate the entire setup process.
1. Right click on your project and select New > RESTful Web Services from Entity Classes
2. Select the previously created entity
3, for conversion to choose package fr. Responcia. Otns. Articleevaluator. Rest. The converter
For resource selection package fr. Responcia. Otns. Articleevaluator. Rest. The resource
4. Select Create default REST servlet adaptor in web.xml
NetBeans actually generates two sets of classes, one for mapping entity classes to XML documents, using JAXB technology.
The other group is resources, each containing all instances of a class of entities. To facilitate the operation of the client and exchange between the server and the client.
We can use the REST interface to manipulate the entities, and in the resource bundle, you can look at the classes and see how the URLS and the presentation layer for each entity are going to work
Build.
Because we realized the two in front of the author's entity instance, 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 (the picture is omitted), but you can also access a specific author at the following address:
http://localhost:8080/ArticleEvaluator/resources/authors/1/
Testing REST applications
Using a WEB browser alone cannot fully test REST applications because we also use JSON objects, which are widely used in REST applications.

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

There are two ways to test a REST application:

1. Use the testing framework provided by Jsersey
2. Use the underlying library, such as Apache Commons HttpClient, to manually process the HTTP Request.
Usually the first approach, more efficient, because it is a framework, but want to know more details you need to use method 2, you can download it at http://hc.apache.org/httpclient-3.x/
HttpClient.

Test methods typically include three:

CreateArticle ()       - generate the Article
TestResourceAsXml ()-- the format of the test XML
TestResourceAsJson ()

Compare JavaServer Faces and a REST for different types of clients:

1. Both clients can be generated using NetBeans help.
2. Both front ends use the concept of entity objects.
3, the two front-end can be used at the same time, there will be no conflict.

Here are the differences:

1. The application state of JSF is saved on the server side, the user's operation state is saved in HttpSession, while REST saves the state to the client side.
Use REST URLs to interact with resources, which is why JSF is easier to code.
2. JSF provides rich user interface components. If you need an interface in your application, it is more efficient to use JSF.
3. REST has better-looking URLs that can be bookmarked by users and indexed by search engines.
4. REST provides the possibility of displaying the same resource in multiple formats, such as XML,PDF,JDON, etc.

Related articles: