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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s