JAX RS – 16 – @DefaultValue

Merhaba Arkadaslar
Bu bolumde @DefaultValue annotation’ini inceleyecegiz.
@DefaultValue annotation da belirtilen deger , ilgili parametre gonderilmediginde kullanilacaktir.
@QueryParam , @MatrixParam gibi annotationlari dusundugumuzde eger ilgili parametreler gonderilmemisse bu durumda @DefaultValue degeri kullanilacaktir.

@DefaultValue is a parameter annotation that can be combined with any of the other
@*Param annotations to define a default value when the HTTP request item does not exist.

CarResource.java

package _10.defaultValue.service;

import java.util.List;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

@Path("/car-defaultValue")
public class CarResource {

  @GET
  @Path("/carById")
  public String getCarById(@DefaultValue("100") @QueryParam("id") int id) {
    String message = "getBookById is called. <br/>" + " Id : " + id;
    return message;

  }

  @GET
  @Path("/carByMatrixParam")
  public String getCarByMatrixParam(@DefaultValue("default value") @MatrixParam("brand") String brand,
      @DefaultValue("default color") @MatrixParam("color") List<String> colors) {
    String message = "getCarWithMatrixParam is called.<br/>" + "Brand : " + brand + " Colors : " + colors;
    return message;

  }
}

@QueryParam icin default/varsayilan bir value/deger belirtebiliriz. Eger ilgili query parameter bilgisi yoksa bu durumda @DefaultValue ile belirttigimiz bilgi kullanilacaktir.

http://localhost:8080/injavawetrust.jersey.tutorial/car-defaultValue/carById
http://localhost:8080/injavawetrust.resteasy.tutorial/car-defaultValue/carById

Benzer sekilde @MatriParam icin de varsayilan degerleri belirtebiliriz.

http://localhost:8080/injavawetrust.jersey.tutorial/car-defaultValue/carByMatrixParam
http://localhost:8080/injavawetrust.jersey.tutorial/car-defaultValue/carByMatrixParam

Su annotationlar icin @DefaulValue annotation’ini kullanabiliriz.

	 javax.ws.rs.PathParam
	 javax.ws.rs.QueryParam
	 javax.ws.rs.MatrixParam
	 javax.ws.rs.CookieParam
	 javax.ws.rs.FormParam
	 javax.ws.rs.HeaderParam
	 

default-value

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 *