Skip to main content

Command Palette

Search for a command to run...

What is JAX-RS

Published
1 min read
S

I love learning about technology and sharing that with others

Overview

  • there are multiple ways to implement RESTFUL API in java we can use Spring or we can use JAX-RS
  • JAX-RS is a specification , set of interfaces and annotations offered by JAVA EE.

Lightweight Wars

Always know your Server

  1. Glassfish comes with Jersey
  2. Wildfly or jboss comes with RESTEasy.

Annotations

@GET and @POST

@Path

@PathParam

@Consumes

  • it is like an input to a method , it tells what media type the HTTP method takes in

@Produces

  • It is like an output to a method , means media type produced by the method
Note
  • It is good practice to add both for each method

Code Sample


    @GET
    @Path("/get/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getNotification(@PathParam("id") int id) {
        return Response.ok()
          .entity(new Notification(id, "john", "test notification"))
          .build();
    }

    @POST
    @Path("/post/")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response postNotification(Notification notification) {
        return Response.status(201).entity(notification).build();
    }
16 views

More from this blog

H

hashcodehub

271 posts

Consistent, Passionate and Organized :)