Integrate H2 database in your Spring Boot Application – Lab 3

Hi Readers,

I am happy to post another blog post on how to integrate H2 database in your Spring Boot Application.

In this tutorial we will be using the H2 database in embedded mode.

You have created a  spring boot application and like to integrate with h2 database.

In pom.xml add the below dependency.

 <!-- H2 Database -->
 <dependency>
 <groupId>com.h2database</groupId>
 <artifactId>h2</artifactId>
 </dependency>

In applciation.properties file add the below line to enable the h2 database.

# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2

# Datasource
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

 

Take a maven build and run the application as spring boot app.

Open a browser and type

http://localhost:8080/h2  to get the below image.

h2databasejosepraveen

The above yellow highlighted lines should match your application.properties file.

h2databasejosepraveen1

If you get stuck, kindly comment below.

Ask me your questions/ doubt in quora.

Thanks for reading this post. Subscribe my blog for  some awesome posts.

Integrate Swagger with JAVA + Maven + JDBC + Jersey for RESTful API documentation-Lab 2

Hi Readers,

I am happy to post another blog post on how to integrate swagger with RESTful API documentation with java.

You have created a RESTful project(say) and like to integrate swagger for representing your RESTful API then this post is for you.

I have done a maven project and will be listing out the dependencies and other settings to be done in your project to get the swagger representation.

You need to add some configuration tags and dependencies in web.xml and pom.xml . You can get those from this link.

In your service layer i.e REST layer of your project. You need to add @Api above the class and @ApiOperation above the method like this.

Now you need to integrate swagger ui into your projects.

Go to this link and download Swagger UI from github.

Unzip the swagger ui and go to dist folder(swagger-ui-2.2.6dist) and copy those files to your WebContent folder.

Your project will look like this

swaggerprojectsjosepraveen

Once you have copied all the files from dist folder to your PROJECT/WebContent folder.

Open the index.html(swagger) and change the URL like below(line number 48)
url = “http://localhost:8080/YOURPROJECTNAME/URL PATTERN(check in web.xml file)/api-docs”; (i.e) url = “http://localhost:8080/SwaggerProjREST/rest/api-docs

Note: The above url should match with the web.xml swagger.api.basepath param value.

swagger.api.basepath http://localhost:8080/YOURPROJECTNAME/URL PATTERN

Now you have to build your project, you need to add maven dependencies to your deployment assembly of your project. Deploy the war in a tomcat server to get the below page.

swaggerrestjosepraveen

If you get stuck, kindly comment below.

Ask me your questions/ doubt in quora.

Thanks for reading this post. Subscribe my blog for  some awesome posts.

Develop RESTful Web Services API using Java, MySQL with Maven- Lab 1

Hi Readers,

I am happy to post another blog post on RESTful web services api using java and mysql.

This tutorial is based out of maven. So, create a maven project with below structure.

Project structure:

RESTdbjosepraveenlab1.PNG

Download the project from the below link and import it as maven project.

Product.java

package com.josetechblog.model;

public class Product {

private String productCode;
 private String productName;
 private String productLine;
 private String productScale;

public String getProductCode() {
 return productCode;
 }

public void setProductCode(String productCode) {
 this.productCode = productCode;
 }

public String getProductName() {
 return productName;
 }

public void setProductName(String productName) {
 this.productName = productName;
 }

public String getProductLine() {
 return productLine;
 }

public void setProductLine(String productLine) {
 this.productLine = productLine;
 }

public String getProductScale() {
 return productScale;
 }

public void setProductScale(String productScale) {
 this.productScale = productScale;
 }

}


 JDBCConnection.java
package com.josetechblog.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCConnection {

public Connection getConnnection() {
 Connection connection = null;

try {
 String connectionURL = "jdbc:mysql://localhost:3306/classicmodels";
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 connection = DriverManager.getConnection(connectionURL, "root", "root");


} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
 // TODO Auto-generated catch block
 e.getLocalizedMessage();
 }
 return connection;
 }

}

ProductDaoImpl.java

Create a dao(Data Access Object) method with list datatype, using select statement getting results from product table. Binding results into productData object.

package com.josetechblog.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.josetechblog.model.Product;

