JAX RS – 17 – @Encoded

Merhaba Arkadaslar
Bu bolumde @Encoded annotation’ini inceleyecegiz.

URI template , matrix, query ve diger form parametrelerin hepsi HTTP spect’ine gore encode edilmelidir. Varsayilan olarak JAX-RS , bu parametre degerlerini , ilgili Java tipine donusturmeden once decode edecektir.
Iste bazen raw encoded degeri kullanmak isteyebiliriz bu durumda @javax.ws.rs.Encoded annotationdan yararlanabiliriz.

URI template, matrix, query, and form parameters must all be 
encoded by the HTTP specification.
By default, JAX-RS decodes these values before converting them 
into the desired Java types. 
The @javax.ws.rs.Encoded annotation can be used on a class, method, or param. 
By default, inject @PathParam and @QueryParams are decoded.

@Encoded annotation ini otomatik olarak parametrenin degerinin decode edilmesine engel olur.

Disables automatic decoding of parameter values bound using 
@QueryParam, @PathParam, @FormParam or @ MatrixParam.

Using this annotation on a method will disable decoding for all parameters.
Using this annotation on a class will disable decoding for all parameters of all methods.

@Encoded annotation’ini ;

  • Class
  • Method
  • Parameter seviseyinde kullanabiliriz.

Yani sinif, metot ya da parametre basinda kullanabiliriz.

///
@Encoded
public class EmployeeResource {...
}
@Encoded
public String getEmployeeByNameEncoded(@QueryParam("name") String name) {...}
public String getEmployeeByNameEncoded(@Encoded @QueryParam("name") String name) {...}

[ ] ‘ ” gibi karakterler ozel karakterlerdir. Bu karakterler “%” karakterini takip eden 2 hexadecimal digit ile ifade edilir.
ASCII Encoding Reference

EmployeeResource.java

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.ws.rs.Encoded;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

@Path("/employee-encoded")
// @Encoded
public class EmployeeResource {

	@Path("/getEmployeeEncoded")
	@GET
	// http://localhost:8080/injavawetrust.resteasy.tutorial/employee-encoded/getEmployeeEncoded?name=['levent']
	// http://localhost:8080/injavawetrust.resteasy.tutorial/employee-encoded/getEmployeeEncoded?name=['öÖğıİçÇöÖğĞ']
	// Using this annotation on a method will disable decoding for all parameters.
	public String getEmployeeByNameEncoded(@Encoded @QueryParam("name") String name)
			throws UnsupportedEncodingException {

		String message = "getEmployeeByNameEncoded is called. <br/>";
		String decodedName = URLDecoder.decode(name, "UTF-8");
		return message + " decodedName :" + decodedName + "<br/>encoded : " + name;

	}

}

Ornegimizi calistiralim ;

http://localhost:8080/injavawetrust.resteasy.tutorial/employee-encoded/getEmployeeEncoded?name=['levent']
http://localhost:8080/injavawetrust.jersey.tutorial/employee-encoded/getEmployeeEncoded?name=['levent']

encoded

Bır baska encoding problemi olarak Turkce karakter kullandigimizda ortaya cikacaktir.
Ornegimizi Firefoxta calistirirsak karakterlerin bozuldugunu gorebiliriz , Chrome icin karakterler bozulmayacaktir.
Daha fazla bilgi icin ;
Servlet & JSP – 10 – Response – 01

http://localhost:8080/injavawetrust.jersey.tutorial/employee-encoded/getEmployeeEncoded?name=['öÖğıİçÇöÖğĞ']
http://localhost:8080/injavawetrust.resteasy.tutorial/employee-encoded/getEmployeeEncoded?name=['öÖğıİçÇöÖğĞ']

Bunun icin @Produces annotation’ini kullanabiliriz ve charset=UTF-8 bilgisini verebiliriz.

	@Path("/getEmployeeEncodedUTF8")
	@GET
	@Produces("text/html; charset=UTF-8")
	public String getEmployeeByNameEncodedUTF8(@Encoded @QueryParam("name") String name)
			throws UnsupportedEncodingException {
		
		String message = "getEmployeeByNameEncodedUTF8 is called. <br/>";
		String encodedName = URLDecoder.decode(name, "UTF-8");
		return message + " not encoded :" + name + "<br/>encoded : " + encodedName;

	}
http://localhost:8080/injavawetrust.jersey.tutorial/employee-encoded/getEmployeeEncodedUTF8?name=['öÖğıİçÇöÖğĞ']
http://localhost:8080/injavawetrust.resteasy.tutorial/employee-encoded/getEmployeeEncodedUTF8?name=['öÖğıİçÇöÖğĞ']

encoded-utf8

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 *