Sunday, 21 February 2021

1.4 Java Web Services


 

JAX-RS Annotation

@Path

The @Path annotation is used to specify the URI through which a resource and an API can be accessed. Resource in this case is the REST Web service itself. @Path("/").


@Path("/message")

public class MessageReceiveService {


@Path("/birthday")

    public Response printBdayMessage(){


@GET - If service method is returning data based on client request.

@POST - If you want to store the data

@PUT -If you want to modify the data

@DELETE - If you want to delete 


@QueryParam

public String hello(@QueryParam("name1") String name, @QueryParam("age1") int age){

return "Hello ..."+name+" age="+age;

}

http://wwww.localhost:8080/appname/skeleton/helloService/hello?name1=bikesh&age1=32


@PathParam

 @Path("/helloService")

 public class HelloService{

@Path("sayHello/{name1}/{age}")

public String sayHello(@Pathparam("name1") String name, @QueryParam("age1") int age){

return "Hello ..."+name+" age="+age;

}

 }

 http://localhost:8080/webAppname/rest/helloService/sayHello/venkat/32


@MatrixParam

 It is similar to QueryParam but in URL variables will be seperated by semicoloun.

 

@FormParam

    @POST

    @Path("/add")

    public Response addUser(

        @FormParam("name") String name,

        @FormParam("age") int age) {


        return Response.status(200)

            .entity("addUser is called, name : " + name + ", age : " + age)

            .build();


    }

@Consumes annotation specifies the list of media types consumed by a particular API or class

Examples:

@Consumes("application/json")

@Consumes("application/xml")


@Produces annotation specifies the list of the media types produced by a particular API or class

Examples:

@Produces("application/json")

@Produces("application/xml")

No comments:

Post a Comment