Skip to content

Commit 04572e7

Browse files
authored
Merge pull request #153 from WebFuzzing/spring-docker-rest-starter
Spring docker rest
2 parents 6d8e36f + 9928ea4 commit 04572e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2748
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM openjdk:8
2+
ADD target/spring-boot-rest-api-v1.jar spring-boot-rest-api-v1.jar
3+
EXPOSE 9090
4+
ENV ACTIVE_PROFILE=dev
5+
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","spring-boot-rest-api-v1.jar"]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# spring-boot-docker-rest-api
2+
Building RESTFul API Services using spring boot, MySQL and Swagger Documentation with containerization using Docker
3+
4+
Steps for executing :
5+
6+
1. Clone/Download the repository.
7+
8+
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.
9+
10+
3. Rename **docker-compose-sample.yml** file to **docker-compose.yml**.
11+
12+
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.
13+
14+
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.
15+
16+
docker-compose up -d
17+
18+
6. Run the below command to get the list of running containers :
19+
20+
docker ps
21+
22+
7. After executing above steps without any errors and docker containers are up and running, open the browser and navigate to below url:
23+
24+
http://localhost:9090/swagger-ui.html#/
25+
26+
**DEMO**
27+
- Deployed to Heroku Cloud:
28+
29+
https://polar-thicket-63660.herokuapp.com/swagger-ui.html
30+
31+
32+
**Troubleshooting**
33+
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
34+
35+
docker inspect {CONTAINER-ID}
36+
37+
38+
**References**
39+
1. https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
40+
2. https://docs.docker.com/compose/
41+
3. http://mapstruct.org/
42+
4. https://swagger.io/tools/swagger-ui/
43+
5. https://spring.io/guides/gs/rest-service/
44+
6. https://devcenter.heroku.com/articles/deploying-spring-boot-apps-to-heroku
45+
46+
47+
48+
49+
50+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: '3'
2+
3+
services:
4+
db:
5+
image: mysql:latest
6+
restart: always
7+
environment:
8+
MYSQL_ROOT_PASSWORD:
9+
MYSQL_DATABASE:
10+
MYSQL_USER:
11+
MYSQL_PASSWORD:
12+
ports:
13+
- "3306:3306"
14+
15+
spring-rest-api:
16+
build: ./rest-api
17+
depends_on:
18+
- db
19+
restart: always
20+
environment:
21+
- ACTIVE_PROFILE=dev
22+
- DB_HOST=
23+
- DB_PORT=3306
24+
- DB_USER=
25+
- DB_PASS=
26+
- DB_NAME=
27+
links:
28+
- db:mysql
29+
ports:
30+
- "9090:9090"
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.abhishekd</groupId>
7+
<artifactId>rest-api</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>rest-api</name>
12+
<description>Building REST API using Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.2.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
26+
<springfox-swagger2.version>2.8.0</springfox-swagger2.version>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-jpa</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-devtools</artifactId>
42+
<scope>runtime</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.h2database</groupId>
46+
<artifactId>h2</artifactId>
47+
<scope>runtime</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.projectlombok</groupId>
51+
<artifactId>lombok</artifactId>
52+
<optional>true</optional>
53+
<scope>provided</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.mapstruct</groupId>
57+
<artifactId>mapstruct-jdk8</artifactId> <!-- OR use this with Java 8 and beyond: <artifactId>mapstruct-jdk8</artifactId> -->
58+
<version>${org.mapstruct.version}</version>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>io.springfox</groupId>
63+
<artifactId>springfox-swagger2</artifactId>
64+
<version>${springfox-swagger2.version}</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>io.springfox</groupId>
69+
<artifactId>springfox-swagger-ui</artifactId>
70+
<version>${springfox-swagger2.version}</version>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>io.springfox</groupId>
75+
<artifactId>springfox-data-rest</artifactId>
76+
<version>${springfox-swagger2.version}</version>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>mysql</groupId>
81+
<artifactId>mysql-connector-java</artifactId>
82+
<scope>runtime</scope>
83+
</dependency>
84+
85+
<dependency>
86+
<groupId>org.springframework.boot</groupId>
87+
<artifactId>spring-boot-starter-jdbc</artifactId>
88+
</dependency>
89+
90+
<dependency>
91+
<groupId>io.springfox</groupId>
92+
<artifactId>springfox-bean-validators</artifactId>
93+
<version>2.8.0</version>
94+
</dependency>
95+
96+
<dependency>
97+
<groupId>org.springframework.boot</groupId>
98+
<artifactId>spring-boot-starter-test</artifactId>
99+
<scope>test</scope>
100+
</dependency>
101+
</dependencies>
102+
103+
<build>
104+
<plugins>
105+
<plugin>
106+
<groupId>org.springframework.boot</groupId>
107+
<artifactId>spring-boot-maven-plugin</artifactId>
108+
<configuration>
109+
<finalName>spring-boot-rest-api-v1</finalName>
110+
</configuration>
111+
</plugin>
112+
<plugin>
113+
<groupId>org.apache.maven.plugins</groupId>
114+
<artifactId>maven-jar-plugin</artifactId>
115+
<version>2.4</version>
116+
<configuration>
117+
<archive>
118+
<manifest>
119+
<mainClass>
120+
com.abhishekd.restapi.RestApiApplication
121+
</mainClass>
122+
</manifest>
123+
</archive>
124+
</configuration>
125+
</plugin>
126+
<plugin>
127+
<groupId>org.apache.maven.plugins</groupId>
128+
<artifactId>maven-compiler-plugin</artifactId>
129+
<version>3.7.0</version>
130+
<configuration>
131+
<source>1.8</source>
132+
<target>1.8</target>
133+
<annotationProcessorPaths>
134+
<path>
135+
<groupId>org.projectlombok</groupId>
136+
<artifactId>lombok</artifactId>
137+
<version>${lombok.version}</version>
138+
</path>
139+
<path>
140+
<groupId>org.mapstruct</groupId>
141+
<artifactId>mapstruct-processor</artifactId>
142+
<version>${org.mapstruct.version}</version>
143+
</path>
144+
</annotationProcessorPaths>
145+
<compilerArgs>
146+
<compilerArg>
147+
-Amapstruct.defaultComponentModel=spring
148+
</compilerArg>
149+
</compilerArgs>
150+
</configuration>
151+
</plugin>
152+
</plugins>
153+
</build>
154+
155+
156+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.abhishekd.restapi;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
6+
7+
@SpringBootApplication
8+
@EnableScheduling
9+
public class RestApiApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(RestApiApplication.class, args);
13+
}
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.abhishekd.restapi.api.v1.mapper;
2+
3+
import com.abhishekd.restapi.api.v1.model.CategoryDTO;
4+
import com.abhishekd.restapi.domain.Category;
5+
import org.mapstruct.Mapper;
6+
import org.mapstruct.factory.Mappers;
7+
8+
/**
9+
* Data transfer object mapper using MapStruct
10+
*/
11+
@Mapper
12+
public interface CategoryMapper {
13+
14+
CategoryMapper INSTANCE = Mappers.getMapper(CategoryMapper.class);
15+
16+
CategoryDTO categoryToCategoryDTO(Category category);
17+
Category categoryDTOToCategory(CategoryDTO categoryDTO);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.abhishekd.restapi.api.v1.mapper;
2+
3+
import com.abhishekd.restapi.api.v1.model.CustomerDTO;
4+
import com.abhishekd.restapi.domain.Customer;
5+
import org.mapstruct.Mapper;
6+
import org.mapstruct.factory.Mappers;
7+
8+
/**
9+
* Data transfer object mapper using MapStruct
10+
*/
11+
@Mapper
12+
public interface CustomerMapper {
13+
14+
CustomerMapper INSTANCE = Mappers.getMapper(CustomerMapper.class);
15+
16+
CustomerDTO customerToCustomerDTO(Customer customer);
17+
Customer customerDTOToCustomer(CustomerDTO customerDTO);
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.abhishekd.restapi.api.v1.mapper;
2+
3+
import com.abhishekd.restapi.api.v1.model.VendorDTO;
4+
import com.abhishekd.restapi.domain.Vendor;
5+
import org.mapstruct.Mapper;
6+
import org.mapstruct.factory.Mappers;
7+
8+
/**
9+
* Data transfer object mapper using MapStruct
10+
*/
11+
@Mapper
12+
public interface VendorMapper {
13+
14+
15+
VendorMapper INSTANCE = Mappers.getMapper(VendorMapper.class);
16+
17+
VendorDTO vendorToVendorDTO(Vendor vendor);
18+
Vendor vendorDTOToVendor(VendorDTO vendorDTO);
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.abhishekd.restapi.api.v1.model;
2+
3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
5+
import lombok.Data;
6+
7+
import javax.validation.constraints.NotBlank;
8+
import javax.validation.constraints.NotNull;
9+
import javax.validation.constraints.Size;
10+
11+
/**
12+
* Category data transfer object model
13+
*/
14+
@ApiModel(value = "Category", description = "category")
15+
@Data
16+
public class CategoryDTO {
17+
18+
@ApiModelProperty(value = "Category Name", required = true)
19+
@NotNull(message = "NotNull.categoryDTO.description")
20+
@Size(min = 1, max = 255)
21+
private String categoryName;
22+
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.abhishekd.restapi.api.v1.model;
2+
3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Category list data transfer object model
12+
*/
13+
@ApiModel(value = "Category List", description = "category list")
14+
@Data
15+
@AllArgsConstructor
16+
public class CategoryListDTO {
17+
18+
List<CategoryDTO> categories;
19+
}

0 commit comments

Comments
 (0)