<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ravi Yasas</title>
    <description>The latest articles on DEV Community by Ravi Yasas (@raviyasas).</description>
    <link>https://dev.to/raviyasas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F372931%2F4b0df3c6-4ebb-42b3-9898-079482bf1cf9.png</url>
      <title>DEV Community: Ravi Yasas</title>
      <link>https://dev.to/raviyasas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raviyasas"/>
    <language>en</language>
    <item>
      <title>NestJS Best Practices for Developers</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Fri, 28 Mar 2025 04:01:41 +0000</pubDate>
      <link>https://dev.to/raviyasas/nestjs-best-practices-for-developers-1670</link>
      <guid>https://dev.to/raviyasas/nestjs-best-practices-for-developers-1670</guid>
      <description>&lt;p&gt;My &lt;a href="https://medium.com/@raviyasas/spring-boot-best-practices-for-developers-3f3bdffa0090" rel="noopener noreferrer"&gt;Spring Boot Best Practices for Developers&lt;/a&gt; article has become one of the most viewed in my articles. It offers valuable insights on how to effectively work with Spring Boot, covering tips, techniques, and strategies that can help developers write cleaner, more efficient code.&lt;/p&gt;

&lt;p&gt;I also tackled a trending debate in the developer community with my article &lt;a href="https://medium.com/@raviyasas/which-is-the-best-nestjs-or-spring-boot-33393612790d" rel="noopener noreferrer"&gt;Which is the Best: NestJS or Spring Boot?&lt;/a&gt; With NestJS gaining significant popularity as a backend framework, I compared it to Spring Boot, weighing the pros and cons of each, and helping developers choose the right framework based on their project requirements.&lt;/p&gt;

&lt;p&gt;In this article, I will discuss some tips when you work with the NestJS framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use the Modular architecture
&lt;/h2&gt;

&lt;p&gt;NestJS stands out for its modular architecture. It’s a great idea to structure your code into modules based on specific features or domains, such as users, authentication, or products. Each module should handle its own controllers, services, and DTOs (Data Transfer Objects). This approach helps keep your code organized, clean, and easy to maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stick to the MVC flow
&lt;/h2&gt;

&lt;p&gt;Using the MVC pattern in NestJS helps organize your application efficiently, especially for larger applications. By following this pattern, you create a clean separation of concerns and improve code organization, which makes the application more scalable and maintainable.&lt;/p&gt;

&lt;p&gt;Keep controllers focused on HTTP logic, delegate business logic to services, and use models for data structures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use DTOs properly for validations
&lt;/h2&gt;

&lt;p&gt;You can use class-validator and class-transformer to validate incoming requests. This will make your code clean. It’s recommended that DTOs be used to define request payloads and pass them through the ValidationPipe for validation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { IsString, IsInt, Min, Max, IsOptional, IsEmail } from 'class-validator';

export class CreateUserDto {
  @IsString()
  @IsNotEmpty()
  name: string;

  @IsEmail()
  @IsNotEmpty()
  email: string;

  @IsInt()
  @Min(18)
  @Max(100)
  age: number;

  @IsOptional()
  @IsString()
  @IsNotEmpty()
  address?: string;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use caching (In memory or distributed)
&lt;/h2&gt;

&lt;p&gt;For lightweight apps, you can use in-memory caching, and for large-scale projects, you can go with distributed caching like Redis. Caching will improve performance and scalability&lt;/p&gt;

&lt;p&gt;Remember to carefully manage cache invalidation to ensure your data remains accurate and up-to-date.&lt;/p&gt;

&lt;h2&gt;
  
  
  Take the advantage of Dependency Injection
&lt;/h2&gt;

&lt;p&gt;Just like Spring Boot, NestJS uses dependency inject to make your life easier with decoupling components to make it easy to test and maintain. You can use @Injectable decorator. Unnecessary injections will make circular dependencies and it causes performance issues and memory leaks. Try to avoid them using forwardRef().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from '@nestjs/common';

@Injectable()
export class UsersService {
  private readonly users = [];

  createUser(name: string, email: string) {
    const user = { name, email };
    this.users.push(user);
    return user;
  }

  getAllUsers() {
    return this.users;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Controller, Get, Post, Body } from '@nestjs/common';
import { UsersService } from './users.service';

@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Post()
  create(@Body() body: { name: string; email: string }) {
    return this.usersService.createUser(body.name, body.email);
  }

