Skip to content

Commit aa5e579

Browse files
committed
Spring Boot RESTful API
1 parent 5459e20 commit aa5e579

File tree

10 files changed

+259
-1
lines changed

10 files changed

+259
-1
lines changed

springboot-examples/springboot-restapi-testing-all-layers/src/test/java/com/hellokoding/springboot/restful/product/ProductAPITest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
@RunWith(SpringRunner.class)
2626
@WebMvcTest
2727
public class ProductAPITest {
28-
2928
@Autowired
3029
private MockMvc mockMvc;
3130

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM maven:3.5-jdk-8
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: '3'
2+
services:
3+
hk-mysql:
4+
container_name: hk-mysql
5+
image: mysql/mysql-server:5.7
6+
environment:
7+
MYSQL_DATABASE: test
8+
MYSQL_ROOT_PASSWORD: hellokoding
9+
MYSQL_ROOT_HOST: '%'
10+
ports:
11+
- "3306:3306"
12+
restart: always
13+
14+
hk-app:
15+
build: .
16+
volumes:
17+
- .:/app
18+
- ~/.m2:/root/.m2
19+
working_dir: /app
20+
ports:
21+
- 8080:8080
22+
command: mvn clean spring-boot:run
23+
depends_on:
24+
- hk-mysql
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.hellokoding.springboot</groupId>
8+
<artifactId>springboot-restfulapi</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.1.4.RELEASE</version>
15+
</parent>
16+
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<java.version>1.8</java.version>
20+
<org.apache.maven.plugins.version>3.6.0</org.apache.maven.plugins.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>mysql</groupId>
34+
<artifactId>mysql-connector-java</artifactId>
35+
<scope>runtime</scope>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.hellokoding.springboot.restful;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.hellokoding.springboot.restful.product;
2+
3+
import org.hibernate.annotations.CreationTimestamp;
4+
import org.hibernate.annotations.UpdateTimestamp;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
import java.math.BigDecimal;
11+
import java.util.Date;
12+
13+
@Entity
14+
public class Product {
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.IDENTITY)
17+
private Long id;
18+
19+
private String name;
20+
21+
private String description;
22+
23+
private BigDecimal price;
24+
25+
@CreationTimestamp
26+
private Date createdAt;
27+
28+
@UpdateTimestamp
29+
private Date updatedAt;
30+
31+
public Long getId() {
32+
return id;
33+
}
34+
35+
public void setId(Long id) {
36+
this.id = id;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
public String getDescription() {
48+
return description;
49+
}
50+
51+
public void setDescription(String description) {
52+
this.description = description;
53+
}
54+
55+
public BigDecimal getPrice() {
56+
return price;
57+
}
58+
59+
public void setPrice(BigDecimal price) {
60+
this.price = price;
61+
}
62+
63+
public Date getCreatedAt() {
64+
return createdAt;
65+
}
66+
67+
public void setCreatedAt(Date createdAt) {
68+
this.createdAt = createdAt;
69+
}
70+
71+
public Date getUpdatedAt() {
72+
return updatedAt;
73+
}
74+
75+
public void setUpdatedAt(Date updatedAt) {
76+
this.updatedAt = updatedAt;
77+
}
78+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.hellokoding.springboot.restful.product;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
12+
@RestController
13+
@RequestMapping("/api/v1/products")
14+
public class ProductAPI {
15+
private final ProductService productService;
16+
17+
@Autowired
18+
public ProductAPI(ProductService productService) {
19+
this.productService = productService;
20+
}
21+
22+
@GetMapping
23+
public ResponseEntity<List<Product>> findAll() {
24+
return ResponseEntity.ok(productService.findAll());
25+
}
26+
27+
@GetMapping("/{id}")
28+
public ResponseEntity<Product> findById(@PathVariable Long id) {
29+
Optional<Product> product = productService.findById(id);
30+
31+
return ResponseEntity.ok(productService.findById(id).get());
32+
}
33+
34+
@PostMapping
35+
public ResponseEntity<Product> create(Product product) {
36+
return ResponseEntity.status(HttpStatus.CREATED).body(productService.save(product));
37+
}
38+
39+
@PutMapping("/{id}")
40+
public ResponseEntity<Product> update(@PathVariable Long id, @RequestBody Product product) {
41+
return ResponseEntity.accepted().body(productService.save(product));
42+
}
43+
44+
@DeleteMapping("/{id}")
45+
public ResponseEntity delete(@PathVariable Long id) {
46+
productService.deleteById(id);
47+
48+
return ResponseEntity.accepted().build();
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.hellokoding.springboot.restful.product;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface ProductRespository extends JpaRepository<Product, Long> {
6+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.hellokoding.springboot.restful.product;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
@Service
10+
public class ProductService {
11+
private final ProductRespository productRespository;
12+
13+
@Autowired
14+
public ProductService(ProductRespository productRespository) {
15+
this.productRespository = productRespository;
16+
}
17+
18+
public List<Product> findAll() {
19+
return productRespository.findAll();
20+
}
21+
22+
public Optional<Product> findById(Long id) {
23+
return productRespository.findById(id);
24+
}
25+
26+
public Product save(Product stock) {
27+
return productRespository.save(stock);
28+
}
29+
30+
public void deleteById(Long id) {
31+
productRespository.deleteById(id);
32+
}
33+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring.datasource.url=jdbc:mysql://hk-mysql:3306/test?useSSL=false
2+
spring.datasource.username=root
3+
spring.datasource.password=hellokoding
4+
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
5+
6+
spring.jpa.hibernate.ddl-auto=create
7+
spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialect
8+
spring.jpa.generate-ddl=true
9+
spring.jpa.show-sql=true

0 commit comments

Comments
 (0)