JAX RS – 23 – javax.ws.rs.core.Response

Merhaba Arkadaslar
Bu bolumde javax.ws.rs.core.Response sinifini inceleyecegiz.

javax.ws.rs.core.Response abstract sinifinda dikkatimizi cekecek su 3 metot yer almaktadir.

public abstract class Response {
	public abstract Object getEntity();
	public abstract int getStatus();
	public abstract MultivaluedMap<String, Object> getMetadata();
	...
}

javax.ws.rs.core.Response abstract siniftir direkt objesini olusturamayiz.
javax.ws.rs.core.Response.ResponseBuilder abstract sinifinda static yardimci metotlar yardimiyla kullanacagiz.

PersonResource.java
Basit bir ornek yapalim,metodumuz geriye javax.ws.rs.core.Response tipinde donmektedir.

//imports

	@GET
	@Path("/name")
	// http://localhost:8080/injavawetrust.resteasy.tutorial/person-response/name
	public Response getPersonNameById() {
		String personName = "Levent";
		ResponseBuilder builder = Response.ok(personName);
		// add header
		builder.header("myHeader", "myHeaderValue");
		// add cookie
		builder.cookie(new NewCookie("myCookie", "myCookieValue"));
		return builder.build();

	}

Response$ResponseBuilder.class
ResponseBuilder sinifi static nested class’tir. ok() metodu icin overloaded metotlar yer almaktadir.

ResponseBuilder#header ve ResponseBuilder#cookie geriye ResponseBuilder donmektedir.
Ornek kodumuzda yeni bir header ve cookie ekledik.

public static abstract class ResponseBuilder {...

    public abstract ResponseBuilder header(String name, Object value);
    public abstract ResponseBuilder cookie(NewCookie... cookies);
    public static ResponseBuilder ok() {
        return status(Status.OK);
    }

    public static ResponseBuilder ok(Object entity) {
        ResponseBuilder b = ok();
        b.entity(entity);
        return b;
    }
....

HTTP response code’lari icin Response$Status enum kullanilmaktadir.

Response$Status.class

public enum Status {
	OK(200, "OK"),
	CREATED(201, "Created"),
	ACCEPTED(202, "Accepted"),
	NO_CONTENT(204, "No Content"),
	MOVED_PERMANENTLY(301, "Moved Permanently"),
	SEE_OTHER(303, "See Other"),
	NOT_MODIFIED(304, "Not Modified"),
	TEMPORARY_REDIRECT(307, "Temporary Redirect"),
	BAD_REQUEST(400, "Bad Request"),
	UNAUTHORIZED(401, "Unauthorized"),
	FORBIDDEN(403, "Forbidden"),
	NOT_FOUND(404, "Not Found"),
	NOT_ACCEPTABLE(406, "Not Acceptable"),
	CONFLICT(409, "Conflict"),
	GONE(410, "Gone"),
	PRECONDITION_FAILED(412, "Precondition Failed"),
	UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
	INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
	SERVICE_UNAVAILABLE(503, "Service Unavailable");
	public enum Family {
	INFORMATIONAL, SUCCESSFUL, REDIRECTION,
	CLIENT_ERROR, SERVER_ERROR, OTHER
	}
	public Family getFamily()
	public int getStatusCode()
	public static Status fromStatusCode(final int statusCode)
}

Ornegimizi calistiralim ;

http://localhost:8080/injavawetrust.jersey.tutorial/person-response/name
http://localhost:8080/injavawetrust.resteasy.tutorial/person-response/name

Dikkat edecek olursak header ve cookie’nin eklendigini gorebiliriz.

response-cookie-header

Bir baska ornek olarak ;

	@GET
	@Path("/person")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getPersonById() {
		Person person = new Person(100, "Levent", "Erguder");
		ResponseBuilder builder = Response.ok(person);
		return builder.build();
	}

Ornegimizi calistirdigimizda ;

http://localhost:8080/injavawetrust.resteasy.tutorial/person-response/person
http://localhost:8080/injavawetrust.jersey.tutorial/person-response/person

response-json

Bir baska ornek olarak ;

	@GET
	@Path("/persons")
	@Produces(MediaType.APPLICATION_JSON)
	// http://localhost:8080/injavawetrust.resteasy.tutorial/person-response/persons
	public Response getAllPersons() {

		Person person1 = new Person(100, "Levent", "Erguder");
		Person person2 = new Person(101, "James", "Gosling");
		Person person3 = new Person(102, "Joshua", "Bloch");

		List<Person> persons = Arrays.asList(person1, person2, person3);

		//GenericEntity<List<Person>> genericEntity = new GenericEntity<List<Person>>(persons) {};
		return Response.ok(persons).build();
	}	

Ornegi calistirdigimizda ;

http://localhost:8080/injavawetrust.resteasy.tutorial/person-response/persons
http://localhost:8080/injavawetrust.jersey.tutorial/person-response/persons

response-json

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

Leave a Reply

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