Java高并发秒杀Api-web 层

章节目录

  • 前端交互设计
  • Restful
  • Spring MVC理论
  • 整合配置MVC框架
  • bootstrap+jquery
  • 福利彩蛋

前端交互设计

前端交互设计
详情页流程逻辑

Restful接口

什么是Restful

  • Restful 是接口设计规范
  • 一种优雅的URI表达方式
  • 资源的状态和状态转移
    获取的是资源的状态,删除的时候,其实是更改资源的状态 put、post、delete

Restful示例

image.png
image.png

不同的请求方法代表着对资源的不同操作类型。操作类型应与资源的接下来发生的状态状态转移对应起来

GET 请求是幂等性的

URL设计

url设计

秒杀Api设计

Spring MVC理论

理论部分
围绕handler开发

image.png

Spring mvc 的运行流程

image.png

解释:
用户请求->前端控制器->寻找对应处理请求的handler,默认为DefaultAnnotationHandlerMapping->DefaultAnnotationHandlerAdapter->寻找处理请求的Controller->请求生成ModelAndView至中央控制器->中央控制器将数据转发给InternalResourceViewResolver去解析->解析完成的数据及页面结构,组合后,被生成资源,返回给用户。

HTTP请求映射的原理

image.png

默认使用DefaultAnnotationHandlerMapping->handler方法

注解映射技巧

  • @RequestMapping 注解
    (1) 支持标准标准的URL
    (2) Ant风格URL
    (3) 带{xxx}占位符的URL


    注解映射技巧
  • 请求方法细节处理
    (1) 请求参数绑定
    (2) 请求方式限制
    (3) 请求转发与重定向
    (4) 数据模型赋值
    (5) 返回json数据
    (6) cookie的访问

请求参数绑定、重定向与转发、model数据绑定
返回json数据
cookie 数据

整合配置Spring MVC配置文件

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1"
         metadata-complete="true">

    <!-- 修改servlet版本为3.1 -->
    <!-- 配置DispatcherServlet-->
    <servlet>
        <servlet-name>seckill-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置SpringMVC需要加载的配置文件
            Spring-dao.xml, spring-service.xml, spring-web.xml
            MyBatis -> Spring -> SpringMVC
        -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-*.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>seckill-dispatcher</servlet-name>
        <!-- 默认匹配所有的请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Spring MVC 配置 spring-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--配置Spring MVC-->
    <!--1:开启Spring MVC注解模式-->
    <!--简化配置
       (1):自动注册DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter url->handler 映射
       (2):默认提供一系列功能、数据绑定、数字与日期format @NumberFormat @DateTimeFormat xml json 默认读写支持
    -->
    <mvc:annotation-driven/>

    <!--servlet-mapping 映射路径 /-->
    <!--静态资源配置,默认servlet配置-->
    <!--
      1:加入对静态资源的处理:js、png
      2:允许使用"/"作为映射
    -->
    <mvc:default-servlet-handler/>

    <!--配置jsp  显示 view Resolver-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--扫描web相关的bean-->
    <context:component-scan base-package="org.seckill.web"/>
</beans>

**SecKillController **

package org.seckill.web;

import org.seckill.domain.SecKill;
import org.seckill.dto.Exposer;
import org.seckill.dto.SecKillResult;
import org.seckill.service.SecKillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/seckill") //url:/模块/资源/{id}/细分
public class SecKillController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SecKillService secKillService;

    /**
     * 获取秒杀商品list
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        //1.获取列表页面
        List<SecKill> secKillList = secKillService.getSecKillList();

        model.addAttribute("list", secKillList);

        //2.返回页面并携带model 数据list.jsp + model = ModelAndView
        return "list";
    }

    /**
     * 接收参数、参数验证、业务逻辑调用并处理
     * 获取秒杀商品详情
     *
     * @param model
     * @param secKillId
     * @return
     */
    @RequestMapping(value = "/{secKillId}/detial", method = RequestMethod.GET)
    public String detial(Model model, @PathVariable("secKillId") Long secKillId) {
        if (secKillId == null) {//请求重定向到list页面
            return "redirect:/seckill/list";
        }

        SecKill secKill = secKillService.getSecKillById(secKillId);
        if (secKill == null) {
            return "forward:/seckill/list";
        }

        model.addAttribute("seckill", secKill);

        return "detial";
    }

    /**
     * ajax接口-返回类型 json
     * 获取秒杀曝露地址
     *
     * @param secKillId
     */
    @RequestMapping(value = "/{secKillId}/exposer",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"}//告诉浏览器返回数据类型是json
    )
    @ResponseBody
    public SecKillResult<Exposer> /*TODO*/ exposer(@PathVariable("secKillId") Long secKillId) {
        SecKillResult<Exposer> result;
        try {
            Exposer exposer = secKillService.exportSecKillUrl(secKillId);//返回秒杀地址
            result = new SecKillResult<Exposer>(true, exposer);
        } catch (Exception e) {
            logger.error(e.getMessage());
            result = new SecKillResult<Exposer>(false, e.getMessage());
        }

        return result;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,525评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,203评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,862评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,728评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,743评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,590评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,330评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,244评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,693评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,885评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,001评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,723评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,343评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,919评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,042评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,191评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,955评论 2 355

推荐阅读更多精彩内容