  @Get()
  findAll() {
    return this.usersService.getAllUsers();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Use an ORM framework
&lt;/h2&gt;

&lt;p&gt;TypeORM and Prisma are two common ORM frameworks that can be used with NestJS. Compared to Prisma, TypeORM is powerful since it is a fully featured ORM framework with a massive community base.&lt;/p&gt;

&lt;p&gt;Exercise caution when implementing Middleware and Interceptors&lt;br&gt;
Every middleware or interceptor introduces some processing overhead. It’s best to use them sparingly and, when possible, combine multiple interceptors into one streamlined and efficient one.&lt;/p&gt;

&lt;p&gt;Middleware runs before route handlers and has access to the request/response lifecycle. If you use middleware, you can limit the scope and handle errors.&lt;/p&gt;

&lt;p&gt;Interceptors wrap around request handling and can modify input/output. If you use interceptors, keep it lightweight. Always try to avoid overriding the response. It is a common pitfall of using interceptors.&lt;/p&gt;
&lt;h2&gt;
  
  
  Use Fastify
&lt;/h2&gt;

&lt;p&gt;Fastify is a powerful web framework that is created on top of NodeJS. If your project is a large-scale one, with a large number of concurrent requests, you can use Fastufy as the HTTP server to improve performance.&lt;/p&gt;
&lt;h2&gt;
  
  
  Use Lazy Loading for Modules
&lt;/h2&gt;

&lt;p&gt;Load modules only when needed to improve startup speed and reduce memory consumption. Here is a sample lazy load module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Module, DynamicModule } from '@nestjs/common';
import { LazyService } from './lazy.service';

@Module({})
export class LazyModule {
  static forRoot(): DynamicModule {
    return {
      module: LazyModule,
      providers: [LazyService], 
      exports: [LazyService], 
    };
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Optimize your DB queries
&lt;/h2&gt;

&lt;p&gt;Although this isn’t specific to NestJS, I feel it’s important to mention it here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Well design your database&lt;/li&gt;
&lt;li&gt;Use an ORM tool (Optional)&lt;/li&gt;
&lt;li&gt;Use indexes&lt;/li&gt;
&lt;li&gt;Use pagination&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://medium.com/@raviyasas/spring-boot-performance-upgrade-for-developers-f26ee1095d2e" rel="noopener noreferrer"&gt;Find more Database optimization tips…&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimize modules
&lt;/h2&gt;

&lt;p&gt;Remove unnecessary imports, relations, dependencies, and declarations. You can always take advantage of using the asynchronous behavior of NestJS.&lt;/p&gt;

&lt;p&gt;I hope you find this article enjoyable, and I’d love to hear your thoughts!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@raviyasas/nestjs-best-practices-for-developers-0666d95b783e" rel="noopener noreferrer"&gt;&lt;strong&gt;Find the original article&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enjoyed this article? If you found this helpful, consider supporting my work by buying me a coffee! Your support helps me create more content like this. 🚀☕&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="//buymeacoffee.com/raviyasas"&gt;Buy Me a Coffee&lt;/a&gt; ☕☕☕&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>developers</category>
      <category>backend</category>
      <category>programming</category>
    </item>
    <item>
      <title>Introduction to PostGraphile</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Wed, 21 Jul 2021 10:24:18 +0000</pubDate>
      <link>https://dev.to/raviyasas/introduction-to-postgraphile-2aa1</link>
      <guid>https://dev.to/raviyasas/introduction-to-postgraphile-2aa1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1srppgv0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfy4b3ey39rougm1vj80.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1srppgv0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfy4b3ey39rougm1vj80.png" alt="postgraphile logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What is PostGraphile?
&lt;/h1&gt;

&lt;p&gt;If you are familiar with Spring Data JPA, it will be very easy to understand PostGraphile. But never mind. Let's look into it. PostgreSQL DB is one of the very popular DB for high-performance applications. The ProstGraphile works with PostgreSQL database and GraphQL. It provides instant and high-performance GraphQL APIs from the PostgreSQL schema within seconds. It gives a lot more features.&lt;/p&gt;

&lt;h1&gt;
  
  
  Features of PostGraphile
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Stunning performance&lt;/li&gt;
&lt;li&gt;Database relations are discovered automatically and automatic CRUD mutations
&lt;em&gt;Ex: vehicleById, createVehicle, updateVehicle, deleteVehicle... etc.&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;Enable query functions like sorting, filtering, pagination... etc.&lt;/li&gt;
&lt;li&gt;Very easy to enable and use.&lt;/li&gt;
&lt;li&gt;Schema documentation is generated through the CLI&lt;/li&gt;
&lt;li&gt;You can directly call PostGraphile endpoints via your client application.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  PostGraphile without a middleware service
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zZDpP76i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2t6i3m4nejmp3z5chu5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zZDpP76i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2t6i3m4nejmp3z5chu5.png" alt="without service"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may think that we can use PostGraphile without a service. Because PostGraphile provides GraphQL APIs, then a client can directly call those APIs. Yes, this is possible, but if we need some customizations or if we need to request data from any other web services (Ex: REST), we need to have a middleware service like NestJS, Spring Boot...etc. It will look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rsg8KLMF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35xozv0h4wzz3c8rb0iz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rsg8KLMF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35xozv0h4wzz3c8rb0iz.png" alt="with service"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Install PostGraphile
&lt;/h1&gt;

&lt;p&gt;Hit this command to install PostGraphile globally.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install -g postgraphile&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  PostGraphile CLI interface
&lt;/h1&gt;

&lt;p&gt;My TestDB2 database has only one table called Vehicle. This is my DB structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zklQ9lJj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2onsg1u0p2r9tqvclya.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zklQ9lJj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2onsg1u0p2r9tqvclya.png" alt="DB structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then I run the following command to get the PostGraphile CLI interface. You can find this from the Postgraohile documentation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx postgraphile -c 'postgres://postgres:1234@localhost/TestDB2' --watch --enhance-graphiql --dynamic-json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As you can see my database is &lt;em&gt;TestDB2&lt;/em&gt;, my username is &lt;em&gt;postgres&lt;/em&gt; and the password is &lt;em&gt;1234&lt;/em&gt;. Once I hit this command in the terminal it will give a few details as mentioned below. There some other CLI options. You can find more details from the official documentation.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--COIy2wBO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/583sw8a24rmnlc8ldw0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--COIy2wBO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/583sw8a24rmnlc8ldw0h.png" alt="terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you can go to the &lt;a href="http://localhost:5000/graphiql"&gt;http://localhost:5000/graphiql&lt;/a&gt; endpoint, you will be able to the PostGraphile CLI interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Usjlmyxo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/msyvd1u9s5hxqnkeknd5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Usjlmyxo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/msyvd1u9s5hxqnkeknd5.png" alt="playground"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Explore you can see Query and also you can create Mutations too. This is very similar to the GraphQL playground. All queries and mutations are automatically generated. So you have no worries, just click on the query or mutation and hit the play button to execute the query. &lt;/p&gt;

&lt;p&gt;Please check the following video to understand the complete flow of working with PostGraphile.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/KCsQNAV1_to"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Learn more about &lt;strong&gt;GraphQL, PostGraphile, NestJS and Angular&lt;/strong&gt;, please visit &lt;a href="http://www.javafoundation.xyz/"&gt;Java Foundation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>postgraphile</category>
      <category>graphql</category>
      <category>postgres</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The story of OAuth 2.0</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Wed, 22 Jul 2020 10:20:54 +0000</pubDate>
      <link>https://dev.to/raviyasas/the-story-of-oauth-2-0-16g1</link>
      <guid>https://dev.to/raviyasas/the-story-of-oauth-2-0-16g1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DsJ-6-vJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-3jpI3JUMG50/XxfUjQ622HI/AAAAAAAAwKE/Gu3vitwlnPgFW7c31dhW3cQek1j8qu69wCLcBGAsYHQ/d/java_foundation_oauth2_logo.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DsJ-6-vJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-3jpI3JUMG50/XxfUjQ622HI/AAAAAAAAwKE/Gu3vitwlnPgFW7c31dhW3cQek1j8qu69wCLcBGAsYHQ/d/java_foundation_oauth2_logo.PNG" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a brief introduction to OAuth 2.0. You may have already heard terms like authorization, authentication, OAuth, OAuth 2.0, OpenID, OpenID Connect, JWT, SSO...etc. It is just like a mess for beginners. When it comes to security the major points are Authentication and Authorization. Before we learn about these things, let's discuss some basics.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the protocol means?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The protocol is just nothing but the official rules and regulations.&lt;/li&gt;
&lt;li&gt;It just a set of rules, just explain "how to do it" or "how to implement".&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is authorization?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This is all about permissions and roles you are granted.&lt;/li&gt;
&lt;li&gt;The authorization will never expose your identity or your credentials.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is authentication?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Simply this is all about who you are.&lt;/li&gt;
&lt;li&gt;If you are authenticated to a system, it means the system knows who you are.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Login without registration
&lt;/h2&gt;

&lt;p&gt;Can you manage hundreds of usernames and passwords? This is ridiculous. Instead of managing many usernames and passwords, now you can log in via an existing account. It can be Google, Facebook, or any other social media account or any private account.&lt;/p&gt;

&lt;p&gt;For example, just think you are going to login to Soundcloud. Look at the following process. You can log in to Soundcloud via your Google account without creating separate accounts in Soundcloud. Once you click on login, you will be able to see a window like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lXDjT7om--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-JoVp8jG42J4/XxZwrGhUuBI/AAAAAAAAwJY/tyfV59PN28kR2xSVIw9UgQdlEF9PaTjWQCLcBGAsYHQ/w321-h400/java_foundation_example.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lXDjT7om--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-JoVp8jG42J4/XxZwrGhUuBI/AAAAAAAAwJY/tyfV59PN28kR2xSVIw9UgQdlEF9PaTjWQCLcBGAsYHQ/w321-h400/java_foundation_example.PNG" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you click "Continue with Google". Then a separate window opens. You can see the URL, it is Google and asked you to enter your email and password. You are not passing your credentials to a third party.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KgmeZ-kq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-bK7xGV5UZiE/XxZwt-T_SMI/AAAAAAAAwJk/Pydx7kfXCGgHown-PSG15GbwS88JtcnlACLcBGAsYHQ/w273-h400/java_foundation_example_2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KgmeZ-kq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-bK7xGV5UZiE/XxZwt-T_SMI/AAAAAAAAwJk/Pydx7kfXCGgHown-PSG15GbwS88JtcnlACLcBGAsYHQ/w273-h400/java_foundation_example_2.PNG" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you are identified from Google, you are successfully login to Soundcloud. This is very convenient, secure, and easy. Under the hood, it is the process of OAuth2.0&lt;/p&gt;

&lt;h2&gt;
  
  
  What is OAuth2.0?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;OAuth2.0 is an authorization protocol. &lt;/li&gt;
&lt;li&gt;It explains how to delegate the authorization.&lt;/li&gt;
&lt;li&gt;This works over HTTP&lt;/li&gt;
&lt;li&gt;This can be used to authorize applications, servers, devices...etc&lt;/li&gt;
&lt;li&gt;The token-based authentication depends on OAuth 2.0
OAuth2.0 will never share your identity or credentials.&lt;/li&gt;
&lt;li&gt;Mainly this is used external applications to access any secure content.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Roles of OAuth2.0
&lt;/h2&gt;

&lt;p&gt;Basically, OAuth2.0 describes four roles. Let's compare these roles with our example&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Resource owner&lt;br&gt;
This is actually you, it means the owner of the protected resource.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Client&lt;br&gt;
This is the application that requests to access the protected resource.&lt;br&gt;
Here it is soundcloud.com&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Resource server&lt;br&gt;
This is the server that hosts protected resources.&lt;br&gt;
Here it is google.com&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authorization server&lt;br&gt;
This server issues access tokens&lt;br&gt;
It can be Google or separate authorization servers Google uses.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  OAuth2.0 authorization channels
&lt;/h2&gt;

&lt;p&gt;OAuth2.0 uses a two-way channel process to make sure of a very secure authorization process. According to the situation, we can use the front channel, back channel, or both channels.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Front channel&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is suitable for pure single-page applications that don't have a backend.&lt;/li&gt;
&lt;li&gt;This is browser-based&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Back channel&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is server-based&lt;/li&gt;
&lt;li&gt;Back channel is not visible to front-end&lt;/li&gt;
&lt;li&gt;You will be able to understand these channels later on in this post. Keep reading. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Authorization flows of OAuth2.0
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Authorization code flow (front channel + back channel)&lt;/li&gt;
&lt;li&gt;Implicit flow (front channel only)&lt;/li&gt;
&lt;li&gt;Resource owner password credentials flow (back channel only)&lt;/li&gt;
&lt;li&gt;Client credentials flow (back channel)
Mostly we are using Authorization code flow and implicit flow.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  OAuth2.0 authorization code flow
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9KYeskYu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-26TzDFbS_2U/Xxe5qwJpIlI/AAAAAAAAwJ4/8VROpFd-CZcbpyYFOMHVW3GcRQ6qeApmgCLcBGAsYHQ/d/java_foundation_oauth2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9KYeskYu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://1.bp.blogspot.com/-26TzDFbS_2U/Xxe5qwJpIlI/AAAAAAAAwJ4/8VROpFd-CZcbpyYFOMHVW3GcRQ6qeApmgCLcBGAsYHQ/d/java_foundation_oauth2.PNG" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above diagram, front channel flows are directed in green arrows and back channel flows are directed in dashed green arrows. The back channel process is not visible to the user.&lt;/p&gt;

&lt;h1&gt;
  
  
  For more info about OAuth 2.0, please visit &lt;a href="http://www.javafoundation.xyz/2020/07/the-story-of-oauth-20.html"&gt;The story of OAuth 2.0&lt;/a&gt;
&lt;/h1&gt;

</description>
      <category>oauth2</category>
      <category>java</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Builder pattern</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Fri, 29 May 2020 16:35:10 +0000</pubDate>
      <link>https://dev.to/raviyasas/builder-pattern-112p</link>
      <guid>https://dev.to/raviyasas/builder-pattern-112p</guid>
      <description>&lt;p&gt;This is another creational design pattern. Just think about the StringBuilder. If you create a StringBuilder object, you can append strings one by one to the same StringBuilder object.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("Java").append("Foundation");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This is the functionality of the builder pattern. There are a few ways to implement the builder pattern.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using constructor
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Using inner classes
&lt;/h2&gt;

&lt;p&gt;This is the best approach to implement the builder pattern.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;&lt;a href="https://www.javafoundation.xyz/2020/05/builder-design-pattern.html"&gt;--&amp;gt; Find advantages, disadvantages, usages and more details about the builder pattern&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>tutorial</category>
      <category>designpattern</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Enable Swagger2 with Spring Boot</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Sun, 10 May 2020 18:01:15 +0000</pubDate>
      <link>https://dev.to/raviyasas/enable-swagger2-with-spring-boot-4dp8</link>
      <guid>https://dev.to/raviyasas/enable-swagger2-with-spring-boot-4dp8</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CG5WFuiq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/300/1%2A2DKX6fd0wlVbbjff_noWHg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CG5WFuiq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/300/1%2A2DKX6fd0wlVbbjff_noWHg.png" alt="Swagger2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Swagger2 dependencies&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Add these dependencies to your pom.xml file&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Swagger configuration file&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now you can create the Swagger configuration file&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now you are ready. Your REST controllers are now available on Swagger 2. You can access the Swagger documentation via &lt;a href="http://localhost:8090/swagger-ui.html"&gt;http://localhost:8090/swagger-ui.html&lt;/a&gt; You may change the port with the port your application is running.&lt;/p&gt;

&lt;p&gt;You can find the complete code from my &lt;a href="https://github.com/raviyasas/swagger-demo.git"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

</description>
      <category>swagger</category>
      <category>swagger2</category>
      <category>java</category>
      <category>springboot</category>
    </item>
    <item>
      <title>Vector vs ArrayList vs LinkedList</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Sat, 25 Apr 2020 21:16:56 +0000</pubDate>
      <link>https://dev.to/raviyasas/vector-vs-arraylist-vs-linkedlist-1lb3</link>
      <guid>https://dev.to/raviyasas/vector-vs-arraylist-vs-linkedlist-1lb3</guid>
      <description>&lt;p&gt;Here I mentioned all the differences between &lt;strong&gt;Vector&lt;/strong&gt;, &lt;strong&gt;ArrayList&lt;/strong&gt;, and &lt;strong&gt;LinkedList&lt;/strong&gt;. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Vector&lt;/th&gt;
&lt;th&gt;ArrayList&lt;/th&gt;
&lt;th&gt;LinkedList&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Data structure&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Growable array&lt;/td&gt;
&lt;td&gt;Dynamic array&lt;/td&gt;
&lt;td&gt;Doubly linked list&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Increment size&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;100%&lt;/td&gt;
&lt;td&gt;50%&lt;/td&gt;
&lt;td&gt;No initial size&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Traverse&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Uses enumeration&lt;/td&gt;
&lt;td&gt;Uses iterator&lt;/td&gt;
&lt;td&gt;Uses iterator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Accessibility&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Random and fast&lt;/td&gt;
&lt;td&gt;Random and fast&lt;/td&gt;
&lt;td&gt;Sequential and slow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Order&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Insertion order&lt;/td&gt;
&lt;td&gt;Insertion order&lt;/td&gt;
&lt;td&gt;Insertion order&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Duplicates&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Insert / Delete&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Slow&lt;/td&gt;
&lt;td&gt;Slow&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Synchronized&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;Null values&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;td&gt;Allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>java</category>
      <category>interview</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Compile-time polymorphism vs Run-time polymorphism</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Sat, 25 Apr 2020 20:43:21 +0000</pubDate>
      <link>https://dev.to/raviyasas/compile-time-polymorphism-vs-run-time-polymorphism-fin</link>
      <guid>https://dev.to/raviyasas/compile-time-polymorphism-vs-run-time-polymorphism-fin</guid>
      <description>&lt;p&gt;In this post, you will understand,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is run-time polymorphism?&lt;/li&gt;
&lt;li&gt;What is compile-time polymorphism?&lt;/li&gt;
&lt;li&gt;What does it happen in the compile-time &amp;amp; run-time?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Run-time polymorphism&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You may look at the following classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Parent class
class Parent {
    public void eat(){
        System.out.println("Parent - eat()");
    }
}

//Child class
class Child extends Parent {

    @Override
    public void eat() {
        System.out.println("Child - eat()");
    }
}

public class OOPDemo {

    public static void main(String args[]){
        Parent obj = new Child();
        obj.eat();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, I have created a parent class, child class, and the main class. Then I have created a &lt;strong&gt;Child object by Parent reference&lt;/strong&gt;. Now let's check what happens in the compile-time and run-time.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Compile-time&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;In compile time it takes care of the Parent reference. &lt;/li&gt;
&lt;li&gt;Because objects are created in the run time. &lt;/li&gt;
&lt;li&gt;First, it checks is there any eat() method in Parent class. &lt;/li&gt;
&lt;li&gt;In this example Parent class has eat() method. &lt;/li&gt;
&lt;li&gt;So it doesn't give any exception in the compile-time and compilation will be a success.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Run time&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;At the run time it takes care of the Child reference and it creates the new child object.&lt;/li&gt;
&lt;li&gt;Then it checks is there any eat() method in Child class. &lt;/li&gt;
&lt;li&gt;There is an eat() method in Child also. &lt;/li&gt;
&lt;li&gt;Then this method will be called at the run time. &lt;/li&gt;
&lt;li&gt;It means object creation is happening at the run time by JVM and Run time is responsible to call which eat() method to call.&lt;/li&gt;
&lt;li&gt;That is why it is called run-time polymorphism (&lt;strong&gt;Dynamic polymorphism&lt;/strong&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Compile-time polymorphism&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal{

    public void eat(){
        System.out.println("Eat method");
    }

    public void eat(int quantity){
        System.out.println("Eat method with quantity");
    }
}

public class ReferenceDemo {
    public static void main(String args[]){
        Animal animal = new Animal();
        animal.eat();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, a single class has the same methods with two different parameters. As you know it is called &lt;strong&gt;method overloading&lt;/strong&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Compile-time&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;In compile time it checks animal.eat() method in Animal class. &lt;/li&gt;
&lt;li&gt;No arg eat() method is there in the animal class.&lt;/li&gt;
&lt;li&gt;According to the parameter list, it will call which method should be called.&lt;/li&gt;
&lt;li&gt;Then the compilation is OK.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Run time&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;At the run time it creates the same Animal type object.&lt;/li&gt;
&lt;li&gt;Then it checks the same thing that it did at the compile time.&lt;/li&gt;
&lt;li&gt;It means compile time is enough to check which method should be called.&lt;/li&gt;
&lt;li&gt;That is why it is called Compile time polymorphism (&lt;strong&gt;Static polymorphism&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Final words !&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Overriding   = run-time polymorphism      = dynamic polymorphism&lt;br&gt;
Overloading  = compile-time polymorphism  = static polymorphism&lt;/p&gt;

&lt;p&gt;More info: &lt;a href="https://www.javafoundation.xyz/2017/11/compile-time-polymorphism-vs-run-time.html"&gt;www.javafoundation.xyz&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>oop</category>
    </item>
    <item>
      <title>Java interview coding questions - Part 02</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Sat, 25 Apr 2020 20:17:55 +0000</pubDate>
      <link>https://dev.to/raviyasas/java-interview-coding-questions-part-02-1h15</link>
      <guid>https://dev.to/raviyasas/java-interview-coding-questions-part-02-1h15</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Palindrome check&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Reverse a number&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Copy an array&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Check the longest word&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Swap numbers of two integers&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Common elements of two arrays&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


</description>
      <category>java</category>
      <category>interview</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Java interview coding questions - Part 01</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Sat, 25 Apr 2020 16:20:37 +0000</pubDate>
      <link>https://dev.to/raviyasas/java-interview-coding-questions-part-01-5hdp</link>
      <guid>https://dev.to/raviyasas/java-interview-coding-questions-part-01-5hdp</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;How to count words in a String&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;How to remove duplicates in a String&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;How to reverse a String&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;How to add a List to a Map&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;How to add a List to a Set&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;How to add a List to an array&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;How to sort an array&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


</description>
      <category>java</category>
      <category>interview</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Default methods in Java 8</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Sat, 25 Apr 2020 15:25:00 +0000</pubDate>
      <link>https://dev.to/raviyasas/default-methods-in-java-8-4b96</link>
      <guid>https://dev.to/raviyasas/default-methods-in-java-8-4b96</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;The problem&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Automobile{
    void engineStart();
    void engineStop();
}

class Car implements Automobile{
    public void engineStart() { }
    public void engineStop() { }
}

class Bus implements Automobile{
    public void engineStart() { }
    public void engineStop() { }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Just think we need to add another method in the interface in the future.&lt;/li&gt;
&lt;li&gt;But if we add it, we have to change all other classes which are implemented that interface. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Default methods&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To overcome this, default methods are introduced in Java 1.8&lt;/li&gt;
&lt;li&gt;The interface segregation principle can be replaced with default methods in Java.&lt;/li&gt;
&lt;li&gt;This is the best option to change the existing interfaces without changing implemented classes.&lt;/li&gt;
&lt;li&gt;Until Java 1.7 every method inside an interface is always public and abstract.&lt;/li&gt;
&lt;li&gt;In 1.8 version default and static methods are also allowed.&lt;/li&gt;
&lt;li&gt;In 1.9 version private methods are also allowed.&lt;/li&gt;
&lt;li&gt;Default methods can be overridden in implementing class.&lt;/li&gt;
&lt;li&gt;Default methods cannot be overridden object class's methods.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The solution using Java 8 default methods&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Automobile{
    default void m1(){}
}

interface Car{
    default void m1(){}
}

class Mazda3 implements Automobile, Car{
    public void m1() { }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In such cases you have to override the default method.&lt;/li&gt;
&lt;li&gt;Otherwise Mazda3 class won't be able to find which m1( ) method needs to be called.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or else you can use it like this also using the "&lt;strong&gt;super&lt;/strong&gt;" keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Automobile{
    default void m1(){}
}

interface Car{
    default void m1(){}
}

class Mazda3 implements Automobile, Car{
    public void m1() {
        Automobile.super.m1();    
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>java8</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Lambda expressions in Java</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Fri, 24 Apr 2020 16:52:35 +0000</pubDate>
      <link>https://dev.to/raviyasas/lambda-expressions-in-java-263b</link>
      <guid>https://dev.to/raviyasas/lambda-expressions-in-java-263b</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;What is a lambda expression?&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Lambda expression is a function without any signature.&lt;/li&gt;
&lt;li&gt;It doesn't have a name, return type, and modifiers. &lt;/li&gt;
&lt;li&gt;It can have any number of arguments.&lt;/li&gt;
&lt;li&gt;For one argument lambda expressions, parenthesis are optional.&lt;/li&gt;
&lt;li&gt;Compiler can understand the data type.&lt;/li&gt;
&lt;li&gt;Curly braces are mandatory for many arguments.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Advantages of Lambda expressions&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It helps to iterate, filter, and extract data from a collection in a very efficient way.&lt;/li&gt;
&lt;li&gt;It saves a lot of code.&lt;/li&gt;
&lt;li&gt;Code reuse&lt;/li&gt;
&lt;li&gt;Reduce class files&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Functional interface&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An interface that has only one abstract method.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;@FunctionalInterface&lt;/em&gt; annotation is used to declare an interface as a functional interface. &lt;/li&gt;
&lt;li&gt;Ex: Runnable interface&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Lambda expression signature&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;(argument-list) -&amp;gt; {body}&lt;/li&gt;
&lt;li&gt;Ex:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;( ) -&amp;gt; { }        
(p1) -&amp;gt; { }        
(p1, p2) -&amp;gt; { }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Rules&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void m1(int a, int b){    
    System.out.println(a+b);
}
    //lambda expressions
( a,b ) -&amp;gt; System.out.println(a+b);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Lambda expression doesn't have a name, a modifier and a return type.&lt;/li&gt;
&lt;li&gt;If the code contains only one line. curly braces are optional.&lt;/li&gt;
&lt;li&gt;Compiler can guess the return type.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public int square(int n){
    return n*n;
}

//lambda expressions
(int n) -&amp;gt; {return n*n;}
(n) -&amp;gt; {return n*n;}
n -&amp;gt; n*n;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If only one parameter is there, parentheses are optional.&lt;/li&gt;
&lt;li&gt;Since it has only one line, curly braces are optional. &lt;/li&gt;
&lt;li&gt;Without curly braces, the return type is optional.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Valid statements&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Interf i = n -&amp;gt; n*n;
Interf i = (n) -&amp;gt; n*n;
Interf i = n -&amp;gt; {return n*n;};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Invalid statements&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Interf i = n -&amp;gt; return n*n;
Interf i = n -&amp;gt; {return n*n};
Interf i = n -&amp;gt; {return n*n;}
Interf i = n -&amp;gt; {n*n;};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Example 01&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void m1(){
    System.out.println("Hello");
}

//lambda expressions
( ) -&amp;gt; System.out.println("Hello");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Example 02&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void add(int a, int b){
    System.out.println(a+b);
}

//lambda expressions
(int a, int b) -&amp;gt; System.out.println(a+b);
(a, b) -&amp;gt; System.out.println(a+b);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Example 03&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public int square(int n){
    return n*n;
}

//lambda expressions
(int n) -&amp;gt; {return n*n;}
(n) -&amp;gt; {return n*n;}
n -&amp;gt; n*n;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>java8</category>
      <category>lambda</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Hibernate Optimistic locking and Pessimistic locking</title>
      <dc:creator>Ravi Yasas</dc:creator>
      <pubDate>Fri, 24 Apr 2020 16:23:44 +0000</pubDate>
      <link>https://dev.to/raviyasas/hibernate-optimistic-locking-and-pessimistic-locking-2396</link>
      <guid>https://dev.to/raviyasas/hibernate-optimistic-locking-and-pessimistic-locking-2396</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Optimistic locking&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When two threads are going to update the same data at the same time, conflict can happen.&lt;/li&gt;
&lt;li&gt;There are two options to handle this, &lt;strong&gt;Versioned&lt;/strong&gt; and &lt;strong&gt;Version-less&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Versioned Optimistic locking&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In this case, &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/version"&gt;@version&lt;/a&gt;&lt;/em&gt; annotation can be used.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/version"&gt;@version&lt;/a&gt;&lt;/em&gt; annotation allows Hibernate to activate the optimistic locking whenever executing an UPDATE and DELETE statements.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity@Table(name = "orders")    
public class Order {
   @Id
   private long id;

   @Version
   private int version;

   private String description;
   private String status;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Version-less Optimistic locking&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;But in this approach, it considers the entity as a whole unit.&lt;/li&gt;
&lt;li&gt;If you change two fields at the same time, then It will throw an &lt;em&gt;ObjectOptimisticLockingFailureException&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;To handle this we can use simply version-less optimistic locking.&lt;/li&gt;
&lt;li&gt;This can be achieved by using &lt;em&gt;@OptimisticzLocking&lt;/em&gt; and &lt;em&gt;@DynamicUpdate&lt;/em&gt; annotations.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
@OptimisticLocking(type = OptimisticLockType.DIRTY)
@DynamicUpdate
public class VersionlessProduct {
    @Id
    private UUID id;
    private String name;
    private int stock;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Pessimistic locking&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This can be implemented by &lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/lock"&gt;@lock&lt;/a&gt;&lt;/em&gt; annotation
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface WidgetRepository extends JPARepository&amp;lt;Widget, Long&amp;gt; {

   @Lock(LockModeType.PESSIMISTIC_WRITE)
   Widget findOne(Long id);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>hibernate</category>
      <category>beginners</category>
      <category>java</category>
      <category>jpa</category>
    </item>
  </channel>
</rss>
