123.【SpringBoot 源码刨析B】(五)

简介: 123.【SpringBoot 源码刨析B】
(2).普通参数与基本注解
(1.1)、注解:
注解通常有三个源码属性。
name="" 接受的名字?
value="" 接受的名字?
name和value因为互相为别名所以两者都一样的作用,用谁都一样
required ="" 是否必须接受到值?
1. 主要用于Rest风格传参方面。
@PathVariable: 假如说方法里面有一个 Map<String,String>的参数,那么SPringBoot会自动帮助我们以键值对的方式进行自动收集里面的数据。
2. 主要用于获取请求头
@RequestHeader: 加入方法里面有一个Map<String,String>的参数,那么所有的请求头都会放进去。
3.获取HttpRequest设置的值(这是一个系列的)
@RequestAttribute: 主要是获取setAttribute(k,v)的值
4.获取非REST风格传参的参数
@RequestParam: 假如方法里面有一个Map<String,String>的参数,那么获取到的参数都会放在这里
5. 矩阵注解
@MatrixVariable: 假如方法里面有一个
6.获取Cookie的值
@CookieValue: 假如方法里面有一个Cookie对象,那么获取到的cookie的名字和值都会放在这里。
7. 获取POST的非REST值
@RequestBody: 假如是POST请求,那么会获取到POST的非REST的参数值。
8. 重定向注解
@RedirectAttributes : 通过addAtribute()方法给重定向进行自动拼接

  1. 非矩阵注解

非@RequestAttribute的后端代码

package com.jsxs.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Author Jsxs
 * @Date 2023/7/3 16:14
 * @PackageName:com.jsxs.controller
 * @ClassName: ParamterTestController
 * @Description: TODO
 * @Version 1.0
 */
@Controller
@ResponseBody
public class ParameterTestController {
    //    1. @PathVariable 注解 : 获取REST的值
    @GetMapping("/car/{id}/owner/{username}/{df}")
    public Map<String, Object> getCar1(@PathVariable("id") Integer id,
                                       @PathVariable("username") String username,
                                       @PathVariable("df") String df,
                                       @PathVariable Map<String, String> mp) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", username);
        map.put("df", df);
        map.put("mp", mp);
        return map;
    }
    //    2.@RequestHeader 注解 : 获取请求头的信息
    @GetMapping("/car/{id}/owner/{username}")
    public Map<String, Object> getCar2(@PathVariable("id") Integer id,
                                       @PathVariable("username") String username,
                                       @PathVariable Map<String, String> mp,
                                       @RequestHeader("User-Agent") String userAgent,
                                       @RequestHeader Map<String, String> header) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", username);
        map.put("mp", mp);
        map.put("userAgent", userAgent);
        map.put("header", header);
        return map;
    }
    //    3. @RequestParam 注解 : 获取GET拼接的提交的值
    @GetMapping("/car/{id}/owner3/{username}")
    public Map<String, Object> getCar3(@PathVariable("id") Integer id,
                                       @PathVariable("username") String username,
                                       @PathVariable Map<String, String> mp,
                                       @RequestHeader("User-Agent") String userAgent,
                                       @RequestHeader Map<String, String> header,
                                       @RequestParam("age") Integer age,
                                       @RequestParam("inters") List<String> inters,
                                       @RequestParam Map<String, String> params
    ) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", username);
        map.put("mp", mp);
        map.put("userAgent", userAgent);
        map.put("header", header);
        map.put("age", age);
        map.put("inters", inters);
        map.put("params", params);
        return map;
    }
    //      4. @CookieValue :获取指定的cookie值
    @GetMapping("/car/{id}/owner4/{username}")
    public Map<String, Object> getCar4(@PathVariable("id") Integer id,
                                       @PathVariable("username") String username,
                                       @PathVariable Map<String, String> mp,
                                       @RequestHeader("User-Agent") String userAgent,
                                       @RequestHeader Map<String, String> header,
                                       @RequestParam("age") Integer age,
                                       @RequestParam("inters") List<String> inters,
                                       @RequestParam Map<String, String> params,
                                       @CookieValue("Idea-d024f886") String cookie_ga,
                                       @CookieValue("Idea-d024f886") Cookie cookie
    ) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", username);
        map.put("mp", mp);
        map.put("userAgent", userAgent);
        map.put("header", header);
        map.put("age", age);
        map.put("inters", inters);
        map.put("params", params);
        map.put("cookie_ga", cookie_ga);
        System.out.println("通过注解获取到Idea-d024f886的cookie对象为:" + cookie);
        return map;
    }
    //      5.@RequestBody 注解:  主要作用获得POST提交的数据
    @PostMapping("/save")
    public Map postMethod(@RequestBody String content) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        return map;
    }
}

@RequestAttribute的后端代码

