Get all updates of Java4s
Follow Us: Email Newsletter

RESTful Web Services (JAX-RS) Annotations

This tutorial explains important annotations of JAX-RS for creating RESTful web services, friends i am giving these annotations just for your understanding purpose. you better know about these annotations before we go forward with the remaining RESTful web services tutorials.

JAX-RS Annotations

  • @Path(‘Path‘)
  • @GET
  • @POST
  • @PUT
  • @DELETE
  • @Produces(MediaType.TEXT_PLAIN [, more-types])
  • @Consumes(type[, more-types])
  • @PathParam()
  • @QueryParam()
  • @MatrixParam()
  • @FormParam()

@Path() Annotation

  • Its a Class & Method level of annotation
  • This will check the path next to the base URL

Syntax :
Base URL :
http://localhost:(port)/<YourApplicationName>/<UrlPattern In Web.xml>/<path>
Here <path> is the part of URI, and this will be identified by @path annotation at class/method level, you will be able to understand in the next RESTful  hello world tutorial.

@GET

Its a method level of annotation, this annotation indicates that the following method should respond to the HTTP GET request only,  i mean if we annotate our method with @GET, the execution flow will enter that following method if we send GET request from the client

@POST

Its a method level of annotation, this annotation indicates that the following method should respond to the HTTP POST request only.

@PUT

Its a method level of annotation, this annotation indicates that the following method should respond to the HTTP PUT request only.

@DELETE

Its a method level of annotation, this annotation indicates that the following method should respond to the HTTP DELETE request only.

@Produces

Its a method or field level annotation, This tells which MIME type is delivered by the method annotated with @GET.  I mean when ever we send a HTTP GET request to our RESTful service, it will invokes particular method and produces the output in different formats.  There you can specifies in what are all formats (MIME) your method can produce the output, by using @produces annotation.
Remember: We will use @Produces annotation for GET requests only.

@Consumes

This is a class and method level annotation, this will define which MIME type is consumed by the particular method. I mean in which format the method can accept the input from the client.

Will discuss later regarding @PathParam, @QueryParam, @MatrixParam, @FormParam annotations :-) , i will talk more about these annotations in the next examples.

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

5 Responses to “RESTful Web Services (JAX-RS) Annotations”
  1. surya says:

    Thanks nice work…:)

  2. Vijay says:

    Why @Produce Annotation only for “Get” Requests…..

  3. Raja says:

    Nice Work… Clear Explanation for RESTful Web Services…

  4. Raja says:

    We can use the @Produces for POST & PUT also…

  5. Shubham Rathore says:

    @Produces will generate specific data like JSON,XML.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.