接着上一篇文章:SpringBoot实现Java高并发之Service层开发,今天我们开始讲SpringBoot实现Java高并发秒杀系统之Web层开发。
Web层即Controller层,当然我们所说的都是在基于Spring框架的系统上而言的,传统的SSH项目中,与页面进行交互的是struts框架,但struts框架很繁琐,后来就被SpringMVC给顶替了,SpringMVC框架在与页面的交互上提供了更加便捷的方式,MVC的设计模式也是当前非常流行的一种设计模式。这次我们针对秒杀系统讲解一下秒杀系统需要和页面交互的操作和数据都涉及哪些?
本项目的源码请参看:springboot-seckill 如果觉得不错可以star一下哦(#
.#)
本项目一共分为四个模块来讲解,具体的开发教程请看我的博客文章:
-
SpringBoot实现Java高并发秒杀系统之DAO层开发(一)
-
SpringBoot实现Java高并发秒杀系统之Service层开发(二)
-
SpringBoot实现Java高并发秒杀系统之Web层开发(三)
-
SpringBoot实现Java高并发秒杀系统之并发优化(四)
首先如果你对SpringBoot项目还是不清楚的话,我依然推荐你看一下我的这个项目:优雅的入门SpringBoot2.x,整合Mybatis实现CRUD
前端交互流程设计
编写Controller就是要搞清楚:1.页面需要什么数据?2.页面将返回给Controller什么数据?3.Controller应该返回给页面什么数据?
带着这些问题我们看一下秒杀详情页流程逻辑(不再讲基本的findById和findAll()方法):
因为整个秒杀系统中最核心的业务就是:1.减库存;2.查询订单明细。我们看一下Controller层的源码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 1@Controller
2@RequestMapping("/seckill")
3public class SeckillController {
4
5 @Autowired
6 private SeckillService seckillService;
7
8 private final Logger logger = LoggerFactory.getLogger(this.getClass());
9
10 @ResponseBody
11 @RequestMapping("/findAll")
12 public List<Seckill> findAll() {
13 return seckillService.findAll();
14 }
15
16 @ResponseBody
17 @RequestMapping("/findById")
18 public Seckill findById(@RequestParam("id") Long id) {
19 return seckillService.findById(id);
20 }
21
22 @RequestMapping("/{seckillId}/detail")
23 public String detail(@PathVariable("seckillId") Long seckillId, Model model) {
24 if (seckillId == null) {
25 return "page/seckill";
26 }
27 Seckill seckill = seckillService.findById(seckillId);
28 model.addAttribute("seckill", seckill);
29 if (seckill == null) {
30 return "page/seckill";
31 }
32 return "page/seckill_detail";
33 }
34
35 @ResponseBody
36 @RequestMapping(value = "/{seckillId}/exposer",
37 method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
38 public SeckillResult<Exposer> exposer(@PathVariable("seckillId") Long seckillId) {
39 SeckillResult<Exposer> result;
40 try {
41 Exposer exposer = seckillService.exportSeckillUrl(seckillId);
42 result = new SeckillResult<Exposer>(true, exposer);
43 } catch (Exception e) {
44 logger.error(e.getMessage(), e);
45 result = new SeckillResult<Exposer>(false, e.getMessage());
46 }
47 return result;
48 }
49
50 @RequestMapping(value = "/{seckillId}/{md5}/execution",
51 method = RequestMethod.POST,
52 produces = {"application/json;charset=UTF-8"})
53 @ResponseBody
54 public SeckillResult<SeckillExecution> execute(@PathVariable("seckillId") Long seckillId,
55 @PathVariable("md5") String md5,
56 @RequestParam("money") BigDecimal money,
57 @CookieValue(value = "killPhone", required = false) Long userPhone) {
58 if (userPhone == null) {
59 return new SeckillResult<SeckillExecution>(false, "未注册");
60 }
61 try {
62 SeckillExecution execution = seckillService.executeSeckill(seckillId, money, userPhone, md5);
63 return new SeckillResult<SeckillExecution>(true, execution);
64 } catch (RepeatKillException e) {
65 SeckillExecution seckillExecution = new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL);
66 return new SeckillResult<SeckillExecution>(true, seckillExecution);
67 } catch (SeckillCloseException e) {
68 SeckillExecution seckillExecution = new SeckillExecution(seckillId, SeckillStatEnum.END);
69 return new SeckillResult<SeckillExecution>(true, seckillExecution);
70 } catch (SeckillException e) {
71 SeckillExecution seckillExecution = new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR);
72 return new SeckillResult<SeckillExecution>(true, seckillExecution);
73 }
74 }
75
76 @ResponseBody
77 @GetMapping(value = "/time/now")
78 public SeckillResult<Long> time() {
79 Date now = new Date();
80 return new SeckillResult(true, now.getTime());
81 }
82}
83
84
下面我以问答的形式讲解一下Controller层方法的定义:
1.@ResponseBody和@RestController注解分别有什么作用?
-
@ResponseBody注解标识的方法,Spring会将此方法return的数据转换成JSON格式且不会被Spring视图解析器所扫描到,也就是此方法永不可能返回一个视图页面。且这个注解只能用在方法体上,不能用在类上。
-
@RestController注解标识的类,Spring会将其下的所有方法return的数据都转换成JSON格式且不会被Spring视图解析器扫描到,也就是此类下面的所有方法都不可能返回一个视图页面。且这个注解只能用在类上,不能用在方法体上。
2.@RequestMapping中{xx}的语法是什么?@PathVariable注解的用处是什么?
Spring框架很早就支持开发REST资源。也是就是现在我们定义的RESTful URL,在Spring框架上支持的尤为完美,我们可以在Controller中定义这样一个URL映射地址:/{id}/detail,他是合理的RESTful URL定义方式。
这种URL的特点:URL地址由动态的数据拼接组成的,而不是将所有的资源全部映射到一个路径下,比如:/article/detail。
这种URL结构的优势:我们能很容易从URL地址上判断出该地址所展示的页面是什么?比如:/1/detail就可能表示ID为1的文章的详情页,看起来设计的很清晰。
这种URL如何进行交互:我们定义了/{id}/detail这样一个URL映射地址,其对应的映射方法上就应该添加@PathVariable注解标识,如:@PathVariable("id") Long idSpring就能装配前端传递的URL中指定位置的数据并赋值给id这个参数。比如前端调用后端接口:localhost:8080/seckill/1/detail,后端存在一个映射方法:@RequestMapping("/{id}/detail"),这样就能刚好匹配上这个URL映射地址。
所以我们看一下秒杀系统的RESTful URL设计:
3.为什么要单独写一个接口用来获取当前系统时间?
由于我们开发的系统肯定不是给自己用的,我们的用户可能处于不同的时区,他们的当前系统时间也是不同的,所以我们写一个通用的时间规范:就是当前服务器的时间。
4.SeckillResult是什么?
在前面我们将Service层系统开发的时候就手动创建了很多类来封装一些通用的结果信息。而对于Controller层也会返回很多结果数据,比如传入的URL中id值为null,那么就没必要继续向下请求,而是直接给页面返回false信息。
于是我们创建:SeckillResult.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1public class SeckillResult<T> {
2
3 private boolean success;
4
5 private T data;
6
7 private String error;
8
9 public SeckillResult(boolean success, T data) {
10 this.success = success;
11 this.data = data;
12 }
13
14 public SeckillResult(boolean success, String error) {
15 this.success = success;
16 this.error = error;
17 }
18}
19
20
泛型T表示可以代表不同类型的对象。这是泛型类应用很广泛的一个特性,我们调用SeckillResult类,将其中的T用什么替换那么T就表示这个替换的对象类型。
页面设计
用了哪些技术?
- HTML页面,用Bootstrap绘制。
- Thymeleaf模板引擎渲染HTML页面,使得HTML页面拥有类似JSP页面一样功能。
- JS方面使用原生的JQuery。
我们来看一下前端的页面设计:
本项目使用Cookie存储用户手机号的方式模拟用户登录功能,实际上没有与后端交互的操作。如果用户没有登录就打开了商品详情页会直接弹出一个手机号登录框提醒用户登录,且没有登录时无法关闭登录框的。
具体的源码请看:GitHub
思考
在从JSP页面转换到HTML页面的时候我常会遇到这么一个问题:前端如何取出来后端查询到的数据?
在之前我们写的JSP页面中,可以通过将后端查询到的数据放进request,session域对象中,JSP页面可以直接调用Java中域对象的数据,甚至可以通过EL表达式(${})来直接获取参数,但是这种方法有一个弊端:Controller必须是返回一个视图,这样才能在此视图中获取存进域对象中的数据。
而我们现在都开始用HTML页面,也无法从域对象中取出数据该怎么办呢?我这里提供两个思路:
-
1.像本项目中一样,前端使用Thymeleaf模板引擎渲染页面,那么Thymeleaf内置很多方法如同JSP页面的EL表达式。Thymeleaf在HTML中取出域对象数据使用:<span th:text="${xx}">;在JS中取出域对象数据:var v = [[${xx}]](当然都必须是在HTML页面中,在外部JS文件中是得不到数据的)。
-
2.使用原生js提供的location对象,我们先看一下URL的组成结构:
详细介绍请看:博文
举个栗子
1
2
3
4
5
6
7
8
9
10 1function QueryUrl(name){
2 var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
3 var r = window.location.search.substr(1).match(reg);
4 if(r!=null)return unescape(r[2]); return null;
5}
6
7// 调用方法
8alert(QueryUrl("参数名1"));
9
10
交流
如果大家有兴趣,欢迎大家加入我的Java交流群:671017003 ,一起交流学习Java技术。博主目前一直在自学JAVA中,技术有限,如果可以,会尽力给大家提供一些帮助,或是一些学习方法,当然群里的大佬都会积极给新手答疑的。所以,别犹豫,快来加入我们吧!
联系
If you have some questions after you see this article, you can contact me or you can find some info by clicking these links.
- Blog@TyCoding’s blog
- GitHub@TyCoding
- ZhiHu@TyCoding