While serializing java objects to JSON and deserializing JSON to Java Objects, sometimes one would want to ignore the property only while serializing but allow the property to be deserialized into Java object property. On a similar note there can be a requirement where a property has to be ignored only while deserializing into java object property but the property has to be allowed to be serialized.

Suppose we have a backend service in an application which stores the Customer details of the application like name,telephone,password etc.Supposing the Customer object structure to be below.

public class Customer {
private String name;
private String customerTelephone;
private String password;
private String welcomeMessage;

public String getName() {
return name;
}

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

public String getCustomerTelephone() {
return customerTelephone;
}

public void setCustomerTelephone(String customerTelephone) {
this.customerTelephone = customerTelephone;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getWelcomeMessage() {
return welcomeMessage;
}

public void setWelcomeMessage(String welcomeMessage) {
this.welcomeMessage …

Spring Boot 2.2.0 comes with a handy feature which can be utilized to reduce the application start up time by deferring the bean creations and dependency injections to later time thereby quickening the application start up time.

Spring beans creation will be initiated only when needed by Lazy initializing the bean creation and dependency injections.

Lazy Initialization is already possible in previous versions of spring boot but then to enable lazy initialization one need to implement BeanFactoryPostProcessor. Spring boot 2.2.0 offers a simple way to enable lazy initialization of beans.

There is a property added newly that will enable/disable lazy…


When you want to apply a specific color to an image using java,it can be done using the BufferedImage class.

As we know that images are made up of pixels and each pixel is represented by it’s red,green,blue values which range from 0 to 255.So,to represent a color in each pixel these red,green,blue values has to be mixed in the appropriate proportion required to obtain the desired color to the pixel.

An example program to achieve the effect of applying a color layer to an image is given below:

package com.example.demo;

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.util.Scanner;
import javax.imageio.ImageIO;

public class ImageColorFilter{
public static void main(String args[])throws IOException{

Scanner commandLineScanner = new Scanner(System.in);

System.out.println("Select the number of the color to apply for the image:");
System.out.println("1.Blue");
System.out.println("2.Red");
System.out.println("3.Green");
System.out.println("4.Purple");
System.out.println("5.Orange");
System.out.println("6.Yellow");
System.out.println("7.Gold");
System.out.println("8.Pink");
System.out.println("9.Turquoise");
System.out.println("10.Silver");
System.out.println("11.Other");

int selectedOption = commandLineScanner.nextInt();

int redPercent = 0;
int greenPercent = 0;
int bluePercent = 0;

switch (selectedOption){
case 1:bluePercent=100;
break;
case 2:redPercent=100;
break;
case 3:greenPercent=100;
break;
case 4:redPercent=50;
bluePercent=50;
break;
case 5:redPercent=100;
greenPercent=65;
break;
case 6:redPercent=100;
greenPercent=100;
break;
case 7:redPercent=100;
greenPercent=84;
break;
case 8:redPercent=100;
greenPercent=75;
bluePercent=80;
break;
case 9:redPercent=25;
greenPercent=88;
bluePercent=82;
break;
case 10:redPercent=75;
greenPercent=75;
bluePercent=75;
break;
case 11:System.out.println("Enter Red %:");
redPercent = commandLineScanner.nextInt();

System.out.println("Enter Green %:");
greenPercent = commandLineScanner.nextInt();

System.out.println("Enter Blue %:");
bluePercent = commandLineScanner.nextInt();
break;
}

applyColorFilter(redPercent, greenPercent, bluePercent);
}

private static void applyColorFilter(int redPercent, int greenPercent, int bluePercent) throws IOException {
File inputImage = new File("/home/sai/Desktop/aishwarya.jpg");//File path for input image
BufferedImage image = ImageIO.read(inputImage);

for(int y = 0; y < image.getHeight(); y++){
for(int x = 0; x < image.getWidth(); x++){
int pixel = image.getRGB(x,y);

int alpha = (pixel>>24)&0xff;
int red = (pixel>>16)&0xff;
int green = (pixel>>8)&0xff;
int blue = pixel&0xff;

pixel = (alpha<<24) | (redPercent*red/100<<16) | (greenPercent*green/100<<8) | (bluePercent*blue/100);

image.setRGB(x, y, pixel);
}
}

ImageIO.write(image, "jpg", new File("/home/sai/Desktop/output.jpg")); //File path to store resulting image. …


When you are using Java.util package’s classes like Date,Calendar etc ,quite often than not one would need to get the date or Calendar populated with current date,time.

In such cases,we can obtain that using below

Date currentDate = new Date();
Calendar currentCalendar = Calendar.getInstance();
System.out.println(currentDate);
System.out.println(currentCalendar);
------------------------------------
Output:
Tue Jun 19 20:02:38 IST 2018
Tue Jun 19 20:02:38 IST 2018

Generally, while creating a Rest application using Spring one would leverage Jackson’s message converter to convert the received JSON onto domain object in java i.e typically a POJO.

Example: Let’s say there is a request which we need to send to the service then that has to be de-serialized to a Java object.Java object would be this:

package com.example.demo;public class Request {
private String firstName;
private String lastName;
private int age;

public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age …

Sai Pitchuka

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store