Sunday, March 22, 2020

Spring Boot with Swagger UI , JPA , MYSQL , Mockito, Integration Test and Sonar Qube

This post will expain you about
1. How we will integrate spring boot and swagger api
2. Spring boot with JPA and MySQL (Crud Repository and NamedJdbcTemplate)
3 Spring mockMvc test for controller
4. Integration test for Service and DAO classes
5. Usage of Mockito
6. Sonarqube Code coverage

Step 1 : create a maven project called - springboot-jpa-swagger-mysql-sonarqube in eclipse
Step 2 : provide groupId,artifactId,version,jar,name and description
Step 3: Replace below pom.xml into your local system
pom.xml will have dependencies related to spring boot,swagger,jdbc,mysql,mockito,sonarqube



 Demo project for Spring Boot PJA MySQL AND Sonarqube








Step 4: Now create a Springboot application, which is the starting point to run the Application.

Step 5: create under resources folder, add the db related details
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/employee
spring.datasource.username = root
spring.datasource.password = root

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

logging.level.root = DEBUG


Step 5: Create a SwaggerConfig class, which will have the details , what is controller package and other details
Learn more about Swagger API swagger-ui

package com.siva.springboot.javaguruonline.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

public class Swagger2Config {
 public Docket api() {
  return new Docket(DocumentationType.SWAGGER_2).select()

 private ApiInfo apiEndPointsInfo() {

  return new ApiInfoBuilder().title("Spring Boot REST API")
    .description("Employee Management REST API")
    .contact(new Contact("Siva Raju", "", ""))
    .license("Apache 2.0")

Step 6 : This project is related to employee management system - like Employee CRUD operations

Step 7 : Write Model class called Employee and EmployeeDetails

package com.siva.springboot.javaguruonline.model;


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

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@Table(name = "employee")
@ApiModel(description="All details about the Employee. ")
public class Employee implements Serializable{

 private static final long serialVersionUID = 7407317371057056536L;

 @ApiModelProperty(notes = "The database generated employee ID")
 private int id;

 @ApiModelProperty(notes = "The employee name")
 private String name;

 @ApiModelProperty(notes = "The employee age")
 private int age;

 public Employee() {


 public Employee(String name, int age) { = name;
  this.age = age;

 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "emp_id", nullable = false)
 public int getId() {
  return id;

 public void setId(int id) { = id;

 @Column(name = "emp_name", nullable = false)
 public String getName() {
  return name;

 public void setName(String name) { = name;
 @Column(name = "email_age", nullable = false)
 public int getAge() {
  return age;

 public void setAge(int age) {
  this.age = age;


package com.siva.springboot.javaguruonline.model;

import lombok.EqualsAndHashCode;
import lombok.ToString;

public class EmployeeDetails {
 private int empId;
 public int getEmpId() {
  return empId;
 public void setEmpId(int empId) {
  this.empId = empId;
 public String getEmpName() {
  return empName;
 public void setEmpName(String empName) {
  this.empName = empName;
 private String empName;


Step 8: Step 8: service/serviceimpl and repository classes

In the repository class, It is extending the JpaRepository, which will get all the default methods related to that Model Object.
In the DAO class, NamedParameterJdbcTemplate , to work with native query and how to map using rowmapper

package com.siva.springboot.javaguruonline.repository;

import java.util.List;

import com.siva.springboot.javaguruonline.model.EmployeeDetails;

public interface EmployeeDao {
 public List getEmployeeDetails();


package com.siva.springboot.javaguruonline.repository;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.siva.springboot.javaguruonline.mapper.EmployeeDetailsMapper;
import com.siva.springboot.javaguruonline.model.EmployeeDetails;

public class EmployeeDaoImpl implements EmployeeDao{
 String sqlQuery="select emp_id,emp_name from employee";
 private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

 public List getEmployeeDetails() {
  return namedParameterJdbcTemplate.query(sqlQuery, new EmployeeDetailsMapper());


package com.siva.springboot.javaguruonline.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.siva.springboot.javaguruonline.model.EmployeeDetails;

public class EmployeeDetailsMapper implements RowMapper {
 public EmployeeDetails mapRow(ResultSet resultset, int count) throws SQLException {
  EmployeeDetails employeeDetails = new EmployeeDetails();
  return employeeDetails;


package com.siva.springboot.javaguruonline.repository;

import org.springframework.stereotype.Repository;

import com.siva.springboot.javaguruonline.model.Employee;

public interface EmployeeRepository extends JpaRepository{


Step 9: Write Controller class, which will have the all the crud operations
package com.siva.springboot.javaguruonline.controller;

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

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
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.siva.springboot.javaguruonline.exception.ResourceNotFoundException;
import com.siva.springboot.javaguruonline.model.Employee;
import com.siva.springboot.javaguruonline.model.EmployeeDetails;
import com.siva.springboot.javaguruonline.repository.EmployeeDao;
import com.siva.springboot.javaguruonline.repository.EmployeeRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(value="Employee Management System")
public class EmployeeController {
 private static final String EMPLOYEE_NOT_FOUND_FOR_THIS_ID = "Employee not found for this id :: ";

 private EmployeeRepository employeeRepository;
 private EmployeeDao employeeDao;

 @ApiOperation(value = "View a list of available employees", response = List.class)
 @ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully retrieved list"),
   @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
   @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
   @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
 public List getAllEmployees() {
  return employeeRepository.findAll();
 @ApiOperation(value = "View a list of available employee details", response = List.class)
 @ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully retrieved list"),
   @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
   @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
   @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
 public List getAllEmployeeDetails() {
  return employeeDao.getEmployeeDetails();

 @ApiOperation(value = "Get an employee by Id")
 public ResponseEntity getEmployeeById(
   @ApiParam(value = "Employee id from which employee object will retrieve", required = true)
   @PathVariable(value = "id") Long employeeId)
   throws ResourceNotFoundException {
  Employee employee = employeeRepository.findById(employeeId)
    .orElseThrow(() -> new ResourceNotFoundException(EMPLOYEE_NOT_FOUND_FOR_THIS_ID + employeeId));
  return ResponseEntity.ok().body(employee);

 @ApiOperation(value = "Add an employee")
 public Employee createEmployee(
   @ApiParam(value = "Employee object store in database table", required = true)
   @Valid @RequestBody Employee employee) {

 @ApiOperation(value = "Update an employee")
 public ResponseEntity updateEmployee(
   @ApiParam(value = "Employee Id to update employee object", required = true)
   @PathVariable(value = "id") Long employeeId,
   @ApiParam(value = "Update employee object", required = true)
   @Valid @RequestBody Employee employeeDetails) throws ResourceNotFoundException {
  Employee employee = employeeRepository.findById(employeeId)
    .orElseThrow(() -> new ResourceNotFoundException(EMPLOYEE_NOT_FOUND_FOR_THIS_ID + employeeId));

  final Employee updatedEmployee =;
  return ResponseEntity.ok(updatedEmployee);

 @ApiOperation(value = "Delete an employee")
 public Map deleteEmployee(
   @ApiParam(value = "Employee Id from which employee object will delete from database table", required = true)
   @PathVariable(value = "id") Long employeeId)
   throws ResourceNotFoundException {
  Employee employee = employeeRepository.findById(employeeId)
    .orElseThrow(() -> new ResourceNotFoundException(EMPLOYEE_NOT_FOUND_FOR_THIS_ID + employeeId));

  Map response = new HashMap<>();
  response.put("deleted", Boolean.TRUE);
  return response;

Step 10: Once above code has been completed, then you can run the application by right clicking on the Springboot application.
Step 11. Once the application executed successfully, then we need to test this application

One way is either Using- SOAP UI, Postman - these needs to be installed on our machine, else we can't test the rest service.
To test the REST API, we already configured Swagger UI. So just go to your web browser, then click, http://localhost:8080/swagger-ui.html - it will display all the operations, which is availabe in controller class.

Now you can test those API's by giving request parameters

Step 12: Once done, we need to write Junit test cases for the all the classes

package com.siva.springboot.javaguruonline;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

public class SpringbootJpaSwaggerSonarqubeApplicationTest {

 public void contextLoads() {


Controller class test

package com.siva.springboot.javaguruonline.controller;

import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.meta.When;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.siva.springboot.javaguruonline.SpringbootJpaSwaggerSonarqubeApplication;
import com.siva.springboot.javaguruonline.model.Employee;
import com.siva.springboot.javaguruonline.model.EmployeeDetails;
import com.siva.springboot.javaguruonline.repository.EmployeeDaoImpl;
import com.siva.springboot.javaguruonline.repository.EmployeeRepository;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes=SpringbootJpaSwaggerSonarqubeApplication.class)
public class EmployeeControllerTest {
 private MockMvc mockMvc;
 private EmployeeRepository employeeRepository;
 private EmployeeDaoImpl employeeDao;
 public void testGetAllEmployees() throws Exception{
  List employeeList  = employeeRepository.findAll();
  ObjectMapper mapper = new ObjectMapper();
  String jsonString = mapper.writeValueAsString(employeeList);
  FileWriter file = new FileWriter(new File("employee.json"));
  .andExpect(jsonPath("$", hasSize(4)));

 public void testGetAllEmployeeDetails()  throws Exception {
  EmployeeDetails employee = new EmployeeDetails();
  List employeeList = new ArrayList();
  .andExpect(jsonPath("$", hasSize(4)));


DaoImpl Test

package com.siva.springboot.javaguruonline.repository;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.siva.springboot.javaguruonline.SpringbootJpaSwaggerSonarqubeApplication;
import com.siva.springboot.javaguruonline.model.EmployeeDetails;
import com.siva.springboot.javaguruonline.repository.EmployeeDao;
@SpringBootTest(classes = SpringbootJpaSwaggerSonarqubeApplication.class)
public class EmployeeDaoImplTest {
 public EmployeeDao employeeDao;
 public void testGetAllEmployeeDetails(){
  List employeeDetails = employeeDao.getEmployeeDetails();


Repository Test

package com.siva.springboot.javaguruonline.repository;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.siva.springboot.javaguruonline.SpringbootJpaSwaggerSonarqubeApplication;
import com.siva.springboot.javaguruonline.model.Employee;
import com.siva.springboot.javaguruonline.repository.EmployeeRepository;

@SpringBootTest(classes = SpringbootJpaSwaggerSonarqubeApplication.class)
public class EmployeeRepositoryTest {
 EmployeeRepository employeeRepository;
 public void getEmployeeDetails(){
  List employeeList = employeeRepository.findAll();
  Assert.assertNotNull("EmployeeListNotEmpty", employeeList);;


Step 13: We need to check the code coverage tool called Sonarqube, required dependencies added in pom.xml

Step 14: Download Sonarqube from SonarQube - and look for Historical Downloads, then download , which ever the version you want to work on.

Step 15 : Unzip that file and go to bin folder- StartSonar.bat file

Step 16 : Once Sonar is up, then go to browser and try http://localhost:9000 , login with username -admin and password- admin

Step 17: Once Sonarqube is up and running, we need to run the code through either sonar scanner or maven

Before Scanning the code, we need to create properties file called - place the inside of the your project
and need to mention src code path , test path, java version.. etc

# must be unique in a given SonarQube instance
# this is the name displayed in the SonarQube UI

# Path is relative to the file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set. 
# If not set, SonarQube starts looking for source code from the directory containing 
# the file.
# Encoding of the source code. Default is default system encoding

# Generate sonar issues report in html and console

# Display Jacoco report into SonarQube dashboard
# Comma-separated paths to directories with tests (optional)
# This name depends on the configuration in pom.xml. In this example we have ${}/coverage-reports/jacoco-ut.exec entry in our pom.xml

Step 18 : Use the below command to run the code coverage- make sure before running the sonar, sonarqube should up and running
Go to your project location in the command prompt.
/>mvn clean install sonar:sonar

Saturday, February 29, 2020

Getting Started with Apache Solr on Windows

This Post will explain about what is Apache Solr and how to do the setup on windows environment, Basic Apache Solr commands

What is Apache Solr?

Solr is the popular, blazing-fast, open source enterprise search platform built on Apache Lucene

Solr is highly reliable, scalable and fault tolerant, providing distributed indexing, replication and load-balanced querying, automated failover and recovery, centralized configuration and more. Solr powers the search and navigation features of many of the world's largest internet sites

We need the Java Runtime Environment (JRE) version 1.8 or higher to install the Apache Solr

Check the java version using the below command

Java -version

Step 1:

Download the Apache latest version from this link, either source or binary
Download Apache Solr


Step 2:

Unzip the downloaded one and set the path, inside environment variables, check my previous post how to set the environment variables

After Setting the env varibles, use the below command to start the solr

Step 3:

Go to the location, where it has unzipped up to bin folder

solr start

Will start the solr and give the message stating solr has been started

Know the status of the solr use the below command

solr status

create a core that uses a data-driven schema which tries to guess the correct field type when you add documents to the index.

solr create -c movies

Step 4:

Use the below link in the browser, to check about created core and other details

try with http://localhost:8983/

Step 5:

Stop the Solr

solr stop -all

Thank you for viewing this post , just we created the core, Next post will explain , how to do the indexing and search and many more features, which is provided by Apache Solr.

Sunday, February 23, 2020

How Apache Kafka is Better than any other traditional messaging brokers(Active MQ,JMS,Camel,IBM MQ)

1. Kafka is a distributed streaming platform
2. Kafka stores all the messages before sending or after successufully received by subscriping applications.
3. Kafka Run as a cluster on one or more servers that can be available on multiple datacenters.
4. Kafka is client centric and it has fair distribution of related messages to consumers and it is extreamly fast and scalable broker
5. Kafka has Horizontally scalable, by adding more partitions
6. Kafka does not degrade performance , adding of new consumers
7. Kafaka uses one destination type called topic, which internally combines both publish-subscribe and point-to-point
8. Kafka message is key-value pair. payload of the message sent as the value.
9. Kafaka cients are responsible for replaying failed messages
10. Kafka retains messages, it is possible for clients to retrieve any message, providing option for re-processing of all messages.

Traditional messasing brokers either don't persist messages at all. and it will store only until they consumed and acknowledged.
Traditional messaging brokers like imperative programming, An event is occurred and our code is notified of that event. it is tightly coupled.

You can Read Apache Kafka for more information about apache kafka

Monday, October 28, 2019

Jasper Reports export to EXCEL, CSV and PDF Using JRXML Java and My SQL

This Post will explain , How to export different formats using Jasper and Jrxml using java

Step 1: Download Download Jasper Studio
or I Report Designer

Step 2 : Design your page by providing connection details and query details and what columns needs to be display in PDF/excel/CSV

Step 3: Your design Page details can be mentioned in Summary Band.


Step 4: Once design completes , then it is time to write java class to export into different formats

Step 5: Excel report Generation

package arrayListTest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.export.SimpleCsvExporterConfiguration;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;

public class GenerateMultipleReportsFromJasper {
  public static void main(String[] args) {

  //Jrxml file source
  String sourceFileName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\csv_export.jrxml";
  //destination file location
  String outXlsName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\test.xls";
  //If we want to pass any parameters to jasper report, then we can use this map
  HashMap xlsParams = new HashMap();
  Connection con = null;
  try {
    // here employee is database name, root is username and password
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root", "root");
    JasperReport report = JasperCompileManager.compileReport(sourceFileName );
    JasperPrint xlsPrint = JasperFillManager.fillReport(report, xlsParams, con);
    JRXlsExporter xlsExporter = new JRXlsExporter();
    xlsExporter.setExporterInput(new SimpleExporterInput(xlsPrint));
    xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outXlsName));
    SimpleXlsReportConfiguration xlsReportConfiguration = new SimpleXlsReportConfiguration();
  } catch (Exception e) {
 if(con != null){
    catch(Exception ex){


Step 6: CSV Generation

HashMap csvParamsMap = new HashMap();
String outcsvName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\test.csv";
JasperReport report1 = JasperCompileManager.compileReport(sourceFileName );
JasperPrint jasperPrint = JasperFillManager.fillReport(report1, csvParamsMap, con);
JRCsvExporter exporter = new JRCsvExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(new File(outcsvName)));
SimpleCsvExporterConfiguration configuration = new SimpleCsvExporterConfiguration();

Step 7: PDF Generation

HashMap pdfParamsMap = new HashMap();
String outPdfName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\test.pdf";
JasperReport report2 = JasperCompileManager.compileReport(sourceFileName );
JasperPrint jasperPrint2 = JasperFillManager.fillReport(report2, pdfParamsMap, con);
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setExporterInput(new SimpleExporterInput(jasperPrint2));
pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outPdfName));
SimplePdfExporterConfiguration pdfConfiguration = new SimplePdfExporterConfiguration();

Step 8: Required jar file to execute this program

1. Itext 2.1.7.jar
2. commons-beanutils-1.9.2.jar
3. commons-logging-1.2.jar
4. commons-collections-3.2.1.jar
5. commons-lang-2.6.jar
6. commons-digester-1.6.jar
7. jasperreports-6.2.0.jar
8. mysql-connector-java-5.1.40.jar
9. poi-ooxml-3.16.jar
10. poi-3.12.jar

Step 9: Once We downloaded all jars then add into java build path.

Step 10: This is how we can export different format using jasper jrxml and Java.

Saturday, August 10, 2019

Getting started with Kubernetes and Install Kubernetes on Windows , Tools for Kubernetes Cluster

1. What is Kubernetes?
It is an open source system for automating deployment, scaling and managing containerized applications

How to install Kubernetes?

1. Install Kube control(kubectl) on Windows
2. kubectl (kubernet command control tool) , used to run the commands in kubernetes clusters.
3. Before install, we have to make sure each client version has to work with minor difference with master
For example, a v1.2 client should work with v1.1, v1.2, and v1.3 master

4. Download from the given link Download Kubernetes
5. Add the binary in to your PATH.(System - Environment Variables)
6.Check the version using kubectl version

We can create sample cluster online using following link

Online interactive kubernetes cluster

Popular tools for kubernetes clusters
1. (Mini Kube)
2. Google Kubernetes Engine (GKE)
3. Amazon Elastic Kubernetes Service (EKS):
4. Azure Kubernetes Service (AKS)

Wednesday, August 8, 2018

Getting started Machine Learning Using R on Windows environment Step By Step Process.

This post will explain us, how to install R on windows environment and how to work with Machine learning project using R with simple dataset

First Download R from this link.
You can download latest version of R.
1. Once download completes, then install the same in your machine. This is like any other software installation. There is no special instructions required for this.
2. After successful installation we need to setup the path- go to MyComputer-RightClick- environment variables- System variables-
C:\Program Files\R\R-3.5.1\bin

3. After setting up the path, Now we need to start the R
4. Go to command prompt and Type R
5. Now we can see the simple R terminal

6. Now we will understand what is machine learning? and what is datasets?
7. When we are applying machine learning to our own datasets, we are working on a project.
The process of a machine learning project may not be linear, but there are a number of well-known steps:

Define Problem.
Prepare Data.
Evaluate Algorithms.
Improve Results.
Present Results.

8.The best way to really come in terms with a new platform or tool is to work through a machine learning project end-to-end and cover the key steps.
Namely, from loading data, summarizing your data, evaluating algorithms and making some predictions.
Machine Learning using R Step By Step
Now this the time to work simple machine learning program using R and inbuilt dataset called iris

We already installed R and it has started.
Install any default packages using following syntax.

Packages are third party add-ons or libraries that we can use in R.

//While installing the package, after typing the above command , it will ask us for select mirror, you can select default one.
//Load the package, which we are going to use.
Load the data from inbuilt data and rename the same using following syntax.
// Attach iris dataset to the current environment
// Rename iris dataset  to dataset
 dataset <- iris
Now iris data loaded in R and accessible with variable called dataset Now we will create validation dataset. We will split the loaded dataset into two, 80% of which we will use to train our models and 20% that we will hold back as a validation dataset.
//  We create a list of 80% of the rows in the original dataset we can use for training
validation_index <- createDataPartition(dataset$Species, p=0.80, list=FALSE)
//select 20% of the data for validation
validation <- dataset[-validation_index,]
//use the remaining 80% of data to training and testing the models
dataset <- dataset[validation_index,]

Now we have training data in the dataset variable and a validation set we will use later in the validation variable. Note that we replaced our dataset variable with the 80% sample of the dataset. 1. dim function We can get a quick idea of how many instances (rows) and how many attributes (columns) the data contains with the dim function.
2. Attribute types - Knowing the types is important as it will give us an idea of how to better summarize the data we have and the types of transforms we might need to use to prepare the data before we model it.
3. head function used to display the first five rows.
4. The class variable is a factor. A factor is a class that has multiple class labels or levels
5. Class Distribution Let’s now take a look at the number of instances (rows) that belong to each class. We can view this as an absolute count and as a percentage. 6. Summary of each Attribute
Visualize Dataset We now have a seen the basic details about the data. We need to extend that with some visualizations. We are going to look at two types of plots: 1. Univariate plots to better understand each attribute. 2. Multivariate plots to better understand the relationships between attributes. First we will see the Univariate plots, this is for each individual variable. Input attributes x and the output attributes y.
  //Split input and output
    x <- dataset[,1:4]
    y <- dataset[,5]
Given that the input variables are numeric, we can create box and whisker plots of each.
   for(i in 1:4) {
   boxplot(x[,i], main=names(iris)[i])
We can also create a barplot of the Species class variable to get a graphical representation of the class distribution (generally uninteresting in this case because they’re even).
This confirms what we learned in the last section, that the instances are evenly distributed across the three class: Multivariate Plots First let’s look at scatterplots of all pairs of attributes and color the points by class. In addition, because the scatterplots show that points for each class are generally separate, we can draw ellipses around them.
We can also look at box and whisker plots of each input variable again, but this time broken down into separate plots for each class. This can help to tease out obvious linear separations between the classes.
Next we can get an idea of the distribution of each attribute, again like the box and whisker plots, broken down by class value. Sometimes histograms are good for this, but in this case we will use some probability density plots to give nice smooth lines for each distribution.
// density plots for each attribute by class value
scales <- list(x=list(relation="free"), y=list(relation="free"))
featurePlot(x=x, y=y, plot="density", scales=scales)
Evaluating the Algorithms Set-up the test harness to use 10-fold cross validation. We will split our dataset into 10 parts, train in 9 and test on 1 and release for all combinations of train –test splits. We will also repeat the process 3 times for each algorithm with different splits of the data into 10 groups We are using the metric of “Accuracy” to evaluate models. This is a ratio of the number of correctly predicted instances in divided by the total number of instances in the dataset multiplied by 100 to give a percentage (e.g. 95% accurate). We will be using the metric variable when we run build and evaluate each model next.
control <- tarinControl(method=”csv”,number=10)
     metric <- “Accuarcy”
Build 5 different models to predict species from flower measurements Linear Discriminant Analysis (LDA) Classification and Regression Trees (CART). k-Nearest Neighbors (kNN). Support Vector Machines (SVM) with a linear kernel. Random Forest (RF)
fit.lda <- train(Species~., data=dataset, method="lda", metric=metric, trControl=control)
# b) nonlinear algorithms
fit.cart <- train(Species~., data=dataset, method="rpart", metric=metric, trControl=control)
# kNN
fit.knn <- train(Species~., data=dataset, method="knn", metric=metric, trControl=control)
# c) advanced algorithms
fit.svm <- train(Species~., data=dataset, method="svmRadial", metric=metric, trControl=control)
# Random Forest
fit.rf <- train(Species~., data=dataset, method="rf", metric=metric, trControl=control)

We reset the random number seed before reach run to ensure that the evaluation of each algorithm is performed using exactly the same data splits. It ensures the results are directly comparable. Select the best model. We now have 5 models and accuracy estimations for each. We need to compare the models to each other and select the most accurate. We can report on the accuracy of each model by first creating a list of the created models and using the summary function.
# summarize accuracy of models
results <- resamples(list(lda=fit.lda, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf))

We can also create a plot of the model evaluation results and compare the spread and the mean accuracy of each model. There is a population of accuracy measures for each algorithm because each algorithm was evaluated 10 times (10 fold cross validation)
The results can be summarized. This gives a nice summary of what was used to train the model and the mean and standard deviation (SD) accuracy achieved, specifically 97.5% accuracy +/- 4% How to Predictions using predict and confusion Matrix The LDA was the most accurate model. Now we want to get an idea of the accuracy of the model on our validation set. This will give us an independent final check on the accuracy of the best model. It is valuable to keep a validation set just in case you made a slip during such as overfitting to the training set or a data leak. Both will result in an overly optimistic result. We can run the LDA model directly on the validation set and summarize the results in a confusion matrix.
predictions <- predict(fit.lda,validation)

Thursday, August 2, 2018

REST, REST Security, REST API Methods, REST annotations

REST - Representational State Transfer

1. REST is Architecture Style implementation

2. REST implemenation is based on Json Over HTTP

3. REST implemented based on simple HTTP protocol

4. REST has better scalability and performance

5. REST permits more data formats like JSON,XML etc..

6. REST emphasizes scalability of component interactions, independent deployments of components.

7. REST is design of HTTP and URI standards

8. REST is follow http methods like GET,POST,PUT,DELETE,PATCH

9. HTTP PATCH requests are to make partial update on a resource.
PUT requests also modify a resource entity so to make more clear –
PATCH method is the correct choice for partially updating an existing resource
and PUT should only be used if we are replacing a resource in it’s entirety.

10. REST impelnetations using JAX-RS and Jersy

11. Annotations of JAX-RS


Injects information into a class field, bean property, or method parameter


Extracts information from cookies declared in the cookie request header


Extracts information from a request representation whose content type is application/x-www-form-urlencoded


Extracts the value of a header


Extracts the value of a URI matrix parameter


Extracts the value of a URI template parameter


Extracts the value of a URI query parameter

12. HTTP Status codes

200 OK - Response to a successful REST API action. The HTTP method can be GET, POST, PUT, PATCH or DELETE.
400 Bad Request - The request is malformed, such as message body format error.
401 Unauthorized - Wrong or no authentication ID/password provided.
403 Forbidden - It's used when the authentication succeeded but authenticated user doesn't have permission to the request resource.
404 Not Found - When a non-existent resource is requested.
405 Method Not Allowed - The error checking for unexpected HTTP method. For example, the RestAPI is expecting HTTP GET, but HTTP PUT is used.

13. REST security interface to implement security programmatically

        public String updateUser(@Context SecurityContext sc) {
                if (sc.isUserInRole("admin"))  return "User will be updated";
                throw new SecurityException("User is unauthorized.");

Applying annotations to your JAX-RS classes


Declares roles.


Specifies that no security roles are allowed to invoke the specified methods.


Specifies that all security roles are allowed to invoke the specified methods.


Specifies the list of security roles that are allowed to invoke the methods in the application.


Defines the identity of the application during execution in a J2EE container.

@RolesAllowed({"ADMIN", "DEV"})
public class helloUser {

   public String updateUser() {
      return "User Updated!";

Updating the web.xml deployment descriptor to define security configuration


Thanks for viewing this post. If you like it don't forget to provide comments

Sunday, July 15, 2018

Given Strings are anagrams or not using java

What is String Anagram?

"An anagram is a type of word, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.

for Example: 'java' can be rearranged as - 'vaaj' or 'vjaa' -all characters are equal in both the words. So this is called string anagram.

Now we will see how to write a java program to check whether given strings are anagram or not.

package com.siva;

import java.util.Arrays;

public class StringAnagramTest {
  public boolean isStringAnagram(String s1, String s2){
   char[] charArray1 = s1.toCharArray();
   char[] charArray2 = s2.toCharArray();
   boolean stringAnagram = Arrays.equals(charArray1, charArray2);
   return stringAnagram;
  public static void main(String[] args) {
   StringAnagramTest test = new StringAnagramTest();
    String str1 ="test";
    String str2 ="sett";
  boolean stringAnagram = test.isStringAnagram(str1, str2);
    System.out.println("Given Strings["+str1+"] and ["+str2 +"] are anagrams");
    System.out.println("Given Strings["+str1+"] and ["+str2 +"] are not anagrams");


We can provide any value as input for both the strings.

Detailed Explanation:

Step 1: Convert given Strings into character array.

Step 2: Sort the converted character array using Arrays.sort

Step 3: compare both the strings using Arrays.equals

There are different ways to implement the same logic by writing our own sorting and equals methods. this is simple way to find the result.


Given Strings[test] and [sett] are anagrams

Thursday, March 1, 2018

Java Program to check whether given word is Palindrome or not

This post will explain us, how to implement palindrome logic with different ways, using java.

public class WordPalindrome {
  public static void main(String[] args) {
   String word="LIRIL";
   boolean isPalin = isPalindromeOne(word);
    System.out.println("Given word ["+word+"] is palindrome");
    System.out.println("Given word ["+word+"] is not palindrome");
   word = "Hello";
   isPalin = isPalindromeOne(word);
    System.out.println("Given word ["+word+"] is palindrome");
    System.out.println("Given word ["+word+"] is not palindrome");

  public static boolean isPalindromeOne(String word){
   boolean isPalindrome = false;
   //first way of checking
   char[] array = word.toCharArray();
   char[] newArray = new char[array.length];
   for (int i = 0; i < array.length; i++) {
  char c = array[array.length-i-1];
  newArray[i] =(char)c;
    isPalindrome = true;
   //second way of checking
   String reverse ="";
   for (int i = 0; i < word.length(); i++) {
  char c= word.charAt(i);
  reverse = c+reverse;
    isPalindrome = true; 
   return isPalindrome;

Saturday, February 10, 2018

Jersy Rest Webervice with eclipse and Weblogic 12c or tomcat

How to work with jersy rest webservice using eclipse and weblogic server/tomcat

Step 1: Create a dynamic web project in eclipse and provide project name

Step 2: open web.xml and add below configuration code for rest webservices.

        Jersey Web Application
        Jersey Web Application

Step 3: Create one file name called weblogic.xml and add the below code.


Step 4: Download rest api related jar - Unzip the same and place the jar inside lib folder of the project.

Step 5: Create a class name called add below code.

package com.siva.restservice;

 * @author Siva

public class SimpleRestService {
	public String getDetails(){
		System.out.println("This is Get request");
		return "Hello Welcome to Rest webservice";
	public String getDetailsWithParameter(@PathParam("requestParam") String requestParam){
		System.out.println("USer request paramater["+requestParam +"]");
		System.out.println("This is Get request");
		return "Hello Welcome to Rest webservice you have given input as -"+requestParam;


Step 6: Create one config class called-
Right click on src-> Webservice->RestWebservice->select Classname- which class needs to be configured in ApplicationConfig Click Next-> Finish

package rest.application.config;

import java.util.HashSet;
import java.util.Set;

 * @author Siva
public class ApplicationConfig  extends Application{

	public Set> getClasses(){
		return getRestClasses();
	private Set> getRestClasses(){
	  Set> resources = new HashSet>();	
	  return resources;

Step 7: This is very important step

Run the weblogic server and open the console
http://localhost:7001/console provide username and password and login the same into weblogic server admin console

Step 8: Click on the Deployments- the install the jax-rs-2.0.war, which is inside-
If you are working on jsr311 then deploy jsr311-api-111.war which is available on same path.

For Tomcat Deployment Step 3 , 6,7,8 not required

Right click on the Project and Run AS - server- select appropriate server and deploy the same.

if you want to test in browser use-

Step 9: Create rest client class which get the test results for rest webservice
package com.siva.restservice.test;


import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
 * @author Siva
public class RestServiceClient {
	public static void main(String[] args) {
		String uri = "http://localhost:8080/jersyRestService/rest/helloRest";
			Client client = Client.create();
			WebResource resource = client.resource(uri);
			String response = resource.type(MediaType.TEXT_PLAIN).get(String.class);
			System.out.println("Response from Rest webservice["+response+"]");
			String request = "GetRestWithParam";
			uri = uri.concat("/getDetail/").concat(request);
			resource = client.resource(uri);
			String response1 = resource.type(MediaType.TEXT_PLAIN).get(String.class);
			System.out.println("Response from Rest webservice with parameter["+response1+"]");
		catch(Exception ex){


Step 10: You can see the output as below

Response from Rest webservice[Hello Welcome to Rest webservice]
Response from Rest webservice with parameter[Hello Welcome to Rest webservice you have given input as -GetRestWithParam]

Thank you very much for viewing this post. If you like, don't forget to share/comment.

Sunday, November 12, 2017

Read and Parse XML using JAXB and write to a flat file with fixed length format using fixedFormat4j and Java

This post will explain, how to read xml ,parse xml and write to a flat file with fixed length format.

Step 1: Create a maven project -< xmltofile>

Step2: paste the below code into pom.xml file.



Step 3: Now we need to create company.xml file under project- create one xml file add the below xml to that file.

Present Bangalore Karnataka India
Permanent Kadapa AndharaPradesh India
Sanjay 27 Male 55000 01/07/1988
Present Bangalore Karnataka India
Permanent Hyderabad Telangana India
Madhan 34 Male 800000 01/07/1983
Present and Permanent Bangalore Karnataka India

Step 4: Create pojo classes , which need to be parsed as per above xml.

i have created 3 classes - -> List of -> List Of - xmlroot elment is company which is there in xml.

package xmltofile;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class Company {
 private List employee = new ArrayList();

 public List getEmployee() {
  return employee;

 public void setEmployee(List employee) {
  this.employee = employee;


package xmltofile;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
public class Employee {
 private String name;
 private int age;
 private String gender;
 private Double salary;
 private String dob;
address = new ArrayList
(); public String getName() { return name; } public void setName(String name) { = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @XmlElement public List
getAddress() { return address; } public void setAddress(List
address) { this.address = address; } public String getDob() { return dob; } public void setDob(String dob) { this.dob = dob; } }

package xmltofile;

import javax.xml.bind.annotation.XmlRootElement;

public class Address {
 private String city;
 private String type;
 private String state;
 private String country;
 public String getCity() {
  return city;
 public void setCity(String city) { = city;
 public String getType() {
  return type;
 public void setType(String type) {
  this.type = type;
 public String getState() {
  return state;
 public void setState(String state) {
  this.state = state;
 public String getCountry() {
  return country;
 public void setCountry(String country) { = country;


Step 5: Now we need to write class using fixedformat4j API-

package xmltofile.fixedformat;

import com.ancientprogramming.fixedformat4j.annotation.Field;
import com.ancientprogramming.fixedformat4j.annotation.Record;

public class CompanyHeader {
 private String header1;
 private String header2;
 private String header3;
 @Field(offset = 1, length = 20)
 public String getHeader1() {
  return header1;
 public void setHeader1(String header1) {
  this.header1 = header1;
 @Field(offset = 30, length = 50)
 public String getHeader2() {
  return header2;
 public void setHeader2(String header2) {
  this.header2 = header2;
 @Field(offset = 60, length = 100)
 public String getHeader3() {
  return header3;
 public void setHeader3(String header3) {
  this.header3 = header3;

} This class will have - each filed -at what is the starting index and data length of the eachfiled

package xmltofile.fixedformat;

import com.ancientprogramming.fixedformat4j.annotation.Field;
import com.ancientprogramming.fixedformat4j.annotation.Record;

public class EmployeeHeader {

 private String nameHeader;
 private String ageHeader;
 private String genderHeader;
 private String salaryHeader;
 private String dobHeader;
 private String addressHeader;
 @Field(offset = 1, length = 50)
 public String getNameHeader() {
  return nameHeader;
 public void setNameHeader(String nameHeader) {
  this.nameHeader = nameHeader;
 @Field(offset = 55, length = 3)
 public String getAgeHeader() {
  return ageHeader;
 public void setAgeHeader(String ageHeader) {
  this.ageHeader = ageHeader;
 @Field(offset = 60, length = 10)
 public String getGenderHeader() {
  return genderHeader;
 public void setGenderHeader(String genderHeader) {
  this.genderHeader = genderHeader;
 @Field(offset = 75, length = 10)
 public String getSalaryHeader() {
  return salaryHeader;
 public void setSalaryHeader(String salaryHeader) {
  this.salaryHeader = salaryHeader;
 @Field(offset = 90, length = 15)
 public String getDobHeader() {
  return dobHeader;
 public void setDobHeader(String dobHeader) {
  this.dobHeader = dobHeader;
 @Field(offset = 120, length = 200)
 public String getAddressHeader() {
  return addressHeader;
 public void setAddressHeader(String addressHeader) {
  this.addressHeader = addressHeader;


Step 6: Now we need to write test class to Parse the employee.xml using JAXB context and create the Flat file
and write the xml data into file at specified index.

package xmltofile;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import com.ancientprogramming.fixedformat4j.format.FixedFormatManager;
import com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl;

import xmltofile.fixedformat.CompanyHeader;
import xmltofile.fixedformat.EmployeeHeader;

public class XMLReaderAndWriteToFile {

 public static void main(String[] args) {
  BufferedWriter bw = null;
  try {
   JAXBContext context = JAXBContext.newInstance(Company.class);
   // Create Unmarshaller using JAXB context
   Unmarshaller unmarshaller = context.createUnmarshaller();
   System.out.println("xml file started to load...");
   Company company = (Company) unmarshaller.unmarshal(new File("employee.xml"));
            System.out.println("xml file loaded and parsed successfully..");
   // Write to file with fixedformat
            System.out.println("Flat file started to create...");
   bw = new BufferedWriter(new FileWriter("fixedLength.txt", true));
   System.out.println("empty flat file created...");
   System.out.println("company header written successfully to file.");
   System.out.println("employee header written successfully to file.");
   for (Employee employee : company.getEmployee()) {
    EmployeeHeader employeeDetails = new EmployeeHeader();
    String completeAddress = "  ";
    for (Address address : employee.getAddress()) {
     String addressDetails = address.getCity() + " " + address.getState() + " " + address.getCountry()
       + " - " + address.getType();
     completeAddress = completeAddress.concat(addressDetails) + "     ";
    FixedFormatManager manager1 = new FixedFormatManagerImpl();
    String data1 = manager1.export(employeeDetails);

   System.out.println("Employee details written successfully to file.");

  } catch (Exception ex) {
  } finally {
   try {
   } catch (IOException e) {


 private static void getEmployeeHeader(BufferedWriter bw) throws IOException {
  EmployeeHeader employeeHeader = new EmployeeHeader();
  employeeHeader.setNameHeader("Employee Name");
  FixedFormatManager manager = new FixedFormatManagerImpl();
  String data = manager.export(employeeHeader);

 private static void getCompanyHeader(BufferedWriter bw) throws IOException {
  CompanyHeader companyHeader = new CompanyHeader();
  companyHeader.setHeader1("ABC Company");
  FixedFormatManager manager = new FixedFormatManagerImpl();
  String data = manager.export(companyHeader);



Output file will be like this.

ABC Company                  Bangalore                                                                                                                         

Employee Name                                         Age  Gender         Salary         DOB                           Address                                                                                                                                                                                                 

siva                                                         34   Male           35000.0        01/07/1983                      Bangalore Karnataka India - Present     Kadapa AndharaPradesh India - Permanent                                                                                                                       
Sanjay                                                     27   Male           55000.0        01/07/1988                      Bangalore Karnataka India - Present     Hyderabad Telangana India - Permanent                                                                                                                         
Madhan                                                   34   Male           800000.0       01/07/1983                     Bangalore Karnataka India - Present and Permanent                                                                                                                                                     

Thank you verymuch for viewing this post. If you like this don't forget to share.

Monday, November 6, 2017 PKIX path building failed: Gradle eclipse PKIX path building failed:

If you are getting this error . While running the gradle build task from eclipse.
cacert might becorrupted.. which is there inside jdk/jre/lib

to change this copy or download the latest cacert from other system or internt.

create file and place it inside your .gradle home, which is configured in eclipse.\\Users\\ca-certs


Contact Form


Email *

Message *