19 Nisan 2023 Çarşamba

Jakarta EE @Nullable Anotasyonu

Giriş
Eski kodlarda şu satırı dahil ederiz
import javax.annotation.Nullable;
Yeni kodlarda şu satırı dahil ederiz. 
import jakarta.annotation.Nullable;
Örnek
Şöyle yaparız
public Foo fromString(@Nullable String str) {
  ...
}
Örnek
Metodun dönüşünde kullanmak istersek sözdizimi biraz değişik. Şöyle yaparız. Burada metodu aslında java.util.Optional<T> dönüyor.
public static <T> java.util.@Nullable Optional<T> toJavaUtil(
    @Nullable Optional<T> googleOptional) {
  return googleOptional == null ? null : googleOptional.toJavaUtil();
}

jakarta.annotation Paketi

Giriş
Açıklaması şöyle
The jakarta.annotation package is a Java package used for defining standard annotations that can be used in Java applications, particularly in Jakarta EE (Enterprise Edition) applications.

This package contains a set of annotations that can be used to provide additional information about Java code, such as indicating whether a parameter can be null or not, or providing hints to the compiler to suppress certain warnings.

Some of the commonly used annotations in the jakarta.annotation package include:

Nonnull: Indicates that a parameter, field, or method return value cannot be null.
Nullable: Indicates that a parameter, field, or method return value can be null.
PostConstruct: Indicates a method to be executed after dependency injection is done by the container.
PreDestroy: Indicates a method to be executed when the container destroys an instance of a class.

The jakarta.annotation package is included in Jakarta EE and is a replacement for the similarly named javax.annotation package used in earlier versions of Java EE.

Jakarta EE @Nonnull Anotasyonu

Giriş
Eski kodlarda şu satırı dahil ederiz
import javax.annotation.Nonnull;
Yeni kodlarda şu satırı dahil ederiz. 
import jakarta.annotation.Nonnull;
İmzası şöyle
@Documented
@TypeQualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface Nonnull {
  ...
}
Açıklaması şöyle.
@Nonnull annotation documents the fact that obj must be non-null, while Objects.requireNonNull call ensures that obj is non-null at run-time.
IDE İçinde Uyarıları Gösterme
Bu anotasyon tamamen IDE içinde uyarılar göstermek için. Koşma esnasında exception fırlatmıyor. Açıklaması şöyle
Annotations are just extra information attached to the items they annotate; they don't inherently have built-in logic. If you're using tooling like Lombok or the Kotlin language, the compiler may support automatically adding logic based on the annotations, but otherwise, they don't "do anything" until you actually make an active check (for example, by running your POJO through a validator).
JetBrains Yerine Javax Anotasyonlarını Kullanmak
IntelliJ'in bu anotasyonları tanıması için şöyle yaparız
1. Settings > Inspections > Java > Probable bugs > Nullability problems > @NotNull/@Nullable problems > Configure Annotations
penceresine gidilir

2. Burada Annotation used for code generation olarak javax.annotation.Nullable ve javax.annotation.Nonnull atanır
Koşma Esnasında Exception Fırlatma
IntelliJ kullanıyorsak koşma esnasında exception fırlatması sağlanabilir
1. Settings > Build, Execution, Deployment > Compiler penceresine gidilir
2. Add runtime assertions for notnull-annotated methods and parameters seçilir. 
3. Configure annotations düğmesine tıklanır ve yukarıda tanımladığımız anotasyonların aynısı seçilir. Yani javax.annotation.Nullable ve javax.annotation.Nonnull atanır

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>com.google.code.findbugs</groupId>
  <artifactId>jsr305</artifactId>
  <version>3.0.2</version>
</dependency>
Örnek
Şöyle yaparız
@Nonnull
public Foo fromString(@Nullable String str) {
  ...
}
Örnek
Şöyle yaparız. Burada halen Objects.requireNonNull() kullanılıyor
public Integer getId(@Nonnull SomeObject obj){   
  Objects.requireNonNull(SomeObject, "SomeObject is null");
  // do some stuff
  return id;
}

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.


JAX-RS Kullanımı

JAX-RS popular Implementations
Açıklaması şöyle
Eclipse Jersey (Reference Implementation)
RestEasy
Restlet
Apache CXF
Eclipse Jersey Kullanımı yazısına bakabilirsiniz

Apache Tomcat

Apache Tomcat & JakartaEE Versions
Açıklaması şöyle
Apache Tomcat 11.0.x intended to support Jakarta EE 11
Apache Tomcat 10.1.x intended to support Jakarta EE 10
Apache Tomcat 10.0.x intended to support Jakarta EE 9
Apache Tomcat 9.0.x intended to support Jakarta EE 8

Bean Validation @GroupSequence Anotasyonu

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