JAX RS – 11 – @QueryParam

Merhaba Arkadaslar
Bu bolumde query parametrelerini JAX-RS ‘te inject etme yaklasimlarini inceleyecegiz.

@QueryParam

@javax.ws.rs.QueryParam annotation ile URI query parameter’larini inject edebiliriz.

The @javax.ws.rs.QueryParam annotation allows you to inject 
individual URI query parameters into your Java parameters.

Query parametreleri genel formati ;

/path?queryParam=value&queryParam2=value2&queryParam3=value3

LibraryResource.java

package _05.queryParam.service;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

@Path("/library-queryParam")
public class LibraryResource {

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

	}

	@GET
	@Path("/book-byName")
	public String getBookByName(@QueryParam("name") String name) {
		String message = "getBookByName is called. <br/>" + " Name : " + name;
		return message;

	}

	@GET
	@Path("/books")
	public String getBooks(@QueryParam("start") int start, @QueryParam("end") int end,
			@QueryParam("orderBy") List<String> orderBy) {
		String message = "getBooks is called. <br/>" + " Start : " + start + " End : " + end + " OrderBy : " + orderBy;
		return message;

	}

}

Ornegimizi calistiralim ;

http://localhost:8080/injavawetrust.jersey.tutorial/library-queryParam/book?id=10
http://localhost:8080/injavawetrust.resteasy.tutorial/library-queryParam/book?id=10

queryparam-example

getBookById metodu calisacaktir , query param olarak ?id=10 degerini veriyoruz.

http://localhost:8080/injavawetrust.jersey.tutorial/library-queryParam/book-byName?name=goriot baba
http://localhost:8080/injavawetrust.resteasy.tutorial/library-queryParam/book-byName?name=goriot baba

queryparam-example2
getBookByName metodu calisacaktir , query param olarak ?name=goriot baba degerini verdik.
Burada istegi gonderdigimizde name=goriot%20baba olarak encode edilir. Bosluk karakteri %20 olarak encode edilir.

http://localhost:8080/injavawetrust.jersey.tutorial/library-queryParam/books?start=20&end=30&orderBy=author&orderBy=year
http://localhost:8080/injavawetrust.resteasy.tutorial/library-queryParam/books?start=20&end=30&orderBy=author&orderBy=year

getBooks metodu calisacaktir , @QueryParam icin List de kullanabiliriz.

UriInfo with @Context

javax.ws.rs.core.UriInfo tipi parametre kullanarak getQueryParameters metodu uzerinden getFirst , get gibi metotlari kullanarak Query Parameter’lara ulasabiliriz. @Context annotation’i ile inject islemini gerceklestiriyoruz.

javax.ws.rs.core.Context

/**
 * This annotation is used to inject information into a class
 * field, bean property or method parameter.
 */
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Context {
}

LibraryResource.java

	@GET
	@Path("/books-uriInfo")
	// http://localhost:8080/injavawetrust.jersey.tutorial/library-queryParam/books-uriInfo?start=20&end=30&orderBy=author&orderBy=year
	public String getBooksUriInfo(@Context UriInfo uriInfo) {
		String start = uriInfo.getQueryParameters().getFirst("start");
		String end = uriInfo.getQueryParameters().getFirst("end");
		List<String> orderBy = uriInfo.getQueryParameters().get("orderBy");

		String message = "getBooksUriInfo is called. <br/>" + " Start : " + start + " End : " + end + " OrderBy : "
				+ orderBy;
		return message;

	}	
http://localhost:8080/injavawetrust.jersey.tutorial/library-queryParam/books-uriInfo?start=20&end=30&orderBy=author&orderBy=year
http://localhost:8080/injavawetrust.resteasy.tutorial/library-queryParam/books-uriInfo?start=20&end=30&orderBy=author&orderBy=year

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 *