package com.jsxs.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
/**
 * @Author Jsxs
 * @Date 2023/7/3 18:34
 * @PackageName:com.jsxs.controller
 * @ClassName: RequestController
 * @Description: TODO  1.HttpServletRequest 经过一次转发就失效(利用这个重定向是获取不到值的,因为重定向属于第二次转发了) 2.HttpSession 浏览器关闭失效 3. HttpServletContent 服务器关闭
 * @Version 1.0
 */
@Controller
public class RequestController {
    @Resource
    HttpSession session;
    @GetMapping("/goto")
    public String goToPage(HttpServletRequest httpServletRequest){
        httpServletRequest.setAttribute("info","转发成功了..");
        httpServletRequest.setAttribute("msg","jsxs");
        // 切记如果我们没有使用thymeleaf的话,是不能实现前后端跳转的。 (下面会显示找不到MVC)
        return "forward:/success";   // 转发到 /success 请求;并不是转发到success页面的。
    }
    @ResponseBody
    @GetMapping("/success")
    public Map<String, Object> SuccessPage(HttpServletRequest httpServletRequest, @RequestAttribute("msg") String name){
        String info = (String)httpServletRequest.getAttribute("info");
        System.out.println(info+" "+name);
        HashMap<String, Object> map = new HashMap<>();
        map.put("info_代码方式",info);
        map.put("msg_注解方式",name);
        return map;
    }
}

index.html 前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>你好</h1>
<form action="/user" method="post">
    <button type="submit">Post方式进行跳转</button>
</form>
<form action="/user" method="get">
    <button type="submit">Get方式进行跳转</button>
</form>
<form action="/user" method="post">
    <input name="aaaa" type="hidden" value="DELETE">
    <button type="submit">DELETE方式进行跳转</button>
</form>
<form action="/user" method="post">
    <input name="aaaa" type="hidden" value="PUT">
    <button type="submit">PUT方式进行跳转</button>
</form>
<ul>
    <a href="http://localhost:8080/car/1/owner/jsxs/df"> @PathVariable注解</a>
</ul>
<ul>
    <a href="http://localhost:8080/car/1/owner/jsxs"> @PathVariable注解 + @RequestHeader</a>
</ul>
<ul>
    <a href="http://localhost:8080/car/1/owner3/jsxs?age=18&inters=basketball&inters=game"> @PathVariable注解 +
        @RequestHeader + @RequestParam</a>
</ul>
<ul>
    <a href="http://localhost:8080/car/1/owner4/jsxs?age=18&inters=basketball&inters=game"> @PathVariable注解 +
        @RequestHeader + @RequestParam + @CookieValue</a>
</ul>
<form method="post" action="/save">
    <input value="liming" name="user" type="hidden">
    <input value="123456" name="password" type="hidden">
    <button type="submit">@RequestBody注解</button>
</form>
<ul>
    <a href="http://localhost:8080/goto">@RequestAttribute注解(这是一个系列的中的其中一个)</a>
</ul>
</body>
</html>

  1. 矩阵注解

我们要开启矩阵注解的支持,因为SpringBoot默认是关闭的

WebMvcAutoConfiguration类下 -> configurePathMatch()方法体中 -> UrlPathHelper类中 -> removeSemicolonContent属性默认为(true)

假如我们访问矩阵路径的时候报错400 (请求异常) 就是我们的配置矩阵注解的支持。

1. 第一种配置方式 : 继承WebMvcConfigurer 接口 + 实现configurePathMatch方法

2.第二种配置方式: @Bean WebMvcConfigurer接口并重写里面的方法

3.因为JDK1.8支持接口默认方法所以我们不必要重写接口中所有的方法。

配置文件:

package com.jsxs.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;
/**
 * @Author Jsxs
 * @Date 2023/7/3 11:13
 * @PackageName:com.jsxs.config
 * @ClassName: WebConfig
 * @Description: TODO
 * @Version 1.0
 */
@Configuration(proxyBeanMethods = false)
// 第一种方式 @Configuration + 实现WebMvcConfigurer接口 (因为JDK8允许接口的默认方法和默认实现所以我们不需要将所有方法全部重写)
// 第二种方式: @Configuration +@Bean 重新注入我们的组件
public class WebConfig /*implements WebMvcConfigurer */{
    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        hiddenHttpMethodFilter.setMethodParam("aaaa");
        return hiddenHttpMethodFilter;
    }
//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
//        UrlPathHelper helper = new UrlPathHelper();
//        helper.setRemoveSemicolonContent(false);
//        configurer.setUrlPathHelper(helper);
//    }
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer(){
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper helper = new UrlPathHelper();
                helper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(helper);
            }
        };
    }
}




