10 Nisan 2023 Pazartesi

Eclipse Jersey Kullanımı

 Eclipse Jersey Nedir?
Açıklaması şöyle
Jersey is a library that provides JAX-RS Reference Implementation and more. Jersey provides its own APIs that extend the JAX-RS specification with additional features and utilities to further simplify RESTful service and client development.

For a library to be described as a ‘Reference Implementation’ simply means that it’s developed by the same people who created the ‘Specification’.
Maven
Örnek
Şöyle yaparız
<project...>
...
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- Jersey 3.1.x versions support JAX-RS 3.1 -->
    <jersey.version>3.1.1</jersey.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <!-- Jersey's Bill of Material -->
      <dependency>
        <groupId>org.glassfish.jersey</groupId>
        <artifactId>jersey-bom</artifactId>
        <version>${jersey.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <!-- added for deploying on a Servlet container such as Tomcat -->
    <!-- also brings JAX-RS 3.1 API as a transitive dependency -->
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
    </dependency>

    <!-- added for Dependency Injection support in JAX-RS -->
    <dependency>
      <groupId>org.glassfish.jersey.inject</groupId>
      <artifactId>jersey-hk2</artifactId>
    </dependency>

    <!-- added for JSON Binding support using JSON-B API -->
    <!-- also brings JSON-P 2.1 API and JSON-B 3.0 API with Implementations -->
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-binding</artifactId>
    </dependency>

    <!-- added for XML Binding support using JAXB API -->
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-jaxb</artifactId>
    </dependency>

    <!-- Java API for XML Binding Implementation -->
    <!-- also brings JAXB 4.0 API as a transitive dependency -->
    <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>4.0.2</version>
    </dependency>
  </dependencies>
...
</project>
Configuring Eclipse Jersey
Açıklaması şöyle
Like with most other specifications we need to configure what’s known as the front controller servlet, which is simply a servlet that’ll accept all the requests made to a specific url pattern.

The front controller servlet will also be responsible for the initialization of the library and it’s components and through it we pass configurations to the library.

Configuring Eclipse Jersey can be done in one of two ways
1. Using web.xml file.
2. Using annotations on a jersey configuration class.
1. web.xml
Örnek
Şöyle yaparız
<?xml version="1.0" encoding="UTF-8"?>

<web-app
  xmlns="https://jakarta.ee/xml/ns/jakartaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
  version="6.0">

  <servlet>
    <servlet-name>MyFrontControllerServlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
       <param-name>jersey.config.server.provider.packages</param-name>
       <param-value>com.example.api.controllers</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>

  <servlet-mapping>
    <servlet-name>MyFrontControllerServlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>
</web-app>
Açıklaması şöyle
Note that we’re using Jakarta EE 10, that means Servlet API version 6.0, in order to use the deployment descriptor file web.xml we need to use the appropriate xml schema namespace and specify the version attribute.

Here, we’re using jersey’s front controller servlet that’s called “ServletContainer” that exists in the package “org.glassfish.jersey.servlet”.

We’re also providing our package “com.example.api.controllers” as the package that Jersey needs to look at for our JAX-RS Annotated Classes.

All requests that starts with /api/ will be intercepted and handled by Jersey
2- Using annotations on a jersey configuration class
Açıklaması şöyle
Starting with Java EE 6, annotations such as @WebServlet, @WebFilter, @WebListener were introduced which lets you define the deployment properties in the java class itself. You do not have to mention them in web.xml.

All the properties you can mention in web.xml can now be provided using @WebSerlvet annotation. So there is no need to have any deployment descriptor.


Hiç yorum yok:

Yorum Gönder

Bean Validation @GroupSequence Anotasyonu

Örnek Elimizde şöyle bir kod olsun public class SampleRequest {   @NotNull   LocalDate startDate;   @NotNull   LocalDate endDate;   @AssertT...