@RequestMapping详情

@RequestMapping的使用:他是springmvc的路径映射注解,该注解为控制器指定可以控制那些路径,通过请求路径和RequestMapping注解中value属性的值相匹配进行路径跳转。

该注解可以标识的位置

  1. 类:为当前类下所有的方法提供初步的基本路径。
  2. 方法:提供具体的请求路径。

如果一个类上标了该注解,方法上也标识了该注解 ,那么需要得到具体的请求路径,路径名为类上RequestMapping注解值,和具体方法上RequestMapping注解值相拼

举例测试
在RequestMapperTestController类中,类上标识了RequestMapping注解 ,方法上也标识上了RequestMapping注解,在得到具体方法的请求路径时,必须是类是RequestMapping注解的值加上方法二上RequestMapping注解的值

在这里插入图片描述
运行结果在这里插入图片描述
单独方法上的值在这里插入图片描述
RequestMapping的其他属性

  1. value(默认为value) : 请求路径地址
  2. name:这个写者也不知道 ,不过网上有很多关于这个帖子的用法
  3. path:和value一样 ,请求路径地址
  4. method:限定请求方式,默认接受全部请求
  5. params:请求参数
  6. headers:请求头信息
  7. consumes:只接收内容类型是请求信息
  8. produces:告诉游览器返回的类型是什么

method案例
在方法上设置只接受post请求
在方法的跳转上设置为set请求,查看是否能成功跳转
在这里插入图片描述
运行结果
在这里插入图片描述

在这里插入图片描述
params案例
在这里插入图片描述
没带参数执行结果
在这里插入图片描述
携带正确的参数结果
在这里插入图片描述
headers案例
设置请求头信息User-Agent

查看火狐游览器User-Agent请求头信息
在这里插入图片描述
查看谷歌游览器User-Agent请求头信息

在这里插入图片描述

测试代码
在这里插入图片描述

火狐游览器
在这里插入图片描述
谷歌游览器
在这里插入图片描述

解释这段代码@RequestMapping("config") @RestController public class ConfigController{ @Autowired private ConfigService configService; /** * 列表 */ @RequestMapping("/page") public R page(@RequestParam Map<String, Object> params,ConfigEntity config){ EntityWrapper<ConfigEntity> ew = new EntityWrapper<ConfigEntity>(); PageUtils page = configService.queryPage(params); return R.ok().put("data", page); } /** * 列表 */ @IgnoreAuth @RequestMapping("/list") public R list(@RequestParam Map<String, Object> params,ConfigEntity config){ EntityWrapper<ConfigEntity> ew = new EntityWrapper<ConfigEntity>(); PageUtils page = configService.queryPage(params); return R.ok().put("data", page); } /** * 信息 */ @RequestMapping("/info/{id}") public R info(@PathVariable("id") String id){ ConfigEntity config = configService.selectById(id); return R.ok().put("data", config); } /** * 详情 */ @IgnoreAuth @RequestMapping("/detail/{id}") public R detail(@PathVariable("id") String id){ ConfigEntity config = configService.selectById(id); return R.ok().put("data", config); } /** * 根据name获取信息 */ @RequestMapping("/info") public R infoByName(@RequestParam String name){ ConfigEntity config = configService.selectOne(new EntityWrapper<ConfigEntity>().eq("name", "faceFile")); return R.ok().put("data", config); } /** * 保存 */ @PostMapping("/save") public R save(@RequestBody ConfigEntity config){ // ValidatorUtils.validateEntity(config); configService.insert(config); return R.ok(); } /** * 修改 */ @RequestMapping("/update") public R update(@RequestBody ConfigEntity config){ // ValidatorUtils.validateEntity(config); configService.updateById(config);//全部更新 return R.ok(); } /** * 删除 */ @RequestMapping("/delete") public R delete(@RequestBody Long[] ids){ configService.deleteBatchIds(Arrays.asList(ids)); return R.ok(); } }
05-17
package com.xiangmu.controler; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.xiangmu.mappers.GonggaoMapper; import com.xiangmu.pojo.Gonggao; import com.xiangmu.util.JsonResult; @RestController public class GonggaoControler extends BaseController { @Autowired private GonggaoMapper gonggaoMapper; @RequestMapping("/gonggaoAdd") public JsonResult<Void> gonggaoAdd(@RequestBody Gonggao gonggao) { System.out.println(gonggao+"**************"); gonggaoMapper.insert(gonggao); return new JsonResult<>(66); } @RequestMapping("/gonggaoMana") public JsonResult<List> gonggaoMana() { List gonggaoList=gonggaoMapper.select(""); return new JsonResult<>(66,gonggaoList); } @RequestMapping("/gonggaoDel") public JsonResult<Void> gonggaoDel(int id) { gonggaoMapper.delete(id); return new JsonResult<>(66); } @RequestMapping("/gonggaoUpdate") public JsonResult<Void> update(@RequestBody Gonggao gonggao) { gonggaoMapper.update(gonggao); return new JsonResult<>(66); } @RequestMapping("/gonggaoRes") public JsonResult<List> gonggaoRes(String biaoti) { List gonggaoList=gonggaoMapper.select(biaoti.trim()); return new JsonResult<>(66,gonggaoList); } @RequestMapping("/gonggaoAll") public JsonResult<List> gonggaoAll(String biaoti) { List gonggaoList=gonggaoMapper.select(""); return new JsonResult<>(66,gonggaoList); } @RequestMapping("/gonggaoDetailQian") public JsonResult<Gonggao> gonggaoDetailQian(int id) { Gonggao gonggao=gonggaoMapper.findByid(id); return new JsonResult<>(66,gonggao); } @RequestMapping("/gonggaoQian4") public JsonResult<List> gonggaoQian4(String biaoti) { List gonggaoList=gonggaoMapper.select(""); if(gonggaoList.size()>4) { gonggaoList=gonggaoList.subList(0, 4); } return new JsonResult<>(66,gonggaoList); } }
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值