Jersey RESTful Web Services

Source: Internet
Author: User

Jersey is a RESTful request Service Java framework, similar to the struts framework used by regular Java programming, and is primarily used to process the business logic layer. Similar to struts, it can also be integrated with the hibernate,spring framework. Because struts2+hibernate+spring integration in the market share is too high, so few people to pay attention to Jersey. So the introduction of Jersey on the internet is very few. But it really is a very good framework. For a request-only service, you may even need to give a URI to complete the operation for the Get,delete request. A simple example: if you want to get all the data in the server database, you can set the path in the browser or by using the Ajax get method, for example: localhost:8080/student (project name)/studentinfo (Project service overall prefix)/student ( endorsement for handling student objects)/getstudentinfo ( Last prefix)。 This will allow all student information to be obtained. You can choose the return type of the data obtained by Get: json,xml,text_html (String). Once obtained, you can plug this data into HTML or JSP pages via JS. Here's the explanation: the settings for Web.:<!--Define the jersey interceptor--
<servlet>
<servlet-name>JerseyServlet</servlet-name>
<servlet-class>
Com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<!--the folder where the service class is located--
<param-value>com.mirrors.action</param-value><!-- The reason I'm defined as Com.mirrors.action is that the class in this package acts like the action layer class in Struts--! >
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JerseyServlet</servlet-name>
<url-pattern>/new/*</url-pattern> <!--project Service overall prefix --
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>studentaction.java Some code: @Component
@Path ("/student") //Handling endorsement of Student objects
public class Studentaction
{
Private Studentdao Studentdao;
public void Setstudentdaostudentdao Studentdao)
{
This.studentdao =studentdao;
}
@GET //How to get
@Path ("Getstudentinfo")// Last prefix@Produces ({Mediatype.application_json}) //return type is a JSON array of student objects
Public list<student> Gettrade () {
return Studentdao.getstudent ();
}} The processing of such a Get method is finished, followed by the foreground extraction method, you can control the JSON array in the way the page is rendered by JS. Jersey a total of 4 processing methods, namely: @GET, @POST, @DELETE, @PUT.

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.