JAX RS – 15 – @BeanParam

Merhaba Arkadaslar,
Bu bolumde @BeanParam annotation’ini inceleyecegiz.

Product adinda bir POJO sinifimiz olacak , burada instance variable’lar icin @PathParam , @QueryParam , @MatrixParam gibi onceki bolumlerde kullandigimiz annotation’lari kullanacagiz.

The JAX-RS runtime will introspect the @BeanParam parameter’s type 
for injection annotations and then set them as appropriate.

Product.java

package _09.beanParam.model;

import javax.ws.rs.MatrixParam;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;

public class Product {

	@QueryParam("id")
	private int id;

	@MatrixParam("brand")
	private String brand;

        @MatrixParam("price")
	private double price;

	@PathParam("category")
	private String category;

	//getters and setters
	//toString
}

ProductResource.java

package _09.beanParam.service;

import javax.ws.rs.BeanParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import _09.beanParam.model.Product;

@Path("/product-beanParam")
public class ProductResource {

	@GET
	@Path("/getProductById/{category}")
	public String getProductById(@BeanParam Product product) {
		return "getProductById is called. <br/> " + product.toString();
	}

	@GET
	@Path("/getProductsByBrand/{category}")
	public String getProductsByBrand(@BeanParam Product product) {
		return "getProductsByBrand is called. <br/>" + product.toString();
	}

}

Kodu inceleyecek olursak metotlarda parametre olarak Product variable’i kullaniyoruz. @BeanParam annotation’ini ekledigimize dikkat edelim.
/product-beanParam/getProductById/{category} , getProductById metodu tarafindan karsilanacaktir. Burada dikkat edecek olursak ?id=100 seklinde bir query parameter ekledik. Boylece bu query parameter @QueryParam olarak deklarasyon yaptigimiz id instance variable’a inject edilecektir.{category} karsilik /computer degerini kullanabiliriz boylece @PathParam‘a karsilik gelecektir.

	@QueryParam("id")
	private int id;
        @PathParam("category") 
        private String category;

http://localhost:8080/injavawetrust.jersey.tutorial/product-beanParam/getProductById/computer?id=100
http://localhost:8080/injavawetrust.resteasy.tutorial/product-beanParam/getProductById/computer?id=100

/product-beanParam/getProductsByBrand/{category}” , getProductsByBrand metodu tarafindan karsilanacaktir. Burada ;brand=acer seklinde bir matrix parameter ekledik. Boylece bu matrix parameter @MatrixParam olarak deklarasyon yaptigimiz brand instance variable’a inject edilecektir. {category} karsilik /computer degerini kullanabiliriz boylece @PathParam‘a karsilik gelecektir.

       @MatrixParam("brand")
       private String brand;

       @MatrixParam("price")
       private double price;

       @PathParam("category") 
       private String category;
http://localhost:8080/injavawetrust.jersey.tutorial/product-beanParam/getProductsByBrand/computer;brand=acer
http://localhost:8080/injavawetrust.jersey.tutorial/product-beanParam/getProductsByBrand/computer;brand=acer;price=123.45
http://localhost:8080/injavawetrust.resteasy.tutorial/product-beanParam/getProductsByBrand/computer;brand=acer
http://localhost:8080/injavawetrust.resteasy.tutorial/product-beanParam/getProductsByBrand/computer;brand=acer;price=123.45

Bir baska ornek olarak ;

User.java

package _09.beanParam.model;

import javax.ws.rs.FormParam;

public class User {

	@FormParam("id")
	private int id;

	@FormParam("name")
	private String name;

	@FormParam("surname")
	private String surname;

	//getters and setters
	//toString
}

UserResource.java

package _09.beanParam.service;

import javax.ws.rs.BeanParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

import _09.beanParam.model.User;

@Path("/user-beanParam")
public class UserResource {

	@POST
	@Path("/addUser")
	public String addUser(@BeanParam User user) {
		return "addUser is called. " + user.toString();
	}
}

user-jersey.html
html formu projemiz disinda olusturup calistirabiliriz , form’u submit ettigimizde addUser metodu calisacaktir.
input alanlari ile User property alanlari ayni isme sahip olduklari icin otomatik olarak baglanacaktir.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>@BeanParam Example</title>
</head>
<body>
<form id="form1" action="http://localhost:8080/injavawetrust.resteasy.tutorial/user-beanParam/addUser" method="post">
    <div>              
    </div> 
    <table>
       <tr>
              <td align="right">Id :</td>
              <td><input type="text" name="id" /></td>
       </tr>

       <tr>
              <td align="right">Name :</td>
              <td><input type="text" name="name" /></td>
       </tr>
       <tr>
              <td align="right">Surname : </td>
              <td><input type="text" name="surname" /></td>
       </tr>       
        <tr>
              <td> 
              <input type="submit" value="Submit"/> 
              </td>
        </tr>
</table> 
    </form>
</body>
</html>

beanparam-form

beanparam-form-add

 

Github kaynak kodlar / source folder
injavawetrust.resteasy
injavawetrust.jersey

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *