Saturday 24 June 2023

Exception Handling and Validation in Spring Boot

Handling exceptions is an important part of building a robust application. This tutorial I will illustrate how to implement Exception Handling and Validation with Spring for a REST API
please follow the steps and download the code below. 



-------------------------------- UserController -------------------------------------------

package com.ramsis.restApi.controller;

import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ramsis.restApi.payloads.ApiResponse;
import com.ramsis.restApi.payloads.UserDto;
import com.ramsis.restApi.services.UserService;

@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;

@PostMapping("/")
public ResponseEntity<UserDto>addUser(@Valid @RequestBody UserDto userDto)
{
UserDto createUser=this.userService.createuser(userDto);
return new ResponseEntity<>(createUser,HttpStatus.CREATED);
}

@PutMapping("/{userId}")
public ResponseEntity<UserDto> updateUser(@Valid @RequestBody UserDto userDto , @PathVariable("userId")  Integer userId){

UserDto updateUser=this.userService.updateUser(userDto, userId);
return ResponseEntity.ok(updateUser);
}

@DeleteMapping("/{userId}")
public ResponseEntity<ApiResponse>deletedUser(@PathVariable("userId") Integer userId)
{
this.userService.deleteUser(userId);
return new ResponseEntity<ApiResponse>(new ApiResponse("user delete succsfully",String.valueOf(HttpStatus.OK.value()),true),HttpStatus.OK);

}
@GetMapping("/")
public ResponseEntity<List<UserDto>> getAllUsers()
{
return ResponseEntity.ok(this.userService.getAllUsers());
}

@GetMapping("/{userId}")
public ResponseEntity<UserDto> getAllUsers(@PathVariable("userId") Integer userId)
{
return ResponseEntity.ok(this.userService.getByuserId(userId));
}

}







--------------------------------- GlobelExceptionHandler --------------------------------

package com.ramsis.restApi.exception;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.ramsis.restApi.payloads.ApiResponse;

@RestControllerAdvice
public class GlobelExceptionHandler {
@ExceptionHandler(ResourcesNotoundExcpetion.class)
public ResponseEntity<ApiResponse> resourceNotfoiunexceptionHandler(ResourcesNotoundExcpetion ex)
{
String message=ex.getMessage();
ApiResponse apiResponse=new ApiResponse(message,String.valueOf(HttpStatus.NOT_FOUND.value()),false);
return new ResponseEntity<ApiResponse>(apiResponse,HttpStatus.NOT_FOUND);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> handleMethodArgsNotValidateExpection(MethodArgumentNotValidException ex)
{
Map<String, String> resp=new HashMap<>();
ex.getBindingResult().getAllErrors().forEach(error->{
String filedName=((FieldError)error).getField();
String messsage=error.getDefaultMessage();
resp.put(filedName, messsage);
});
return new ResponseEntity<Map<String,String>>(resp,HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(ApiException.class)
public ResponseEntity<ApiResponse> handleApiException(ApiException ex)
{
String message=ex.getMessage();
ApiResponse apiResponse=new ApiResponse(message,String.valueOf(HttpStatus.BAD_REQUEST.value()),true);
return new ResponseEntity<ApiResponse>(apiResponse,HttpStatus.BAD_REQUEST);
}

}

----------------------------------- ResourcesNotoundExcpetion -----------------------------------

package com.ramsis.restApi.exception;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ResourcesNotoundExcpetion  extends RuntimeException{

String resourceName;
String feildName;
long feildValue;
public ResourcesNotoundExcpetion(String resourceName, String feildName, long feildValue) {
super(String.format("%s not found with his name %s :%s", resourceName,feildName,feildValue));
this.resourceName = resourceName;
this.feildName = feildName;
this.feildValue = feildValue;
}
}

------------------------ ApiResponse--------------------------

package com.ramsis.restApi.payloads;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ApiResponse {
private String  meaassge;
private String  status;
private boolean success;

}

--------------------------- userDto --------------------------------

package com.ramsis.restApi.payloads;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@Setter
@Getter
public class UserDto {
private int userId;
@NotEmpty
@Size(min = 4,message ="username must be minimum 4 character")
private String name;
@Email(message = "your email id wrong")
private String email;
@NotEmpty
//@Size(min = 3,max = 15, message = "password must be minimum 3 and max 12 character")
@Pattern(regexp = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#&()–[{}]:;',?/*~$^+=<>]).{5,15}$", message = ""
+ "Password must contain at least one digit [0-9]."
+ "Password must contain at least one lowercase Latin character [a-z]."
+ "Password must contain at least one uppercase Latin character [A-Z]."
+ "Password must contain at least one special character like ! @ # & ( )."
+ "Password must contain a length of at least 5 characters and a maximum of 15 characters.")
private String password;
private String region;
@Size(min = 2,message ="department must be minimum 2 character")
private String department;


}

--------------------------------- userRepository ------------------------------------

package com.ramsis.restApi.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.ramsis.restApi.entities.User;

public interface UserRepository  extends JpaRepository<User, Integer>{
  Optional<User> findByEmail(String email);
}

---------------------------- userService --------------------------

package com.ramsis.restApi.services;

import java.util.List;

import com.ramsis.restApi.payloads.UserDto;

public interface UserService {
UserDto registerNewUser(UserDto user);
UserDto createuser(UserDto user);
UserDto updateUser(UserDto user, Integer userId);
UserDto getByuserId(Integer userId);
List<UserDto> getAllUsers();
void deleteUser(Integer userId);

}

--------------------------- userServiceImpl --------------------

package com.ramsis.restApi.services;

import java.util.List;
import java.util.stream.Collectors;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ramsis.restApi.entities.User;
import com.ramsis.restApi.exception.ResourcesNotoundExcpetion;
import com.ramsis.restApi.payloads.UserDto;
import com.ramsis.restApi.repository.UserRepository;

@Service
public class UserServiceImpl  implements UserService{
@Autowired
private ModelMapper mapper;
@Autowired
private UserRepository userRepository;

@Override
public UserDto createuser(UserDto userDto) {
User user=this.dtoToUser(userDto);
User saveUsers=this.userRepository.save(user);
return this.userToDto(saveUsers);
}

@Override
public UserDto updateUser(UserDto userDto, Integer userId) {
User user=this.userRepository.findById(userId)
.orElseThrow(()-> new ResourcesNotoundExcpetion("User","Id",userId));
user.setName(userDto.getName());
user.setEmail(userDto.getEmail());
user.setPassword(userDto.getPassword());
user.setRegion(userDto.getRegion());
user.setDepartment(userDto.getDepartment());
User updateUser=this.userRepository.save(user);
UserDto userDto1= this.userToDto(updateUser);
return userDto1;
}

@Override
public UserDto getByuserId(Integer userId) {
User user=this.userRepository.findById(userId)
.orElseThrow(()-> new ResourcesNotoundExcpetion("User","Id",userId));
return this.userToDto(user);
}

@Override
public List<UserDto> getAllUsers() {

List<User> users=this.userRepository.findAll();
List<UserDto> userDtos=users.stream().map(user->this.userToDto(user)).collect(Collectors.toList());
return userDtos;
}

@Override
public void deleteUser(Integer userId) {
User user =this.userRepository.findById(userId).orElseThrow(()-> new ResourcesNotoundExcpetion("User","Id",userId));
this.userRepository.delete(user);
}
@Override
public UserDto registerNewUser(UserDto userDto) {
User user = this.mapper.map(userDto, User.class);
//endoed the password
user.setPassword(user.getPassword());
User newUser=this.userRepository.save(user);
return this.mapper.map(newUser, UserDto.class);
}

private User dtoToUser(UserDto userDto)
{
User user=this.mapper.map(userDto, User.class);
// user.setUserId(userDto.getUserId());
// user.setName(userDto.getName());
// user.setEmail(userDto.getEmail());
// user.setDepartment(userDto.getDepartment());
// user.setPassword(userDto.getPassword());
// user.setRegion(userDto.getRegion());
return user;
}
public UserDto userToDto (User user)
{
UserDto userDto=this.mapper.map(user, UserDto.class);
// userDto.setUserId(user.getUserId());
// userDto.setName(user.getName());
// userDto.setEmail(user.getEmail());
// userDto.setDepartment(user.getDepartment());
// userDto.setPassword(user.getPassword());
// userDto.setRegion(user.getRegion());
return userDto;
}

}

------------------------- pom.xml ----------------------------

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ramsis</groupId>
<artifactId>restApiWithValidationErrorHandling</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restApiWithValidationErrorHandling</name>
<description>Demo project for Spring boot Exception Handling and validation</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>


------------------------------ application.properties -------------------------

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=cctns@123

spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql=true

logging.level.org.hibernate.type.descriptor.sql=trace

spring.jpa.hibernate.ddl-auto = update
#spring.jpa.hibernate.ddl-auto = none





Wednesday 14 June 2023

SpringBoot Pagination With Bootstrap , jquery and Ajax

If we have a large dataset and we want to present it to the user in smaller chunks, pagination is helpful solution. So in the tutorial, I am showing how to create "SpringBoot Ajax Pagination Example" use JQuery Ajax and Bootstrap to build a table solution for pagination with SpringBoot RestAPIs examples.

The Spring Data JPA provides a useful Pageable interface that provides number of methods for building pagination in the UI side. Even you can sort the data returned by the JPA query using the methods provided in this interface API.





--------------------------------- pom.xml---------------------------------


<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.roytuts</groupId>
<artifactId>springboot-jpa-pageable</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

---------------------------------- ProductRestController -------------------------------

package com.ramsis.springboot.jpa.pageable.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ramsis.springboot.jpa.pageable.dto.ProductDto;
import com.ramsis.springboot.jpa.pageable.service.ProductService;

@RestController
@CrossOrigin(origins = "*")
public class ProductRestController {

@Autowired
private ProductService productService;

@GetMapping("/product")
public Page<ProductDto> products(Pageable pageable) {
return productService.products(pageable);
}

}


-------------------------------- Product Entity------------------------

package com.ramsis.springboot.jpa.pageable.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Column
private String name;

@Column
private String prise;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPrise() {
return prise;
}

public void setPrise(String prise) {
this.prise = prise;
}

}

---------------------------------- ProductDto ----------------------------

package com.ramsis.springboot.jpa.pageable.dto;

public class ProductDto {

private Integer id;
private String name;
private String prise;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrise() {
return prise;
}
public void setPrise(String prise) {
this.prise = prise;
}

}

------------------------ EntityDtoConverter -------------------------

package com.ramsis.springboot.jpa.pageable.converter;


import com.ramsis.springboot.jpa.pageable.dto.ProductDto;

import com.ramsis.springboot.jpa.pageable.entity.Product;


public final class EntityDtoConverter {


private EntityDtoConverter() {

}


public static ProductDto entityToDto(Product product) {

ProductDto productDto = new ProductDto();


productDto.setId(product.getId());

productDto.setName(product.getName());

productDto.setPrise(product.getPrise());

return productDto;

}


public static Product dtoToEntity(ProductDto productDto) {

Product product = new Product();


product.setId(productDto.getId());

product.setName(productDto.getName());

product.setPrise(productDto.getPrise());

return product;

}


}

---------------------------- ProductService --------------------------

package com.ramsis.springboot.jpa.pageable.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ramsis.springboot.jpa.pageable.converter.EntityDtoConverter;
import com.ramsis.springboot.jpa.pageable.dto.ProductDto;
import com.ramsis.springboot.jpa.pageable.entity.Product;
import com.ramsis.springboot.jpa.pageable.repository.ProductRepository;

@Service
public class ProductService {

@Autowired
private ProductRepository productRepository;

public Page<ProductDto> products(Pageable pageable) {
Page<Product> products = productRepository.findAll(pageable);

Page<ProductDto> pages = products.map(entity -> {
ProductDto dto = EntityDtoConverter.entityToDto(entity);
return dto;
});

return pages;
}

}


------------------------------- ProductRepository -----------------------------

package com.ramsis.springboot.jpa.pageable.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.ramsis.springboot.jpa.pageable.entity.Product;

public interface ProductRepository extends JpaRepository<Product, Integer> {

}

----------------------------------- table-pagination.js ---------------------------

$(document).ready(function() {

let totalPages = 1;

function fetchNotes(startPage) {

//console.log('startPage: ' +startPage);

/**

* get data from Backend's REST API

*/

$.ajax({

type : "GET",

url : "http://localhost:8080/product",

data: {

page: startPage,

size: 10

},

success: function(response){

$('#noteTable tbody').empty();

// add table rows

$.each(response.content, (i, product) => {

let noteRow = '<tr>' +

'<td>' + product.id + '</td>' +

'<td>' + product.name + '</td>' +

'<td>' + product.prise + '</td>' +

'</tr>';

$('#noteTable tbody').append(noteRow);

});

if ($('ul.pagination li').length - 2 != response.totalPages){

// build pagination list at the first time loading

$('ul.pagination').empty();

buildPagination(response);

}

},

error : function(e) {

alert("ERROR: ", e);

console.log("ERROR: ", e);

}

});

}

function buildPagination(response) {

totalPages = response.totalPages;

var pageNumber = response.pageable.pageNumber;

var numLinks = 10;

// print 'previous' link only if not on page one

var first = '';

var prev = '';

if (pageNumber > 0) {

if(pageNumber !== 0) {

first = '<li class="page-item"><a class="page-link">« First</a></li>';

}

prev = '<li class="page-item"><a class="page-link">‹ Prev</a></li>';

} else {

prev = ''; // on the page one, don't show 'previous' link

first = ''; // nor 'first page' link

}

// print 'next' link only if not on the last page

var next = '';

var last = '';

if (pageNumber < totalPages) {

if(pageNumber !== totalPages - 1) {

next = '<li class="page-item"><a class="page-link">Next ›</a></li>';

last = '<li class="page-item"><a class="page-link">Last »</a></li>';

}

} else {

next = ''; // on the last page, don't show 'next' link

last = ''; // nor 'last page' link

}

var start = pageNumber - (pageNumber % numLinks) + 1;

var end = start + numLinks - 1;

end = Math.min(totalPages, end);

var pagingLink = '';

for (var i = start; i <= end; i++) {

if (i == pageNumber + 1) {

pagingLink += '<li class="page-item active"><a class="page-link"> ' + i + ' </a></li>'; // no need to create a link to current page

} else {

pagingLink += '<li class="page-item"><a class="page-link"> ' + i + ' </a></li>';

}

}

// return the page navigation link

pagingLink = first + prev + pagingLink + next + last;

$("ul.pagination").append(pagingLink);

}

$(document).on("click", "ul.pagination li a", function() {

var data = $(this).attr('data');

let val = $(this).text();

console.log('val: ' + val);

// click on the NEXT tag

if(val.toUpperCase() === "« FIRST") {

let currentActive = $("li.active");

fetchNotes(0);

$("li.active").removeClass("active");

// add .active to next-pagination li

currentActive.next().addClass("active");

} else if(val.toUpperCase() === "LAST »") {

fetchNotes(totalPages - 1);

$("li.active").removeClass("active");

// add .active to next-pagination li

currentActive.next().addClass("active");

} else if(val.toUpperCase() === "NEXT ›") {

let activeValue = parseInt($("ul.pagination li.active").text());

if(activeValue < totalPages){

let currentActive = $("li.active");

startPage = activeValue;

fetchNotes(startPage);

// remove .active class for the old li tag

$("li.active").removeClass("active");

// add .active to next-pagination li

currentActive.next().addClass("active");

}

} else if(val.toUpperCase() === "‹ PREV") {

let activeValue = parseInt($("ul.pagination li.active").text());

if(activeValue > 1) {

// get the previous page

startPage = activeValue - 2;

fetchNotes(startPage);

let currentActive = $("li.active");

currentActive.removeClass("active");

// add .active to previous-pagination li

currentActive.prev().addClass("active");

}

} else {

startPage = parseInt(val - 1);

fetchNotes(startPage);

// add focus to the li tag

$("li.active").removeClass("active");

$(this).parent().addClass("active");

//$(this).addClass("active");

}

});

(function(){

// get first-page at initial time

fetchNotes(0);

})();

});


------------------------------------------------------ index.html -------------------------------------------

<!DOCTYPE html>

<html lang="en">

<head>

<title>Bootstrap Ajax SpringBoot Pagination</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">

</head>

<body>

<div class="container">

<div class="row">

<div class="col-sm-7" style="background-color: #f6e796; margin: 5px; padding: 5px; border-radius: 5px; margin: auto;">

<div class="alert alert-warning">

<h4>SpringBoot Pagination + Bootstrap + jquery + Ajax </h4>

</div>

<table id="noteTable" class="table table-hover table-sm">

<thead class="thead-dark">

<tr>

<th>Id</th>

<th>Name</th>

<th>Prise</th>

</tr>

</thead>

<tbody>

</tbody>

</table>

<ul class="pagination justify-content-center" style="margin:10px 0; cursor: pointer;">

</ul>

</div>

</div>

</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.10.2/umd/popper.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.min.js" crossorigin="anonymous"></script>

<script src="table-pagination.js"></script>

</body>

</html>






Friday 5 May 2023

How to mask and unmask input text in html






<html>
   <head>
      <style>

              .form-group{
                position: relative;
                width: 50%;
                overflow:hidden;
              }

              .form-group > .toggleMask{
                position: absolute;
                top: 0;
                right: -20px;
                text-indent: -30px;
                height:100%;
                line-height: 2;
                pointer-events: auto;
                z-index: 5;
                cursor: pointer;
              }
              .form-group > .toggleMask ~ input{
                padding-right: 30px;
              }
              .form-group > .toggleMask:checked::before{
                content:"\e105";
              }
       </style>

   <script src=https://stacksnippets.net/scripts/snippet-javascript-console.min.js?v=1 type="text/javascript"></script>
   <link href=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css rel="stylesheet"/>        
  </head>

 <body>
  <div class="form-group">
    <input type='checkbox' class="glyphicon glyphicon-eye-close toggleMask"/>
    <input type="password" id="data" class="form-control" />
    <button onclick="chackdate()"> check </button>
  </div>

   <script>
       document.querySelector('.toggleMask').addEventListener('change', onToggleMaskChange);
        function onToggleMaskChange(){
          this.nextElementSibling.type = this.checked ? 'text' : 'password'
        }                    
                             
       function chackdate(){
          var data = document.getElementById("data").value;
           alert(data);
       }
   </script>    
  </body>
</html>
--------------------------------------------------------------------------------------------------

Mask and unmask text on onmouseover event in html


<html>

  <head>
   <script>
       function mouseover() {
          let obj = document.getElementById('myPassword');
              obj.type = 'text';
       }
       function mouseout() {
          let obj = document.getElementById('myPassword');
              obj.type = 'password';
       }
   </script>

<input type="password" name="password" id="myPassword" size="30" onmouseover="mouseover();" onmouseout="mouseout();" />

</body>
</html>



Thursday 30 March 2023

How to Implement Fuzzy Search or Approximate String Search in Java

What Is Fuzzy Matching ?

Fuzzy Matching or Approximate String Matching is among the most discussed issues in IT.
It is a method that offers an improved ability to identify two elements of text, strings, or entries that are approximately similar but are not precisely the same.
In other words, a fuzzy method may be applied when an exact match is not found for phrases or sentences on a database. Indeed, this method attempts to find a match greater than the application-defined match percentage threshold.

As below mentioned code you can implement Fuzzy Search. 


import org.apache.commons.codec.language.Soundex;
import org.apache.commons.text.similarity.LevenshteinDistance;
import me.xdrop.fuzzywuzzy.FuzzySearch;

public class FuzzySearch {

   public boolean searchFuzzy(String searchableName, String targetName) {
       boolean matches=false;
       int  fuzzySearchNameThreashold=70;
        System.out.println("FuzzySearch.tokenSetRatio(searchableName, targetName) >> "+FuzzySearch.tokenSetRatio(searchableName, targetName));
        System.out.println("levenshteinDistanceMatch(searchableName, targetName) >> "+levenshteinDistanceMatch(searchableName, targetName));
       if((soundexMatch(searchableName,targetName)
        || FuzzySearch.tokenSetRatio(searchableName, targetName) >= fuzzySearchNameThreashold
        || levenshteinDistanceMatch(searchableName, targetName) >= fuzzySearchNameThreashold)) {    
              matches = true;
        }
       return matches;
    }

   public boolean soundexMatch(String searchableName, String targetName) {
         Soundex soundex = new Soundex();
         return soundex.soundex(searchableName).equals(soundex.soundex(targetName));
   }

     public double levenshteinDistanceMatch(String searchableName, String targetName) {
         LevenshteinDistance levenshteinDistance = new LevenshteinDistance();
         return (100 - Math.round((((double) levenshteinDistance.apply(searchableName, targetName) / searchableName.length()) * 100)));
       }

 public static void main(String aa[]) {
       FuzzySearchUtil fuzzySearchUtil = new FuzzySearchUtil();
       boolean flag = fuzzySearchUtil.searchFuzzy("Amn","Aman Kumar");
       System.out.println("flag >> "+flag);
 }

    }
}

----------------------- Maven Dependency -------------------------

           <dependency>
                <groupId>me.xdrop</groupId>
                <artifactId>fuzzywuzzy</artifactId>
                <version>1.3.1</version>
             </dependency>

        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
             <dependency>
                 <groupId>commons-codec</groupId>
                 <artifactId>commons-codec</artifactId>
                 <version>1.10</version>
             </dependency>

           <dependency>
              <groupId>org.apache.commons</groupId>
              <artifactId>commons-text</artifactId>
              <version>1.3</version>
           </dependency>




Saturday 26 November 2022

Create Rest APT to Encrypt and Decrypt message using AES/CBC/PKCS5PADDING algorithm

Project Structure



Calling Response from Postman tool





------------------------------------------- Controller ---------------------------------------------
package com.ramsis.encrypt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ramsis.encrypt.util.EncryptionDecryption;
import com.ramsis.encrypt.dto.POJO;

@RestController
public class Controller {
   
@Autowired
EncryptionDecryption ecndnc;

@Value("${EncDncKey}")
String publickey;

@CrossOrigin(origins = "*")
@PostMapping("/Ngdecrypt")
public POJO NGdecrypt(@RequestBody POJO pojo) throws Exception {

String msg = pojo.getMessage();
String enckey = pojo.getKeys();
String decryptString = null;
// String publickey = "3K4G2pb4dl8M/7YaOrGiPA=="; // 128 bit key
try {
if (!enckey.equalsIgnoreCase(publickey)) {
System.out.println("You are not Authorised.");
pojo.setMessage("Not Authorised.");
System.out.println(pojo.getMessage());
return pojo;
}

decryptString = ecndnc.decrypt(publickey, null, msg);
pojo.setMessage(decryptString);
} catch (Exception ex) {
ex.getMessage();
}

return pojo;
}

@CrossOrigin(origins = "*")
@PostMapping("/Ngencrypt")
public POJO Ngencrypt(@RequestBody POJO pojo) throws Exception {

String msg = pojo.getMessage();
String enckey = pojo.getKeys();
String encryptString = null;
try {
if (!enckey.equalsIgnoreCase(publickey)) {
System.out.println("You are not Authorised.");
pojo.setMessage("Not Authorised.");
return pojo;
}
encryptString = ecndnc.encrypt(publickey, null, msg);
pojo.setMessage(encryptString);
//System.out.println(encryptString);
} catch (Exception ex) {
ex.getMessage();
}
return pojo;
}
}

----------------------------------------EncryptionDecryption----------------------------------------

package com.ramsis.encrypt.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;

@Component
public class EncryptionDecryption {

public static String encrypt(String key, String initVector, String value) {
byte[] encrypted =null;
try {
initVector = key.substring(0, 16); // 16 bytes IV
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

   encrypted = cipher.doFinal(value.getBytes());
System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted));
} catch (Exception ex) {
ex.printStackTrace();
}
return Base64.encodeBase64String(encrypted);
}

public static String decrypt(String key, String initVector, String encrypted) {
byte[] original =null;
try {
initVector = key.substring(0, 16);
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
   original = cipher.doFinal(Base64.decodeBase64(encrypted));
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(original);
}

}

------------------------------------------------ CallAPI ----------------------------------------------
package com.ramsis.encrypt.callApi;

import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class CallAPI {

public void restAPITest(String url, String message) {

String key = "3K4G2pb4dl8M/7YaOrGiPA==";
try {
RestTemplate restTemplate = new RestTemplate();
JSONObject personJsonObject = new JSONObject();
personJsonObject.put("keys", key);
personJsonObject.put("message", message);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> entityHttp = new HttpEntity<String>(personJsonObject.toString(), headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entityHttp, String.class);
/*
* String answer = restTemplate.postForObject(url, entity, String.class);
* System.out.println(answer);
*/
if (response.getStatusCodeValue() == 200) {
System.out.println(response.getBody());
}

} catch (Exception ex) {
ex.printStackTrace();
}
}

public static void main(String args[]) throws Exception {
CallAPI call = new CallAPI();
//call.restAPITest("http://localhost:8080/Ngencrypt", "ramsis-code");
call.restAPITest("http://localhost:8080/Ngdecrypt", "TaZydiy5PHtA4ME8744bOA==");

}
}

Sunday 20 November 2022

Spring Boot Rest API OneToMany Relationship with JPA Repository

In this Post I am showing how you can easily create a Rest API using JPA Repository with OneToMany Relationships between two Entities.

Project Structure



Calling Response from Postman tool














------------------------------------------ json ------------------------------------------------

{
"title":"Linux",
"author": "Jimmy",
"isbn": "890900",
    "pages" : [{ "number": "30", "content": "Switches", "chapter" : "3"},{ "number": "125", "content":          "Router", "chapter" : "8"}]
}


------------------------------------------ pom.xml -------------------------------------------

# Changing the server port
server.port=9090

# MySQL connection properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=cctns@123
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?createDatabaseIfNotExist=true&useSSL=false

# Log JPA queries
# Comment this in production
spring.jpa.show-sql=true

# Drop and create new tables (create, create-drop, validate, update)
# Only for testing purposes - comment this in production
spring.jpa.hibernate.ddl-auto=update

# Hibernate SQL dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect


---------------------------------------- Book Entity ------------------------------------------------

package com.ramsis.onetomany.entity;

import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "books")
public class Book implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "title" ,nullable = false)
    private String title;
    @Column(name = "author" ,nullable = false)
    private String author;
    @Column(name = "isbn" ,unique = true)
    private String isbn;
    @JsonIgnoreProperties("book")
    @OneToMany(mappedBy = "book", fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    private Set<Page> pages;

    public Book() {
    }

    public Book(String title, String author, String isbn) {
        this.title = title;
        this.author = author;
        this.isbn = isbn;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public Set<Page> getPages() {
        return pages;
    }

    public void setPages(Set<Page> pages) {
        this.pages = pages;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return id.equals(book.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", isbn='" + isbn + '\'' +
                '}';
    }
}

------------------------------------------ Page Entity -----------------------------------------------

package com.ramsis.onetomany.entity;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;

@Entity
@Table(name = "pages")
public class Page implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
   
    @Column(name = "number", nullable = false)
    private int number;
   
    @Column(name = "content" ,nullable = false)
    private String content;
   
    @Column(name = "chapter", nullable = false)
    private String chapter;
   
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "book_id", nullable = false)
    private Book book;

    public Page() {
    }

    public Page(int number, String content, String chapter, Book book) {
        this.number = number;
        this.content = content;
        this.chapter = chapter;
        this.book = book;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getChapter() {
        return chapter;
    }

    public void setChapter(String chapter) {
        this.chapter = chapter;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Page page = (Page) o;
        return id.equals(page.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Page{" +
                "id=" + id +
                ", number=" + number +
                ", content='" + content + '\'' +
                ", chapter='" + chapter + '\'' +
                ", book=" + book +
                '}';
    }
}

----------------------------------------------- BookService -----------------------------------------

package com.ramsis.onetomany.service;

import java.util.List;
import java.util.Set;

import com.ramsis.onetomany.dto.BookDto;
import com.ramsis.onetomany.entity.Book;
import com.ramsis.onetomany.entity.Page;

public interface BookService {

public List<Book> getBooks();
public Book addBook(BookDto book);
public Book updateBook(BookDto book);
public Book getBookByIsbn(String id);

}

-------------------------------------------------BookServiceImpl ----------------------------------

package com.ramsis.onetomany.service;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ramsis.onetomany.dto.BookDto;
import com.ramsis.onetomany.dto.PageDto;
import com.ramsis.onetomany.entity.Book;
import com.ramsis.onetomany.entity.Page;
import com.ramsis.onetomany.repository.BookRepository;
import com.ramsis.onetomany.repository.PageRepository;

@Service
public class BookServiceImpl implements BookService {

@Autowired
BookRepository bookRepo;

@Autowired
PageRepository pageRepo;

@Override
public Book getBookByIsbn(String id) {
// TODO Auto-generated method stub
return bookRepo.findByIsbn(id);
}

@Override
public Book addBook(BookDto book) {
Book bookEntity = new Book();
Page pageEntity = null;
bookEntity.setAuthor(book.getAuthor());
bookEntity.setIsbn(book.getIsbn());
bookEntity.setTitle(book.getTitle());
Set<PageDto> pageList = book.getPages();
Book bookEntityobj = bookRepo.save(bookEntity);

for (PageDto pagedata : pageList) {
pageEntity = new Page(
pagedata.getNumber(),
pagedata.getContent(),
pagedata.getChapter(),
bookEntityobj);
pageRepo.save(pageEntity);
}

return bookEntityobj;
}

@Override
public Book updateBook(BookDto book) {
Book bookEntity = new Book();
Page pageEntity = null;
bookEntity.setAuthor(book.getAuthor());
bookEntity.setIsbn(book.getIsbn());
bookEntity.setTitle(book.getTitle());
Set<PageDto> pageList = book.getPages();
Book bookEntityobj = bookRepo.save(bookEntity);

for (PageDto pagedata : pageList) {
pageEntity = new Page(
pagedata.getNumber(),
pagedata.getContent(),
pagedata.getChapter(),
bookEntityobj);
pageRepo.save(pageEntity);
}

return bookEntityobj;
}

@Override
public List<Book> getBooks() {
// TODO Auto-generated method stub
return  (List<Book>) bookRepo.findAll();
}

}

---------------------------------------------- Controller ---------------------------------------------

package com.ramsis.onetomany.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ramsis.onetomany.dto.BookDto;
import com.ramsis.onetomany.entity.Book;
import com.ramsis.onetomany.service.BookService;

@RestController
public class PageBookController {
   
@Autowired
BookService bookService;

@GetMapping("/home")
public String home() {
return "Welcome in RestApi with OneToMany Relation, home Page";
}

@PostMapping("/addBook" )
public Book addBook(@RequestBody  BookDto book) {
return this.bookService.addBook(book);
}

@PostMapping("/updateBook" )
public Book updateBook(@RequestBody  BookDto book) {
return this.bookService.updateBook(book);
}

@GetMapping("/getBookIsbn/{eid}")
public Book getBookByIsbn(@PathVariable("eid") String eid) {
return this.bookService.getBookByIsbn(eid);
}

@GetMapping("/getBooks")
public List<Book> getBooks() {
return this.bookService.getBooks();
}
}

------------------------------------------------- BookDto --------------------------------------------

package com.ramsis.onetomany.dto;

import java.util.Set;

public class BookDto {
private Long id;
private String title;
private String author;
private String isbn;
private Set<PageDto> pages;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Set<PageDto> getPages() {
return pages;
}
public void setPages(Set<PageDto> pages) {
this.pages = pages;
}

}

-------------------------------------------------- PageDto -------------------------------------------

package com.ramsis.onetomany.dto;

public class PageDto {
private Long id;
private int number;
private String content;
private String chapter;
private BookDto book;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getChapter() {
return chapter;
}
public void setChapter(String chapter) {
this.chapter = chapter;
}
public BookDto getBook() {
return book;
}
public void setBook(BookDto book) {
this.book = book;
}

}


------------------------------------------------- OnetomanyApplication -------------------------

package com.ramsis.onetomany;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import com.ramsis.onetomany.repository.BookRepository;
import com.ramsis.onetomany.repository.PageRepository;

@SpringBootApplication
@EnableJpaAuditing
public class OnetomanyApplication {

public static void main(String[] args) {
SpringApplication.run(OnetomanyApplication.class, args);
}


@Bean
   public CommandLineRunner mappingDemo(BookRepository bookRepository,
                                        PageRepository pageRepository) {
       return args -> {
        /*
        System.out.print( bookRepository.findById(Long.valueOf(1)));
           // create a new book
           Book book = new Book("Linux", "Cdac", "99844");

           // save the book
           bookRepository.save(book);

           // create and save new pages
           pageRepository.save(new Page(2, "Introduction contents of Linux", "Intro", book));
           pageRepository.save(new Page(102, "Switches", "Switches WAN", book));
           pageRepository.save(new Page(70, "Router", "Router WAN", book));
         
           */
       };
   }
}

-------------------------------------------- BookRepository ---------------------------------------

package com.ramsis.onetomany.repository;

import org.springframework.data.repository.CrudRepository;

import com.ramsis.onetomany.entity.Book;

public interface BookRepository extends CrudRepository<Book, Long> {

    Book findByIsbn(String isbn);
    Book save(Book book);
}

--------------------------------------------------------