@BeanParam

The @javax.ws.rs.BeanParam annotation allows you to inject all the matching request parameters into a single bean object. The @BeanParam annotation can be set on a class field, a resource class bean property (the getter method for accessing the attribute), or a method parameter. The bean class can have fields or properties annotated with one of the request parameter annotations, namely @PathParam, @QueryParam, @MatrixParam, @HeaderParam, @CookieParam, or @FormParam. Apart from the request parameter annotations, the bean can have the @Context annotation if there is a need.

Consider the example that we discussed for @FormParam. The createDepartment() method that we used in that example has two parameters annotated with @FormParam:

public void createDepartment( 
  @FormParam("departmentId") short departmentId,  
   @FormParam("departmentName") String departmentName)   

Let's see how we can use @BeanParam for the preceding method to give a more logical and meaningful signature by grouping all the related fields into an aggregator class, thereby avoiding too many parameters in the method signature.

The DepartmentBean class that we use for this example is as follows:

public class DepartmentBean { 
 
  @FormParam("departmentId")  
    private short departmentId; 
 
  @FormParam("departmentName")  
    private String departmentName; 
 
  //getter and setter for the above fields  
  //are not shown here to save space 
} 

The following code snippet demonstrates the use of the @BeanParam annotation to inject the DepartmentBean instance that contains all the FormParam values extracted from the request message body:

@POST 
public void createDepartment(@BeanParam DepartmentBean deptBean) 
{ 
  createDepartmentEntity(deptBean.getDepartmentId(), 
    deptBean.getDepartmentName()); 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.83.176