REST Web Service JAX-RS @Encoded Annotation Example with RESTEasy 3

By Arvind Rai, March 12, 2015
In this page, we will learn JAX-RS @Encoded Annotation example with RESTEasy 3. The JAX-RS method parameters by default decode the values obtained from the request. @Encoded helps to keep values encoded if we want to decode ourselves. @Encoded can be applied at class level, method level and parameter level.
@Encoded can be annotated at
1. Class Level. We achieve that parameters of all the JAX-RS method of our service class do not decode value automatically.
@Path("/manage" )
@Encoded
public class EmployeeService {} 

2. Method Level. We achieve that each and every parameter of the method will not decode values automaticaaly.
@Encoded
public Response methodTwo(@PathParam("id") String id, @QueryParam("company") String company) {} 

3. Parameter Level.
public Response methodOne(@PathParam("id") String id, 
                 @QueryParam("company") @Encoded String company) {} 
The parameter value will not automatically be decoded.

Create Service

For @Encoded example, we are creating two methods in our service class, one for argument level and second for method level.
EmployeeService.java
package com.concretepage;
import javax.ws.rs.Encoded;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
@Path("/manage" )
public class EmployeeService {
    @GET
    @Path("/one/{id}")
    @Produces("application/json")
    public Response methodOne(@PathParam("id") String id, 
	                             @QueryParam("company") @Encoded String company) {
    	System.out.println("id:"+ id+", company:"+ company);
        return Response.ok("OK").build();
    }
    @GET
    @Path("/two/{id}")
    @Produces("application/json")
    @Encoded
    public Response methodTwo(@PathParam("id") String id, 
	                                  @QueryParam("company") String company) {
    	System.out.println("id:"+ id+", company:"+ company);
        return Response.ok("OK").build();
    }
} 

Output

To the check the output deploy the project in tomcat downloading source code from link given in bottom and access the URL ashttp://localhost:8080/resteasyservice-1/employee/manage/one/ab%5Ec?company=ab^c
This URL will hit method named as one(). In this method there are two arguments, one is without @Encoded and second is with @Encoded. In the URL, we are sending (^) special character, the parameter with @Encoded will keep values as encoded. Find the output in console.
id:ab^c, company:ab%5Ec 
Now find second URL http://localhost:8080/resteasyservice-1/employee/manage/two/ab%5Ec?company=ab^c
Find the output in console.
id:ab%5Ec, company:ab%5Ec 
Both values are in encoded state because we have used @Encoded at method level, so each and every parameter of the method will not automatically be decoded.

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us