RESTful风格

@PathVariable注解

学习RESTful风格首先要了解@PathVariable注解
@PathVariable:路径变量,可以将变量直接加入到路径中

具体代码

@Controller
public class HelloController {
    @RequestMapping("hello/{a}/{b}")
    public String test2(@PathVariable int a,@PathVariable int b, Model model){
        int res = a+b;
        model.addAttribute("msg","结果为:"+res);
        return "test";
    }
}

这里将 @PathVariable 放在变量前,表示访问时变量可以作为路径的形式出现
例:http://localhost:8080/hello/1/2
页面显示为结果为:3
一个简单的RESTful风格的url就完成了


此外@RequestMapping可以指定以什么请求方式进行访问

具体代码
@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.GET )

@Controller
public class HelloController {
    @RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.GET )
    public String test2(@PathVariable int a,@PathVariable int b, Model model){
        int res = a+b;
        model.addAttribute("msg","结果为:"+res);
        return "test";
    }
}

通过method属性来设置请求方式,可以将其设置为

@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.GET )
@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.POST)
@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.PUT)
@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.DELETE)
// ......还有一些没有了解到的日后补充

默认访问方法为GET方法

或者以另一种方式指定请求方式

@GetMapping	//以GET方式进行访问,查询
@PostMapping	//新增
@PutMapping		//更新
@DeleteMapping	//删除
@PatchMapping	//对局部更新

OK!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值