forked from digitalsonic/geektime-spring-family
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request digitalsonic#9 from digitalsonic/master
upload 第六章PDF和demo
- Loading branch information
Showing
184 changed files
with
5,217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
HELP.md | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
/build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>geektime.spring.springbucks</groupId> | ||
<artifactId>waiter-service</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>waiter-service</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-cache</artifactId> | ||
</dependency> | ||
<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.joda</groupId> | ||
<artifactId>joda-money</artifactId> | ||
<version>1.0.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jadira.usertype</groupId> | ||
<artifactId>usertype.core</artifactId> | ||
<version>6.0.1.GA</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<optional>true</optional> | ||
</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> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
17 changes: 17 additions & 0 deletions
17
...cache-demo/src/main/java/geektime/spring/springbucks/waiter/WaiterServiceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package geektime.spring.springbucks.waiter; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
|
||
@SpringBootApplication | ||
@EnableJpaRepositories | ||
@EnableCaching | ||
public class WaiterServiceApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(WaiterServiceApplication.class, args); | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
...he-demo/src/main/java/geektime/spring/springbucks/waiter/controller/CoffeeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package geektime.spring.springbucks.waiter.controller; | ||
|
||
import geektime.spring.springbucks.waiter.controller.request.NewCoffeeRequest; | ||
import geektime.spring.springbucks.waiter.model.Coffee; | ||
import geektime.spring.springbucks.waiter.service.CoffeeService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.math.NumberUtils; | ||
import org.apache.tomcat.util.http.fileupload.IOUtils; | ||
import org.joda.money.CurrencyUnit; | ||
import org.joda.money.Money; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.CacheControl; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import javax.validation.Valid; | ||
import java.io.BufferedInputStream; | ||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOError; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Controller | ||
@RequestMapping("/coffee") | ||
@Slf4j | ||
public class CoffeeController { | ||
@Autowired | ||
private CoffeeService coffeeService; | ||
|
||
@PostMapping(path = "/", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) | ||
@ResponseBody | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public Coffee addCoffee(@Valid NewCoffeeRequest newCoffee, | ||
BindingResult result) { | ||
if (result.hasErrors()) { | ||
// 这里先简单处理一下,后续讲到异常处理时会改 | ||
log.warn("Binding Errors: {}", result); | ||
return null; | ||
} | ||
return coffeeService.saveCoffee(newCoffee.getName(), newCoffee.getPrice()); | ||
} | ||
|
||
// @PostMapping(path = "/", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) | ||
// @ResponseBody | ||
// @ResponseStatus(HttpStatus.CREATED) | ||
// public Coffee addCoffeeWithoutBindingResult(@Valid NewCoffeeRequest newCoffee) { | ||
// return coffeeService.saveCoffee(newCoffee.getName(), newCoffee.getPrice()); | ||
// } | ||
|
||
@PostMapping(path = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | ||
@ResponseBody | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public List<Coffee> batchAddCoffee(@RequestParam("file") MultipartFile file) { | ||
List<Coffee> coffees = new ArrayList<>(); | ||
if (!file.isEmpty()) { | ||
BufferedReader reader = null; | ||
try { | ||
reader = new BufferedReader( | ||
new InputStreamReader(file.getInputStream())); | ||
String str; | ||
while ((str = reader.readLine()) != null) { | ||
String[] arr = StringUtils.split(str, " "); | ||
if (arr != null && arr.length == 2) { | ||
coffees.add(coffeeService.saveCoffee(arr[0], | ||
Money.of(CurrencyUnit.of("CNY"), | ||
NumberUtils.createBigDecimal(arr[1])))); | ||
} | ||
} | ||
} catch (IOException e) { | ||
log.error("exception", e); | ||
} finally { | ||
IOUtils.closeQuietly(reader); | ||
} | ||
} | ||
return coffees; | ||
} | ||
|
||
@GetMapping(path = "/", params = "!name") | ||
@ResponseBody | ||
public List<Coffee> getAll() { | ||
return coffeeService.getAllCoffee(); | ||
} | ||
|
||
@RequestMapping(path = "/{id}", method = RequestMethod.GET, | ||
produces = MediaType.APPLICATION_JSON_UTF8_VALUE) | ||
@ResponseBody | ||
public ResponseEntity<Coffee> getById(@PathVariable Long id) { | ||
Coffee coffee = coffeeService.getCoffee(id); | ||
return ResponseEntity.ok() | ||
.cacheControl(CacheControl.maxAge(10, TimeUnit.SECONDS)) | ||
.body(coffee); | ||
} | ||
|
||
@GetMapping(path = "/", params = "name") | ||
@ResponseBody | ||
public Coffee getByName(@RequestParam String name) { | ||
return coffeeService.getCoffee(name); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...mo/src/main/java/geektime/spring/springbucks/waiter/controller/CoffeeOrderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package geektime.spring.springbucks.waiter.controller; | ||
|
||
import geektime.spring.springbucks.waiter.controller.request.NewOrderRequest; | ||
import geektime.spring.springbucks.waiter.model.Coffee; | ||
import geektime.spring.springbucks.waiter.model.CoffeeOrder; | ||
import geektime.spring.springbucks.waiter.service.CoffeeOrderService; | ||
import geektime.spring.springbucks.waiter.service.CoffeeService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/order") | ||
@Slf4j | ||
public class CoffeeOrderController { | ||
@Autowired | ||
private CoffeeOrderService orderService; | ||
@Autowired | ||
private CoffeeService coffeeService; | ||
|
||
@GetMapping("/{id}") | ||
public CoffeeOrder getOrder(@PathVariable("id") Long id) { | ||
return orderService.get(id); | ||
} | ||
|
||
@PostMapping(path = "/", consumes = MediaType.APPLICATION_JSON_VALUE, | ||
produces = MediaType.APPLICATION_JSON_UTF8_VALUE) | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public CoffeeOrder create(@RequestBody NewOrderRequest newOrder) { | ||
log.info("Receive new Order {}", newOrder); | ||
Coffee[] coffeeList = coffeeService.getCoffeeByName(newOrder.getItems()) | ||
.toArray(new Coffee[] {}); | ||
return orderService.createOrder(newOrder.getCustomer(), coffeeList); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...src/main/java/geektime/spring/springbucks/waiter/controller/request/NewCoffeeRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package geektime.spring.springbucks.waiter.controller.request; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import org.joda.money.Money; | ||
|
||
import javax.validation.constraints.NotEmpty; | ||
import javax.validation.constraints.NotNull; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
public class NewCoffeeRequest { | ||
@NotEmpty | ||
private String name; | ||
@NotNull | ||
private Money price; | ||
} |
15 changes: 15 additions & 0 deletions
15
.../src/main/java/geektime/spring/springbucks/waiter/controller/request/NewOrderRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package geektime.spring.springbucks.waiter.controller.request; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
public class NewOrderRequest { | ||
private String customer; | ||
private List<String> items; | ||
} |
32 changes: 32 additions & 0 deletions
32
Chapter 6/cache-demo/src/main/java/geektime/spring/springbucks/waiter/model/BaseEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package geektime.spring.springbucks.waiter.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
import org.hibernate.annotations.UpdateTimestamp; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.MappedSuperclass; | ||
import java.io.Serializable; | ||
import java.util.Date; | ||
|
||
@MappedSuperclass | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@JsonIgnoreProperties(value = {"hibernateLazyInitializer"}) | ||
public class BaseEntity implements Serializable { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(updatable = false) | ||
@CreationTimestamp | ||
private Date createTime; | ||
@UpdateTimestamp | ||
private Date updateTime; | ||
} |
29 changes: 29 additions & 0 deletions
29
Chapter 6/cache-demo/src/main/java/geektime/spring/springbucks/waiter/model/Coffee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package geektime.spring.springbucks.waiter.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
import org.hibernate.annotations.Type; | ||
import org.joda.money.Money; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Table; | ||
import java.io.Serializable; | ||
|
||
@Entity | ||
@Table(name = "T_COFFEE") | ||
@Builder | ||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
@ToString(callSuper = true) | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Coffee extends BaseEntity implements Serializable { | ||
private String name; | ||
@Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyMinorAmount", | ||
parameters = {@org.hibernate.annotations.Parameter(name = "currencyCode", value = "CNY")}) | ||
private Money price; | ||
} |
Oops, something went wrong.