首页 技术 正文
技术 2022年11月6日
0 收藏 765 点赞 1,037 浏览 3235 个字

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
它有6个属性:
1.value:指定请求的具体地址:
value的uri值为以下三类:
A) 可以指定为普通的具体值:
(value = “/add”),其直接访问controller的路径是ip:port/member/add。它还有一种写法就是下面例子中方法上的value = { “/add”, “/add.html” },
它表示我们既可以通过:port/member/add来访问,也可以通过:port/member/add.html来访问。

@Controller
@RequestMapping(value = "/member")
public class MemberController extends BaseMultiController { @Autowired(required=true)
private MemberService memberService; @RequestMapping(value = { "/add", "/add.html" }, method = { RequestMethod.GET,RequestMethod.POST })
@ResponseBody
public ModelAndView add(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> map = new HashMap<String, Object>();
Member member = new Member();
member.setId("1");
member.setNickname("guoxiaoming");
this.memberService.add(member);
return toView("add", map);
}
}

B) 可以指定为含有某变量的一类值:也就是前台传来的参数,该参数day就是前台或者页面地址上带的参数;其写法是value=”/{day}”
切记:必须在该参数的前面加上@PathVariable注解,这样才能表明该参数是变量

@RequestMapping(value="/{day}", method = RequestMethod.GET)
public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
return appointmentBook.getAppointmentsForDay(day);
}

我们也可以将A和B两种结合起来使用,如下:value=”/owners/{ownerId}

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
Owner owner = ownerService.findOwner(ownerId);
model.addAttribute("owner", owner);
return "displayOwner";
}

C) 可以指定为含正则表达式的一类值,如下value=”/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}”。(不推荐使用)

@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")
public void handle(@PathVariable String version, @PathVariable String extension) {
// ...
}

2.method:指定请求的method类型, GET、POST、PUT、DELETE等;
通常我们只写method = RequestMethod.POST,测试时我们可以用method = RequestMethod.GET,还可以写成method = { RequestMethod.GET,RequestMethod.POST }

3.params: 指定request中必须包含某些参数值时才让该方法处理。

@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController { @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
}

仅处理请求中包含了名为“myParam”,值为“myValue”的请求;

4.headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
@RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
}

仅处理request的header中包含了指定“Refer”请求头和对应值为“http://www.ifeng.com/”的请求;

5.consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

@Controller
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")
public void addPet(@RequestBody Pet pet, Model model) {
// implementation omitted
}

方法仅处理request Content-Type为“application/json”类型的请求。

6.produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

@Controller
@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {
// implementation omitted
}

方法仅处理request请求中Accept头中包含了”application/json”的请求,同时暗示了返回的内容类型为application/json。

这就是Java的魅力,你学会了吗?

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,027
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857