Integrate Swagger with Spring boot application for RESTful API documentation-Lab 4

Hi Readers,

I am happy to post another blog post on how to integrate swagger with Spring boot application for RESTful API documentation.

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

In pom.xml add the below dependency

 io.springfox
 springfox-swagger2
 2.2.2
 io.springfox
 springfox-swagger-ui
 2.2.2

Create a swagger config class similar to this.

Inside the predicate method, add your request mapping path.

Take a maven build and run as spring boot app.

Open a browser and type the below url.

http://localhost:8080/swagger-ui.html

swaggerprojectsjosepraveen1.PNG

From the above diagram the highlighted ones points to my @RequestMapping value of my controller.

Let me know your thoughts and experiences on how to integrate swagger with Spring boot application for RESTful API documentation.

Happy Learning !!

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.