相关文章
|
8月前
|
JavaScript 前端开发 Java
制造业ERP源码,工厂ERP管理系统,前端框架:Vue,后端框架:SpringBoot
这是一套基于SpringBoot+Vue技术栈开发的ERP企业管理系统,采用Java语言与vscode工具。系统涵盖采购/销售、出入库、生产、品质管理等功能,整合客户与供应商数据,支持在线协同和业务全流程管控。同时提供主数据管理、权限控制、工作流审批、报表自定义及打印、在线报表开发和自定义表单功能,助力企业实现高效自动化管理,并通过UniAPP实现移动端支持,满足多场景应用需求。
738 1
|
9月前
|
前端开发 Java 关系型数据库
基于Java+Springboot+Vue开发的鲜花商城管理系统源码+运行
基于Java+Springboot+Vue开发的鲜花商城管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的鲜花商城管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。技术学习共同进步
572 7
|
9月前
|
前端开发 Java 物联网
智慧班牌源码,采用Java + Spring Boot后端框架,搭配Vue2前端技术,支持SaaS云部署
智慧班牌系统是一款基于信息化与物联网技术的校园管理工具,集成电子屏显示、人脸识别及数据交互功能,实现班级信息展示、智能考勤与家校互通。系统采用Java + Spring Boot后端框架,搭配Vue2前端技术,支持SaaS云部署与私有化定制。核心功能涵盖信息发布、考勤管理、教务处理及数据分析,助力校园文化建设与教学优化。其综合性和可扩展性有效打破数据孤岛,提升交互体验并降低管理成本,适用于日常教学、考试管理和应急场景,为智慧校园建设提供全面解决方案。
550 70
|
8月前
|
供应链 JavaScript BI
ERP系统源码,基于SpringBoot+Vue+ElementUI+UniAPP开发
这是一款专为小微企业打造的 SaaS ERP 管理系统,基于 SpringBoot+Vue+ElementUI+UniAPP 技术栈开发,帮助企业轻松上云。系统覆盖进销存、采购、销售、生产、财务、品质、OA 办公及 CRM 等核心功能,业务流程清晰且操作简便。支持二次开发与商用,提供自定义界面、审批流配置及灵活报表设计,助力企业高效管理与数字化转型。
682 2
ERP系统源码,基于SpringBoot+Vue+ElementUI+UniAPP开发
|
7月前
|
机器学习/深度学习 数据采集 人机交互
springboot+redis互联网医院智能导诊系统源码,基于医疗大模型、知识图谱、人机交互方式实现
智能导诊系统基于医疗大模型、知识图谱与人机交互技术,解决患者“知症不知病”“挂错号”等问题。通过多模态交互(语音、文字、图片等)收集病情信息,结合医学知识图谱和深度推理,实现精准的科室推荐和分级诊疗引导。系统支持基于规则模板和数据模型两种开发原理:前者依赖人工设定症状-科室规则,后者通过机器学习或深度学习分析问诊数据。其特点包括快速病情收集、智能病症关联推理、最佳就医推荐、分级导流以及与院内平台联动,提升患者就诊效率和服务体验。技术架构采用 SpringBoot+Redis+MyBatis Plus+MySQL+RocketMQ,确保高效稳定运行。
520 0
|
10月前
|
小程序 Java 关系型数据库
weixin117新闻资讯系统设计+springboot(文档+源码)_kaic
本文介绍了一款基于微信小程序的新闻资讯系统,涵盖其开发全过程。该系统采用Java的SSM框架进行后台管理开发,使用MySQL作为本地数据库,并借助微信开发者工具确保稳定性。管理员可通过个人中心、用户管理等功能模块实现高效管理,而用户则能注册登录并查看新闻与视频内容。系统设计注重可行性分析(技术、经济、操作),强调安全性与数据完整性,界面简洁易用,功能全面,极大提升了信息管理效率及用户体验。关键词包括基于微信小程序的新闻资讯系统、SSM框架和MYSQL数据库。
|
11月前
|
小程序 JavaScript Java
基于SpringBoot的智慧停车场微信小程序源码分享
智慧停车场微信小程序主要包含管理端和小程序端。管理端包括停车场管理,公告信息管理,用户信息管理,预定信息管理,用户反馈管理等功能。小程序端包括登录注册,预约停车位,停车导航,停车缴费,用户信息,车辆信息,钱包充值,意见反馈等功能。
469 5
基于SpringBoot的智慧停车场微信小程序源码分享
|
12月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue实现的留守儿童爱心网站设计与实现(计算机毕设项目实战+源码+文档)
博主是一位全网粉丝超过100万的CSDN特邀作者、博客专家,专注于Java、Python、PHP等技术领域。提供SpringBoot、Vue、HTML、Uniapp、PHP、Python、NodeJS、爬虫、数据可视化等技术服务,涵盖免费选题、功能设计、开题报告、论文辅导、答辩PPT等。系统采用SpringBoot后端框架和Vue前端框架,确保高效开发与良好用户体验。所有代码由博主亲自开发,并提供全程录音录屏讲解服务,保障学习效果。欢迎点赞、收藏、关注、评论,获取更多精品案例源码。
|
12月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue实现的家政服务管理平台设计与实现(计算机毕设项目实战+源码+文档)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
|
12月前
|
JavaScript 搜索推荐 Java
基于SpringBoot+Vue实现的家乡特色推荐系统设计与实现(源码+文档+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!