Showing posts with label Maven. Show all posts
Showing posts with label Maven. 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

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 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

Monday, January 14, 2019

Hello World Parameterized Junit Test

In this article we will see how to build a Parameterized Junit Test.

Parameterized tests was introduced with JUnit 4. Basic concept of Parameterized tests is to run the same test over and over again using different inputs.

There are 4 steps involve in this -

  • Annotate the test class with @RunWith(Parameterized.class)
  • Create a public static method annotated with @Parameterized.Parameters that returns a Collection of Objects as test data set
  • Create a public constructor that takes input as a single data item from step 2
  • Create an instance variable for each item of test data


Lets see the example -

Created a maven project as shown below -


pom.xml

HelloWorld.java

HelloWorldTest.java

Saturday, October 6, 2018

Java - Cucumber Hello World example

In the previous article we have seen JBehave Hello World example. In this article we will see same Hello World example but with using Cucumber.

Like JBehave, Cucumber is also used for behavior-driven development (BDD) testing. To know more about Cucumber click here.

Below is the eclipse folder structure -


Sunday, August 5, 2018

JBehave Hello World example (Eclipse + Maven + JUnit)

JBehave is a framework for Behaviour-Driven Development (BDD). To know more about JBehave, click here.

Below is the eclipse folder structure -



Please create a maven project and add the following files -


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



How to install maven and create a new blank maven project


Step 1 - Go to maven site and download maven binary zip archive as shown below
https://maven.apache.org/download.cgi



Step 2 - Once you download the file extract it to a particular location and set below Windows environment variables
 
M2_HOME - C:\TestMaven\apache-maven-3.5.0
MAVEN_HOME - C:\TestMaven\apache-maven-3.5.0

PATH - C:\TestMaven\apache-maven-3.5.0\bin

Make sure that JDK is installed and "JAVA_HOME" variable is added in your Windows environment variable

Now test maven using below command

mvn -version

 

Note - You can change the default maven localRepository as per your requirement, just go to apache-maven-3.5.0/conf/settings.xml and change the path

<localRepository>C:/TestMaven/repo</localRepository>