22 Mart 2018 Perşembe

JAX-RS Response Sınıfı

Giriş
Şu satırı dahil ederiz.
import javax.ws.rs.core.Response;
Açıklaması şöyle
Response
Results in an entity body mapped from the entity property of the Response with the status code specified by the status property of the Response. A null return value results in a 204 status code. If the status property of the Response is not set: a 200 status code is used for a non-null entity property and a 204 status code is used if the entity property is null.
...
Methods that need to provide additional metadata with a response should return an instance of Response, the ResponseBuilder class provides a convenient way to create a Response instance using a builder pattern.
Response nesnesi static metodlar ile ResponseBuilder nesnesi döner. Açıklaması şöyle
Response objects cannot be created directly; instead, they are created from javax.ws.rs.core.Response.ResponseBuilder instances returned by one of the static helper methods of Response
build metodu
Şöyle yaparız.
Response r = r.build ();
entity metodu
Şöyle yaparız.
String data= "...";
Response r = r.entity (data);
Şöyle yaparız.
return Response.status(200).entity("OK").build();
ok metodu
ResponseBuilder nesnesi döner.
Örnek
Şöyle yaparız
StreamingOutput stream = ...;
return Response.ok(stream).build();
Örnek
Şöyle yaparız.
@GET
@Path("version")
@Produces(MediaType.APPLICATION_JSON)
public Response version() {
  return Response.ok(new Version())
    .header("Access-Control-Allow-Origin", "*")
    .build();
}
seeOther metodu
Şöyle yaparız.
return Response.seeOther("/")
               .cookie(new NewCookie("name", "Hello, world!"))
               .build();
status metodu
Şöyle yaparız.
Response r = Response.status (200);

Hiç yorum yok:

Yorum Gönder