public class ProductDaoImpl {

public List getProductDetails() {

List productData = new ArrayList<>();

JDBCConnection jdbcConnection = new JDBCConnection();

Connection connection = jdbcConnection.getConnnection();

try {
 PreparedStatement ps = connection.prepareStatement(
 "select productCode,productName,productLine,productScale from classicmodels.products");
 ResultSet rs = ps.executeQuery();

while (rs.next()) {
 Product product = new Product();
 product.setProductCode(rs.getString("productCode"));
 product.setProductLine(rs.getString("productName"));
 product.setProductName(rs.getString("productLine"));
 product.setProductScale(rs.getString("productScale"));
 productData.add(product);

}

} catch (SQLException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

return productData;
 }

}

ProductService.java

Service class for RESTful API.

package com.josetechblog.service;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.google.gson.Gson;
import com.josetechblog.dao.ProductDaoImpl;
import com.josetechblog.model.Product;

@Path("/product")
public class ProductService {

@GET
 @Path("/productdetails")
 @Produces("application/json")
 public String getProductDetails() {
 String productDetails = null;
 List productList = null;

ProductDaoImpl productDaoImpl = new ProductDaoImpl();

productList = productDaoImpl.getProductDetails();

Gson gson = new Gson();
 productDetails = gson.toJson(productList);
 return productDetails;
 }

}

To get the code of pom.xml, web.xml. Please check my github link.

Take a maven build with maven goals as clean install.

Note: Right click your project to add maven dependencies under the deployment assembly section.

restdbjosepraveen2

 

JSON Output:

[
 {
 "productCode": "S10_1678",
 "productName": "Motorcycles",
 "productLine": "1969 Harley Davidson Ultimate Chopper",
 "productScale": "1:10"
 },
 {
 "productCode": "S10_1949",
 "productName": "Classic Cars",
 "productLine": "1952 Alpine Renault 1300",
 "productScale": "1:10"
 },
 {
 "productCode": "S12_1099",
 "productName": "Classic Cars",
 "productLine": "1968 Ford Mustang",
 "productScale": "1:12"
 },
 {
 "productCode": "S12_1108",
 "productName": "Classic Cars",
 "productLine": "2001 Ferrari Enzo",
 "productScale": "1:12"
 },
 {
 "productCode": "S12_1666",
 "productName": "Trucks and Buses",
 "productLine": "1958 Setra Bus",
 "productScale": "1:12"
 }
]

If you get stuck, kindly comment below.

Ask me your questions/ doubt in quora.

Thanks for reading this post. Subscribe my blog for  some awesome posts.

Monitor your built-in endpoints- Spring Boot Lab 2

Hi Readers,

I am happy to post another blog post on how to monitor your built-in endpoints of your Spring Boot application.

While i was checking my STS console my look peeped into area which is shown below.

springbootjosepraveen3.PNG

If you like to activate your built-in endpoints in your spring boot applciation. Just add the below dependencies in your pom.xml file

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

After adding the above dependencies in pom.xml take a maven build with goals as clean install and run the spring boot application.

You will notice the above image in the console which gets rolled out.

Open a browser and hit the below ones.

http://localhost:8080/health,

http://localhost:8080/env ,

http://localhost:8080/beans,

http://localhost:8080/beans,

http://localhost:8080/trace,

http://localhost:8080/info,

http://localhost:8080/trace

If you get stuck, kindly comment below.

Ask me your questions/ doubt in quora.

Thanks for reading this post. Subscribe my blog for  some awesome posts.

Using map reduce to find max and min temperature for a data set – lab 1

Hi Readers,

I am happy to post another blog post on how to use map reduce to find Max and Min temperature of a data set.

I have a dataset (say temperaturedata.txt) file. I like to analyze the above data set using map reduce.

I have a written a map reduce program to find the max and min temp of the year.

 

I have a data set in my linux file system and I need to move that to hadoop file system.

hdfs dfs -copyFromLocal /home/cloudera/Downloads/temperaturedata.txt /jpraveen/temperaturedata.txt

Now the file is in hdfs directory i.e (hdfs dfs -ls /jpraveen/)

Now you need to convert your map reduce program into a jar file and run the jar file using the below command.

hadoop jar MaxMinTemp.jar  /jpraveen/temperaturedata.txt  ~/output1

Note:

Below code snippet in the main method of your program

 FileInputFormat.setInputPaths(job, new Path(args[0])) represents this value /jpraveen/temperaturedata.txt.
 FileOutputFormat.setOutputPath(job, new Path(args[1])) represents this value ~/output1.

JobClient.runJob(job) this will trigger the map reduce job to start.

Moreover you can also track the job status which you will get at the time of running the map reduce.

http://quickstart.cloudera:19888/jobhistory/job/job_1479137854338_0004/

The output of the above job can seen using the below command or through file browser

hdfs dfs -cat /root/output1/part-00000 .

(or)

http://quickstart.cloudera:50070/explorer.html#/root/output5.

Integrate inmemory authentication for a Spring Boot application – Lab 1

Hi Readers,

I am happy to post another blog post on how to Integrate inmemory authentication for a Spring Boot application.

I have created a simple spring boot project in STS IDE using Spring Starter kit. While navigating through the starter kit for creating your spring boot application you need to select web so that the spring boot web dependencies get added to the pom.xml file.

springbootjosepraveen

Now I like to have a in-memory authentication  for my sample project.

Open pom.xml add the below dependency

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

Create a simple class like this.

Take a maven build with goals as clean, install.

Run this spring boot application. You need to give the right credentials as in authentication class

springbootjosepraveen1.PNG

springbootjosepraveen2.PNG

Points to Ponder:

AuthenticationManagerBuilder ==>> SecurityBuilder used to create an AuthenticationManager. Allows for easily building in memory authentication, LDAP authentication, JDBC based authentication, adding UserDetailsService, and adding AuthenticationProvider’s.

JSON format in GNU/linux- Bash for Windows 10

Hi Readers,

I am happy to post another blog post on how to format a JSON string in GNU/Linux.

If a string is large then JSON looks very cryptic and it becomes difficult to search/edit it.

We can format easily using the Python interpreter. Python provides JSON modules that will do the needful.

#echo ‘content’ | python -m json.tool

josepraveenjsoninbash

Hope this post helps you. Please subscribe and comment if you have any doubts.

Happy Learning !!