@RequestMapping小结

为什么我想去了解这个,最近学习crud,然后需要用到常用的两个注解
@PostMapping和@GetMapping
这些属于符合注解,他们的前身就是这次的主题。
接下来来了解一下@RequestMapping的用法和功能吧。

一、简介

@RequestMapping来映射请求

指定控制器可以处理哪些url请求;

相当于servlet在web.xml中配置

1、可以在方法和类的声明中使用

2、除了name外,其他属性可以定义多个属性值(下面分别介绍属性)

三、测试value和path

1、注解加在login方法上

这时的请求 URL 是相对于 Web 根目录

方法 login() 能处理的 URL 请求路径是基于 Web 应用的,也就是 http://localhost/SpringMVC/login

@Controller
public class UserController {
 
	@RequestMapping("/login")
	public String login() {
		return "success";
	}
}

2、注解在 UserController 类上

这时类的注解是相对于 Web 根目录,而方法上的是相对于类上的路径

能处理的 URL 请求路径则是 http://localhost/SpringMVC/user/login

@Controller
@RequestMapping("/user")
public class UserController {
 
	@RequestMapping("/login")
	public String login() {
		return "success";
	}
}

四、测试method

1简介

主要用来定义接收浏览器发来的何种请求。

实际应用中,很多人并没有按照这个规范做,因为使用GET/POST同样可以完成PUT和DELETE操作,甚至GET也可以完成POST操作,因为GET不需要用到表单,而POST却需要通过表单来发送。

2 method=RequestMethod.GET

@RequestMapping(value=“/login”,method=RequestMethod.GET) 来指定 login()方法 仅处理通过 GET 方式发来的请求

浏览器发来的请求不是GET的话,将收到浏览器返回的错误提示,也就是得通过链接的方式而不是表单的方式:

@Controller
@RequestMapping(path = "/user")
public class UserController {
 
	@RequestMapping(path = "/login", method=RequestMethod.GET)
	public String login() {
		return "success";
	}
}
3、method=RequestMethod.POST

同理

4 method() 方法返回的是 RequestMethod 数组

给 method 同时指定多个请求方式

@Controller
@RequestMapping(path = "/user")
public class UserController {
        // 该方法将同时接收通过GET和POST方式发来的请求
	@RequestMapping(path = "/login", method={RequestMethod.POST,RequestMethod.GET})
	public String login() {
		return "success";
	}
}

五 params属性

表示请求参数,也就是追加在URL上的键值对,多个请求参数以&隔开

使用 params 来限制请求参数,来实现进一步的过滤请求

@Controller
@RequestMapping(path = "/user")
public class UserController {
        
        // 该方法将接收 /user/login 发来的请求,且请求参数必须为 username=kolbe&password=123456
	@RequestMapping(path = "/login", params={"username=kolbe","password=123456"})
	public String login() {
		return "success";
	}
}

六、headers

@RequestMapping 中的 headers 属性,可以限制客户端发来的请求
这个我没遇到过,作为了解

@Controller
@RequestMapping(path = "/user")
public class UserController {
        // 表示只接收本机发来的请求
	@RequestMapping(path = "/login", headers="Host=localhost:8080")
	public String login() {
		return "success";
	}
}

七、带占位符的URL

@PathVariable 将 URL 中的占位符绑定到控制器的处理方法的参数中,占位符使用{}括起来

这个控制器中 show() 方法将可以接收 user/1、user/2、user/3等等的路径请求

@Controller
@RequestMapping(path = "/user")
public class UserController {
        
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public String show(@PathVariable("id") Integer id) {
		return "success";
	}
}

八、REST 风格的 URL 请求

 请求路径        请求方法           作用
-/user/1        HTTP GET        得到id为1的user
-/user/1        HTTP DELETE     删除id为1的user
-/user/1        HTTP PUT        更新id为1的user
-/user          HTTP POST       新增user
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public String show(@PathVariable("id") Integer id) {
		System.out.println("查看id为:" + id + "的user");
		return "success";
	}

上文来源

https://blog.csdn.net/renanrenan/article/details/84654362?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165104825816782248535182%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165104825816782248535182&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-84654362.142^v9^control,157^v4^control&utm_term=requestMapping&spm=1018.2226.3001.4187
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值