Skip to content

Commit e5bf170

Browse files
authored
Merge pull request #1 from anacondong/develop
Develop
2 parents c03902b + e03b826 commit e5bf170

19 files changed

+329
-262
lines changed

Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM openjdk:8-jdk-alpine
2+
RUN addgroup -S spring && adduser -S spring -G spring
3+
USER spring:spring
4+
RUN mvn clean && mvn package
5+
COPY target/workaround-1.0.0.jar app.jar
6+
EXPOSE 8080
7+
ENTRYPOINT ["java","-jar","/app.jar"]

Readme.md

-67
Original file line numberDiff line numberDiff line change
@@ -1,67 +0,0 @@
1-
# Spring Boot, MySQL, JPA, Hibernate Rest API Tutorial
2-
3-
Build Restful CRUD API for a simple Note-Taking application using Spring Boot, Mysql, JPA and Hibernate.
4-
5-
## Requirements
6-
7-
1. Java - 1.8.x
8-
9-
2. Maven - 3.x.x
10-
11-
3. Mysql - 5.x.x
12-
13-
## Steps to Setup
14-
15-
**1. Clone the application**
16-
17-
```bash
18-
git clone https://github.com/callicoder/spring-boot-mysql-rest-api-tutorial.git
19-
```
20-
21-
**2. Create Mysql database**
22-
```bash
23-
create database notes_app
24-
```
25-
26-
**3. Change mysql username and password as per your installation**
27-
28-
+ open `src/main/resources/application.properties`
29-
30-
+ change `spring.datasource.username` and `spring.datasource.password` as per your mysql installation
31-
32-
**4. Build and run the app using maven**
33-
34-
```bash
35-
mvn package
36-
java -jar target/easy-notes-1.0.0.jar
37-
```
38-
39-
Alternatively, you can run the app without packaging it using -
40-
41-
```bash
42-
mvn spring-boot:run
43-
```
44-
45-
The app will start running at <http://localhost:8080>.
46-
47-
## Explore Rest APIs
48-
49-
The app defines following CRUD APIs.
50-
51-
GET /api/notes
52-
53-
POST /api/notes
54-
55-
GET /api/notes/{noteId}
56-
57-
PUT /api/notes/{noteId}
58-
59-
DELETE /api/notes/{noteId}
60-
61-
You can test them using postman or any other rest client.
62-
63-
## Learn more
64-
65-
You can find the tutorial for this application on my blog -
66-
67-
<https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/>

docker-compose.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
version: '3'
2+
services:
3+
workaround-mysql:
4+
container_name: workaround-mysql
5+
image: mysql:5.7.22
6+
environment:
7+
MYSQL_DATABASE: workaround
8+
MYSQL_USER: admin
9+
MYSQL_PASSWORD: admin
10+
MYSQL_ROOT_PASSWORD: admin
11+
ports:
12+
- "3306:3306"
13+
networks:
14+
- my-network
15+
workaround:
16+
container_name: workaround
17+
depends_on:
18+
- workaround-mysql
19+
build:
20+
context: .
21+
dockerfile: Dockerfile
22+
working_dir: /workaround
23+
volumes:
24+
- ./:/workaround
25+
- ~/.m2:/root/.m2
26+
expose:
27+
- "8080"
28+
links:
29+
- workaround-mysql
30+
networks:
31+
- my-network
32+
# restart: always
33+
ports:
34+
- "8080:8080"
35+
networks:
36+
my-network:
37+
driver: bridge

pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>com.example</groupId>
7-
<artifactId>easy-notes</artifactId>
7+
<artifactId>workaround</artifactId>
88
<version>1.0.0</version>
99
<packaging>jar</packaging>
1010

11-
<name>easy-notes</name>
12-
<description>Rest API for a Simple Note Taking Application</description>
11+
<name>workaround</name>
12+
<description>Rest API for a Simple workaround Application</description>
1313

1414
<parent>
1515
<groupId>org.springframework.boot</groupId>

src/main/java/com/example/easynotes/EasyNotesApplication.java

-14
This file was deleted.

src/main/java/com/example/easynotes/controller/NoteController.java

-62
This file was deleted.

src/main/java/com/example/easynotes/model/Note.java

-81
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.workaround;
2+
3+
import com.example.workaround.model.User;
4+
import com.example.workaround.repository.UserRepository;
5+
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
11+
12+
@SpringBootApplication
13+
@EnableJpaAuditing
14+
public class WorkaroundApplication {
15+
16+
17+
public static void main(String[] args) {
18+
19+
20+
SpringApplication.run(WorkaroundApplication.class, args);
21+
}
22+
23+
@Bean
24+
public CommandLineRunner insertDemoData(UserRepository userRepository) {
25+
return args -> {
26+
User user1 = new User(new Long(1), "admin");
27+
userRepository.save(user1);
28+
};
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.easynotes.controller;
1+
package com.example.workaround.controller;
22

33

44
import org.springframework.web.bind.annotation.GetMapping;
@@ -9,8 +9,8 @@
99
@RequestMapping("/")
1010
public class IndexController {
1111

12-
@GetMapping
13-
public String sayHello() {
14-
return "Hello and Welcome to the EasyNotes application. You can create a new Note by making a POST request to /api/notes endpoint.";
15-
}
12+
@GetMapping
13+
public String sayHello() {
14+
return "Hello and Welcome to the workaround application. You can create a new Note by making a POST request to /api/notes endpoint.";
15+
}
1616
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.example.workaround.controller;
2+
3+
import com.example.workaround.model.User;
4+
import com.example.workaround.repository.UserRepository;
5+
6+
import javax.validation.Valid;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestBody;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
@RestController
15+
@RequestMapping("/api")
16+
public class LoginController {
17+
18+
@Autowired
19+
UserRepository userRepository;
20+
21+
22+
@PostMapping("/login")
23+
public User loginUser(@Valid @RequestBody User user) {
24+
User u = userRepository.findById(user.getId()).get();
25+
if (u.getPassword().equals(user.getPassword()))
26+
return user;
27+
else {
28+
throw new RuntimeException("Invalid User !!");
29+
}
30+
}
31+
32+
}

0 commit comments

Comments
 (0)