JAX-RS etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
JAX-RS etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

3 Mart 2020 Salı

JAX-RS @Get Anotasyonu

Giriş
Şu satırı dahil ederiz.
<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-api</artifactId>
  <version>7.0</version>
  <scope>provided</scope>
</dependency>
Örnek
Şöyle yaparız
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Path("/checks")
public class Checks{

  @GET
  @Path("list")
  @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
  public Response doGetListe() {

    Response.ResponseBuilder rb = Response.status(Response.Status.OK);
    rb.entity("...");
    return rb.build();
  }

}

10 Temmuz 2019 Çarşamba

JAX-RS UriInfo Arayüzü

Giriş
Bu arayüz 2 şekilde kullanılabilir.

1 @Context ile enjekte edilir. Şöyle yaparız.
@Context
UriInfo uriInfo;
2. Metoda imzasında parametre olarak kullanılır. Şöyle yaparız.
public Response processMessage(@Context UriInfo uriInfo) {...}
getQueryParameters metodu
Örnek
Elimizde çok fazla parametre alan bir metod olsun.
@GET
@Path("/processMessage")
@Produces({MediaType.APPLICATION_XML})
public Response processMessage(
  @QueryParam("a") String a,
  @QueryParam("b") String b,
  @QueryParam("c") String c,
  @QueryParam("d") String d,
  @QueryParam("e") String e,
  @QueryParam("f") String f,
  @QueryParam("g") String g,
  @QueryParam("h") String h,
  @QueryParam("h") String z,
)
Şöyle yaparız
public Response processMessage(@Context UriInfo uriInfo) {
  MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); 

11 Mayıs 2018 Cuma

JSR 330 @Singleton Anotasyonu

Giriş
Şu satırı dahil ederiz.
import javax.inject.Singleton;
EJB
Bu anotasyon EJB'deki aynı isimli anotasyon ile karıştırılmamalı.
import javax.ejb.Singleton;
JSR 330 ile @Inject, @Named, @Qualifier, @Scope, @Singleton anotasyonları ve Provider arayüzü geliyor. Hepsi javax.inject isim alanında.

Açıklaması şöyle.
  • Scope: Singleton
  • Annotation: @Singleton
  • Annotation full class name: javax.inject.Singleton
In this scope there is only one instance per jax-rs application. Singleton resource can be either annotated with @Singleton and its class can be registered using the instance of Application. You can also create singletons by registering singleton instances into Application.
Gerekirse PreDestroy ile kullanılır. Bu anotasyon en çok JAX-RS ile kullanılıyor.

constructor
Şöyle yaparız.
@Path("/hello")
@Singleton
public class HelloWorldService {
  ...
}

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);