Jersey @BeanParam webservices Restful JAX-RS tutorial


            The annotation that may be used to inject custom JAX-RS "parameter aggregator" value object into a resource class field, property or resource method parameter.
            
            The JAX-RS runtime will instantiate the object and inject all it's fields and properties annotated with either one of the @XxxParam annotation (@PathParam, @FormParam ...) or the @Context annotation. For the POJO classes same instantiation and injection rules apply as in case of instantiation and injection of request-scoped root resource classes.

Example

package com.candidjava.webservices.controller;

import javax.ws.rs.BeanParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.candidjava.webservices.bean.User;

@Path("/User")
public class RegisterProcess
{
	
	@POST
	@Path("/RegisterProcess")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getRegisterDetails(@BeanParam User user)
	{		
		
		String username = user.getUsername();
		String password = user.getPassword();
		
		System.out.println(username);
		System.out.println(password);
		
		return Response.ok().status(200).entity(user).build();
	}	
}

User Bean
package com.candidjava.webservices.bean;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.ws.rs.FormParam;

public class User
{
	@NotNull
	@Min(value=3)
	@FormParam("txt_username")
	private String username;
	
	@FormParam("txt_password")
	private String password;

	public String getUsername()
	{
		return username;
	}
	public void setUsername(String username)
	{
		this.username = username;
	}
	public String getPassword()
	{
		return password;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}
}

Screenshot

Download
        Beanparam jersey example war
        Beanparam jersey example zip



Related Post

Comments


©candidjava.com