Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jdk_8_maven/cs/rest/original/spring-docker-rest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM openjdk:8
ADD target/spring-boot-rest-api-v1.jar spring-boot-rest-api-v1.jar
EXPOSE 9090
ENV ACTIVE_PROFILE=dev
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","spring-boot-rest-api-v1.jar"]
50 changes: 50 additions & 0 deletions jdk_8_maven/cs/rest/original/spring-docker-rest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# spring-boot-docker-rest-api
Building RESTFul API Services using spring boot, MySQL and Swagger Documentation with containerization using Docker

Steps for executing :

1. Clone/Download the repository.

2. Open the project in the IDE (Netbeans/Intellij Idea/Eclipse) and generate the executable .jar file for the application. The alternate method to generate the .jar file is through Maven.

3. Rename **docker-compose-sample.yml** file to **docker-compose.yml**.

4. Open **docker-compose.yml** file and add the MySQL (db) environment parameter values and Spring REST API (spring-rest-api) environment parameter values for database connection from the the application.

5. Open the terminal and go to the directory where docker-compose.yml is located and run the below command in -d (Detach Mode) and will build the MySQL and Spring Boot Rest API Containers.

docker-compose up -d

6. Run the below command to get the list of running containers :

docker ps

7. After executing above steps without any errors and docker containers are up and running, open the browser and navigate to below url:

http://localhost:9090/swagger-ui.html#/

**DEMO**
- Deployed to Heroku Cloud:

https://polar-thicket-63660.herokuapp.com/swagger-ui.html


**Troubleshooting**
1. Any errors related to "connection link failure" is seen while starting/running containers then it might be due to the MySQL hostname use in the application database connection. Run the below command to get the hostname of the MySQL and replace it

docker inspect {CONTAINER-ID}


**References**
1. https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
2. https://docs.docker.com/compose/
3. http://mapstruct.org/
4. https://swagger.io/tools/swagger-ui/
5. https://spring.io/guides/gs/rest-service/
6. https://devcenter.heroku.com/articles/deploying-spring-boot-apps-to-heroku






Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3'

services:
db:
image: mysql:latest
restart: always
environment:
MYSQL_ROOT_PASSWORD:
MYSQL_DATABASE:
MYSQL_USER:
MYSQL_PASSWORD:
ports:
- "3306:3306"

spring-rest-api:
build: ./rest-api
depends_on:
- db
restart: always
environment:
- ACTIVE_PROFILE=dev
- DB_HOST=
- DB_PORT=3306
- DB_USER=
- DB_PASS=
- DB_NAME=
links:
- db:mysql
ports:
- "9090:9090"
156 changes: 156 additions & 0 deletions jdk_8_maven/cs/rest/original/spring-docker-rest/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.abhishekd</groupId>
<artifactId>rest-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>rest-api</name>
<description>Building REST API using Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
<springfox-swagger2.version>2.8.0</springfox-swagger2.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId> <!-- OR use this with Java 8 and beyond: <artifactId>mapstruct-jdk8</artifactId> -->
<version>${org.mapstruct.version}</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger2.version}</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger2.version}</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>${springfox-swagger2.version}</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

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

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.8.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>spring-boot-rest-api-v1</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>
com.abhishekd.restapi.RestApiApplication
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<compilerArg>
-Amapstruct.defaultComponentModel=spring
</compilerArg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.abhishekd.restapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class RestApiApplication {

public static void main(String[] args) {
SpringApplication.run(RestApiApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.abhishekd.restapi.api.v1.mapper;

import com.abhishekd.restapi.api.v1.model.CategoryDTO;
import com.abhishekd.restapi.domain.Category;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
* Data transfer object mapper using MapStruct
*/
@Mapper
public interface CategoryMapper {

CategoryMapper INSTANCE = Mappers.getMapper(CategoryMapper.class);

CategoryDTO categoryToCategoryDTO(Category category);
Category categoryDTOToCategory(CategoryDTO categoryDTO);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.abhishekd.restapi.api.v1.mapper;

import com.abhishekd.restapi.api.v1.model.CustomerDTO;
import com.abhishekd.restapi.domain.Customer;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
* Data transfer object mapper using MapStruct
*/
@Mapper
public interface CustomerMapper {

CustomerMapper INSTANCE = Mappers.getMapper(CustomerMapper.class);

CustomerDTO customerToCustomerDTO(Customer customer);
Customer customerDTOToCustomer(CustomerDTO customerDTO);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.abhishekd.restapi.api.v1.mapper;

import com.abhishekd.restapi.api.v1.model.VendorDTO;
import com.abhishekd.restapi.domain.Vendor;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
* Data transfer object mapper using MapStruct
*/
@Mapper
public interface VendorMapper {


VendorMapper INSTANCE = Mappers.getMapper(VendorMapper.class);

VendorDTO vendorToVendorDTO(Vendor vendor);
Vendor vendorDTOToVendor(VendorDTO vendorDTO);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.abhishekd.restapi.api.v1.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
* Category data transfer object model
*/
@ApiModel(value = "Category", description = "category")
@Data
public class CategoryDTO {

@ApiModelProperty(value = "Category Name", required = true)
@NotNull(message = "NotNull.categoryDTO.description")
@Size(min = 1, max = 255)
private String categoryName;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.abhishekd.restapi.api.v1.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;

/**
* Category list data transfer object model
*/
@ApiModel(value = "Category List", description = "category list")
@Data
@AllArgsConstructor
public class CategoryListDTO {

List<CategoryDTO> categories;
}
Loading
Loading