SpringBoot中关于参数基本注解

学习SpringBoot过程中,参数这几个注解一直傻傻搞不清,今天做一个总结!

1. @PathVariable注解与@RequestParam注解

这两个注解开始搞混了,报了400的错还一直找不到,就放一起,便于对比区分。

@PathVariable:

作用:接收请求路径中占位符的值,举个栗子:

@RequestMapping("/user/{id}")
//请求地址"localhost:8080/user/{id}"

@PathVariable("id")Integer id
//@PathVariable注解就能接收到上面请求路径上传过来的{}中的参数

@RequestParam:

将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)

语法:@RequestParam(value=”参数名”,defaultValue=””)
 
value:参数名
 
defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值

还有一个不怎么用的参数required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。

用法示例:

url:"localhost:8080/user?id="

@RequestParam("id")Integer id
//接收id值

2. @RequestHeader

获取请求头,例如:

@RequestHeader("User-Agent") String userAgent
//获取请求头为"User-Agent"的请求

@RequestHeader Map<String,String> header
//获取所有请求头的请求

3. @ModelAttribute

注释@ModelAttribute的方法会在此Controller每个方法前执行,如下,test方法会在下面两个方法运行前往model中添加。

@Controller
public class TestController{
 
	@ModelAttribute
    public void test(Model model) {
           model.addAttribute("username", lby);
    }
 
    @RequestMapping(value = "/hello1")
    public String helloWorld1() {
           return "hello";
    }
    
    @RequestMapping(value = "/hello2")
    public String helloWorld2() {
           return "hello";
    }

4. @RequestBody

可以参考一下下文:
https://www.cnblogs.com/zly123/p/10853049.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
springboot学习笔记 spring基础 Spring概述 Spring的简史 xml配置 注解配置 java配置 Spring概述 Spring的模块 核心容器CoreContainer Spring-Core Spring-Beans Spring-Context Spring-Context-Support Spring-Expression AOP Spring-AOP Spring-Aspects Messaging Spring-Messaging WEB Spring-Web Spring-Webmvc Spring-WebSocket Spring-Webmvc-Portlet 数据访问/集成(DataAccess/Intefration) Spring-JDBC Spring-TX Spring-ORM Spring-OXM Spring-JMS Spring的生态 Spring Boot Spring XD Spring Cloud Spring Data Spring Integration Spring Batch Spring Security Spring HATEOAS Spring Social Spring AMQP Spring Mobile Spring for Android Spring Web Flow Spring Web Services Spring LDAP Spring Session Spring项目快速搭建 Maven简介 Maven安装 Maven的pom.xml dependencies dependency 变量定义 编译插件 Spring项目的搭建 Spring Tool Suite https://spring.io/tools/sts/all IntelliJ IDEA NetBeans https://netbeans.org/downloads/ Spring基础配置 依赖注入 声明Bean的注解 @Component组件,没有明确的角色 @Service在业务逻辑层(service层) @Repository在数据访问层(dao层) @Controller在展现层(MVC→SpringMVC) 注入Bean的注解 @Autowired:Spring提供的注解 @Inject:JSR-330提供的注解 @Resource:JSR-250提供的注解 Java配置 @Configuration声明当前类是一个配置类 @Bean注解在方法上,声明当前方法的返回值为一个Bean AOP @Aspect 声明是一个切面 拦截规则@After @Before @Around PointCut JoinPoint Spring常用配置 Bean的Scope Singleton Prototype Request Session GlobalSession SpringEL和资源调用 注入普通字符 注入操作系统属性 注入表达式云算结果 注入其他Bean的属性 注入文件内容 注入网址内容 注入属性文件 Bean的初始化和销毁 Java配置方式 注解方式 Profile @Profile 通过设定jvm的spring.profiles.active参数 web项目设置在Servlet的context parameter 事件Application Event 自定义事件,集成ApplicationEvent 定义事件监听器,实现ApplicationListener 使用容器发布事件 Spring高级话题 Spring Aware BeanNameAware BeanFactoryAware

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值