SpringMVC-2

本文详细介绍了SpringMVC中@RequestMapping注解的使用,包括其在方法和类级别的应用,以及value、path、method、params和headers属性的含义和用法。通过示例展示了如何限制请求类型、参数和头部信息,以精确控制控制器处理HTTP请求。
摘要由CSDN通过智能技术生成

SpringMVC-2
一、@RequestMapping介绍
在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,相当于Servlet中在web.xml中配置的映射作用一致。

<servlet>
    <servlet-name>servletName</servlet-name>
    <servlet-class>ServletClass</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>servletName</servlet-name>
    <url-pattern>url</url-pattern>
</servlet-mapping>

RequestMapping注解类的源码

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
	String name() default "";
	String[] value() default {};
	String[] path() default {};
	RequestMethod[] method() default {};
	String[] params() default {};
	String[] headers() default {};
	String[] consumes() default {};
	String[] produces() default {};
}

1)在@Target中有两个属性,分别为 ElementType.METHOD 和 ElementType.TYPE ,也就是说 @RequestMapping 可以在方法和类的声明中使用
2)可以看到注解中的属性除了 name() 返回的字符串,其它的方法均返回数组,也就是可以定义多个属性值,例如 value() 和 path() 都可以同时定义多个字符串值来接收多个URL请求

二、@RequestMapping 中的 value 和 path 属性

1)将 @RequestMapping 注解在 test方法上,而TestController上不添加 @RequestMapping 注解,这时的请求 URL 是相对于 Web 根目录

@Controller
public class TestController{
 
	@RequestMapping("/test")
	public String test() {
		return "success";
	}
}

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

2)将 @RequestMapping 注解在 TestController 类上,这时类的注解是相对于 Web 根目录,而方法上的是相对于类上的路径

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

这时的方法test()能处理的 URL 请求路径则是 http://localhost/SpringMVC/user/test

三、 @RequestMapping 的 method 属性
1)简介:@RequestMapping 中的 method 主要用来定义接收浏览器发来的何种请求。在Spring中,使用枚举类org.springframework.web.bind.annotation.RequestMethod来定义浏览器请求的方式。

Http规范定义了多种请求资源的方式,最基本的有四种,分别为:GET(查)、POST(增)、PUT(改)、DELETE(删),而URL则用于定位网络上的资源相当于地址的作用,配合四种请求方式,可以实现对URL对应的资源的增删改查操作。
在实际应用中,很多人并没有按照这个规范做,因为使用GET/POST同样可以完成PUT和DELETE操作,甚至GET也可以完成POST操作,因为GET不需要用到表单,而POST却需要通过表单来发送。

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

@Controller
@RequestMapping(path = "/user")
public class TestController  {
 
	@RequestMapping(path = "/test", method=RequestMethod.GET)
	public String test() {
		return "success";
	}
}

这时,如果浏览器发来的请求不是GET的话,将收到浏览器返回的错误提示,也就是得通过链接的方式而不是表单的方式
3)通过 @RequestMapping(value=“/login”,method=RequestMethod.POST) 来指定 test()方法 仅处理通过 POST 方式发来的请求

@Controller
@RequestMapping(path = "/user")
public class TestController {
 
	@RequestMapping(path = "/test", method=RequestMethod.POST)
	public String test() {
		return "success";
	}
}

这时,必须通过表单的方式发送请求,否则将收到浏览器返回的错误提示

4)由于在 RequestMapping 注解类中 method() 方法返回的是 RequestMethod 数组,所以可以给 method 同时指定多个请求方式,例如:

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

五、@RequestMapping 的 params 属性,该属性表示请求参数,也就是追加在URL上的键值对,多个请求参数以&隔开

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

该例中则表示 UserController 中的 test() 方法仅处理 /user/login 发来的请求,且必须带有 username=geochance&password=123456 的请求参数,否则浏览器将返回HTTP 404的错误
六、@RequestMapping 的 headers 属性,该属性表示请求头;

用于HTTP协义交互的信息被称为HTTP报文,客户端发送的HTTP报文被称为请求报文,服务器发回给客户端的HTTP报文称为响应报文,报文由报文头部和报文体组成。
请求头部(Request Headers):请求头包含许多有关客户端环境和请求正文的信息,例如浏览器支持的语言、请求的服务器地址、客户端的操作系统等。
响应头部(Rsponse Headers):响应头也包含许多有用的信息,包括服务器类型、日期、响应内容的类型及编码,响应内容的长度等等。

通过 @RequestMapping 中的 headers 属性,可以限制客户端发来的请求

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

此请求中限制了headers中Host必须为“localhost:8080”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值