Showing posts with label SpringBoot. Show all posts
Showing posts with label SpringBoot. Show all posts

Sunday, September 26, 2021

Java JSON Web Tokens example

 What is JSON Web Token?

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. For more details, click here.



In this example, User object is encrypted to jwt also validating it and decrypting token to user object again. For complete github source code, click here

JwtTokenService.java

Wednesday, June 3, 2020

Spring cloud service discovery example

Real time cloud application consist of a large number of micro services communicating with each other. Service instances are added/removed dynamically based on requirement and availability. Service discovery is the process of one service dynamically discovering the network location (IP address and port) of another service without hard coding their location.


Steps involved in service discovery

1. Service registers location - when service boots up it registers itself with discovery server

2. Client looks for service location - When client needs to hit a particular service first it goes to discovery server for its location

3. Discovery server sends back location - Discovery server sends the active location of the particular service

4. Client request service at location - This is a normal request to the service

5. Service sends response - Service responds to client accordingly

Example -

Started eureka server on default port 8761 and Started 2 instances of Application service on 8081 and 8082




Sunday, May 31, 2020

Spring boot ActiveMQ example

ActiveMQ Setup

Download the latest version of ActiveMQ from below link

https://activemq.apache.org/components/classic/download/



Extract the zip file, go to bin folder and start activemq server using activemq.bat

In my case it is
apache-activemq-5.15.11/bin/win32/activemq.bat

Verify ActiveMQ server is UP and running

http://localhost:8161/


Note - You can change the default port 8161 by updating below file
apache-activemq-5.15.11/conf/jetty.xml

Go to admin page

http://localhost:8161/admin/

Login with default credentials

Username - admin
Password - admin



Note - You can change default credentials by updating below file
apache-activemq-5.15.11/conf/jetty-realm.properties

Here is how our data will flow


Saturday, June 29, 2019

Spring Boot + Jasypt example to encrypt database password in property file

In this example we will see how to encrypted database password in property file (application.properties or application.yml). We will use Jasypt library for this purpose.

Jasypt (Java Simplified Encryption) is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.

To know more about Jasypt, click here. Go to Jasypt website and download the latest version of jasypt client. I am using jasypt-1.9.2-dist.zip. Once downloaded extract the zip file and go to folder /jasypt-1.9.2/bin and execute the below command. Here input is your password or any other text that you want to encrypt and password is the secret key used by Jasypt to encode and decode the input.

encrypt.bat input="dummy_password" password="SECRET_KEY"


To know about encrypting from the command line using Jasypt CLI Tools. click here.

Add below maven dependency in your pom.xml file

<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>

application.yml
Note - the encrypted password in parenthesis with keyword ENC 


There are various options by which you can feed the SECRET_KEY to your application

- From java code you can set system property jasypt.encryptor.password as shown below. You can do this in a separate secure JAR file.

System.setProperty("jasypt.encryptor.password", "SECRET_KEY");


- From command line you can pass system properties

$ Java -Djasypt.encryptor.password=SECRET_KEY Application

My Sql + Spring Boot JPA - One to many, many to one example

This is a simple spring boot JPA (hibernate) example. It will demonstrate how one to many, many to one mapping can be done

Below are the tables created in MySQL

create table department
(
 department_id int not null auto_increment,primary key (department_id),
 department_name  varchar(50) not null
);


create table employee
(
 employee_id int not null auto_increment, primary key (employee_id),
 employee_name varchar(50) not null, 
 department_id int not null, foreign key (department_id) references department(department_id)
);

pom.xml


application.yml


Friday, June 28, 2019

Java - Export database table to CSV file

This is a sample springboot application with bare minimum code to export database tables to CSV file

pom.xml

application.yml

Application.java

Java - Export database tables to an excel file

This is a sample springboot application with bare minimum code to export database tables to an excel file

pom.xml

application.yml

Application.java

Saturday, March 23, 2019

Spring Boot + ReactJS hello world example

Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring. To know more about spring boot, click here.

ReactJS is a JavaScript library used for building user interfaces. To know more about react, click here.

In this example spring boot will exposed our business logic using rest endpoints and reactjs is used as frontend user interface. Point here to be noted is unlike a traditional web application where java codes + web code (HTML + javascript + css) are residing in same project here spring boot and reactjs will be in different project. The main advantage of this type of setup is to have a loose coupling between your UI and business logic. Tomorrow if you feel to replace reactjs with anjularjs or vuejs you can very well do it without touching business logic.

Step 1 -
Create a spring boot application, click here for more details.

pom.xml


Saturday, August 25, 2018

Simple spring integration with springboot

In this example we will see a simple file poller application using spring integration. It will poll for a file in a particular location at certain interval and if file is found it will move it to another location.

There are 3 core components of spring integration -

Messages - Messages are the objects sent from one component to another. In our case a file is sent from file poller to file handler.
Channels - Assume channels are the network (a pipe) by which messages are sent from one component to another.
Adapters - Adapters route the output from one channel to the input of another one.



Please see the codes below -



Friday, July 13, 2018

Spring boot web application with JSP

In this article we will see a sample spring boot web application with front end as JSP

Maven project structure -

Spring boot web application with thymeleaf


In this article we will see a sample spring boot web application with front end as thymeleaf. To know more about thymeleaf, click here.

Note you can use any other view technology like JSP instead of thymeleaf as per your requirement

Project structure -

Friday, June 15, 2018

Simple Springboot + hibernate + mysql example

This is a simple spring boot + hibernate (JPA) example.

Below are the tables created in MySQL

create table category
(
catid int not null auto_increment,primary key (catid),
catname  varchar(30) not null
);


create table menu
(
menuid int not null auto_increment, primary key (menuid),
menuname varchar(60) not null,
catid int not null, 
        foreign key (catid) references category(catid)

);


Here is the pom.xml file



Saturday, July 22, 2017

JavaFx with SpringBoot

In this example we will see how to create a JavaFx project using spring boot.

Step 1 - Create a new maven project using below command. If you are new to spring boot, please check my previous article on standalone spring boot application.

mvn archetype:generate -DgroupId=com.javaxp -DartifactId=TestJavaFx -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Step 2 - Create below file -

pom.xml


Standalone SpringBoot Helloworld example

Step 1 - Run the below maven command. If maven is not installed in your machine please click here to check how to install maven.

mvn archetype:generate -DgroupId=com.javaxp -DartifactId=TestSpringboot -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false