JAX RS – 35 – javax.ws.rs.core.UriInfo

Merhaba Arkadaslar
Bu bolumde javax.ws.rs.core.UriInfo interface’ini daha yakindan inceleyecegiz.
Onceki bolumlerde @Context annotation ile birlikte kullandik.

public interface UriInfo {
    public URI getRequestUri();
    public UriBuilder getRequestUriBuilder(); 
    public URI getAbsolutePath();
    public UriBuilder getAbsolutePathBuilder(); 
    public URI getBaseUri();
    public UriBuilder getBaseUriBuilder();

getRequestUri metodu absolute URI bilgisini doner , ek olarak query parametre bilgisi de yer alir.

Get the absolute request URI including any query parameters.

getAbsolutePath metodu absolute URI bilgisini doner , fakat query parametre bilgisi yer almaz.

Get the absolute path of the request. This includes everything preceding
the path (host, port etc) but excludes query parameters.

getBaseUri metodu base URI bilgisini doner.

Get the base URI of the application. URIs of root resource classes are 
all relative to this base URI.

UriInfoResource.java

package _27.uriInfo.service;

import java.net.URI;

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

@Path("/uriInfo-path")
public class UriInfoResource {

	@GET
	@Path("/test1")
	
	public String test1UriInfo(@Context UriInfo uriInfo) {

		URI requestURI = uriInfo.getRequestUri();
		URI absolutePathURI = uriInfo.getAbsolutePath();
		URI baseURI = uriInfo.getBaseUri();

		System.out.println(requestURI);
		System.out.println(absolutePathURI);
		System.out.println(baseURI);

		return "UriInfoResource#test1UriInfo";
	}
}

Ornegimizi test edelim ;

http://localhost:8080/injavawetrust.resteasy.tutorial/uriInfo-path/test1?queryParam=value1
http://localhost:8080/injavawetrust.jersey.tutorial/uriInfo-path/test1?queryParam=value1

Eclipse Console’da

http://localhost:8080/injavawetrust.resteasy.tutorial/uriInfo-path/test1?queryParam=value1
http://localhost:8080/injavawetrust.resteasy.tutorial/uriInfo-path/test1
http://localhost:8080/injavawetrust.resteasy.tutorial/
http://localhost:8080/injavawetrust.jersey.tutorial/uriInfo-path/test1?queryParam=value1
http://localhost:8080/injavawetrust.jersey.tutorial/uriInfo-path/test1
http://localhost:8080/injavawetrust.jersey.tutorial/

getRequestUri metodu query parametre bilgisiyle birlikte donmektedir.
getAbsolutePath metodu absolute path bilgisini donmektedir, query parametre bilgisi yer almaz.
getBaseUri metodu projenin base uri adresini donmektedir.

Bir baska ornek olarak , onceki bolumde isledigimiz UriBuilder objesini kullanalim.
getAbsolutePathBuilder metodu geriye UriBuilder objesi donmektedir.
queryParam metodu ile query parametresi ekleyelim.

UriInfoResource.java

package _27.uriInfo.service;

import java.net.URI;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

@Path("/uriInfo-path")
public class UriInfoResource {

///
	@GET
	@Path("/test2")
	public String test2UriInfo(@Context UriInfo uriInfo) {

		UriBuilder builder = uriInfo.getAbsolutePathBuilder();
		builder.queryParam("startIndex", 10);
		builder.queryParam("endIndex", 20);
		URI uri = builder.build();
		
		return uri.toString();
	}
	
}

http://localhost:8080/injavawetrust.resteasy.tutorial/uriInfo-path/test2
http://localhost:8080/injavawetrust.jersey.tutorial/uriInfo-path/test2

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

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published.