RestFul 风格(SpringMVC学习笔记三)

发布于:2024-04-22 ⋅ 阅读:(28) ⋅ 点赞:(0)

1、什么是Restful风格:

Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

2、使用Restful风格

接上一个笔记的测试类

package com.li.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

//@Controller注解的类会自动添加到Spring上下文中
@Controller
public class ControllerTest2 {
//    真实访问地址http://localhost:8080/abc
    @RequestMapping("/abc")
    public String Hello(Model model){

        //Spring MVC会自动实例化一个Model对象用于向视图中传值
        model.addAttribute("msg","hello,springmvc");

        //返回视图位置
        return "test";
    } 
}

在Spring MVC中可以使用 @PathVariable 注解,让方法参数的值对应绑定到一个URI模板变量0上。 格式如:真实访问地址http://localhost:8080/abc/参数1/参数2

package com.li.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

//@Controller注解的类会自动添加到Spring上下文中
@Controller
public class ControllerTest2 {
//    真实访问地址http://localhost:8080/abc/参数a/参数b    
//    需要注意的是在方法参数绑定到URL模板上后在@RequestMapping("/abc/{参数}/{参数}")要对应的配置上参数
    @RequestMapping("/abc/{a}/{b}")
//在Spring MVC中可以使用 @PathVariable 注解,让方法参数的值对应绑定到一个URI模板变量上。
    public String Hello(@PathVariable int a,  @PathVariable int b, Model model){

        int c = a+b;

        //Spring MVC会自动实例化一个Model对象用于向视图中传值
        model.addAttribute("msg","c的值是:"+c);

        //返回视图位置
        return "test";
    } 
}

所有的地址栏请求默认都会是 HTTP GET 类型的。

Spring MVC 的 @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。

使用method属性指定请求类型

下面我们用method属性把请求改成put,我们可以看到报错了,因为默认的是get类型

package com.li.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//@Controller注解的类会自动添加到Spring上下文中
@Controller
public class ControllerTest2 {
//    真实访问地址http://localhost:8080/abc/参数a/参数b
//    需要注意的是在方法参数绑定到URL模板上后在@RequestMapping("/abc/{参数}/{参数}")要对应的配置上参数
//用method属性指定请求为PUT
    @RequestMapping(value = "/abc/{a}/{b}",method = {RequestMethod.PUT})
//在Spring MVC中可以使用 @PathVariable 注解,让方法参数的值对应绑定到一个URI模板变量上。
    public String Hello(@PathVariable int a,  @PathVariable int b, Model model){

        int c = a+b;

        //Spring MVC会自动实例化一个Model对象用于向视图中传值
        model.addAttribute("msg","c的值是:"+c);

        //返回视图位置
        return "test";
    } 
}

 请求改回来就正常了改回来就正常了

package com.li.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//@Controller注解的类会自动添加到Spring上下文中
@Controller
public class ControllerTest2 {
//    真实访问地址http://localhost:8080/abc/参数a/参数b
//    需要注意的是在方法参数绑定到URL模板上后在@RequestMapping("/abc/{参数}/{参数}")要对应的配置上参数
    @RequestMapping(value = "/abc/{a}/{b}",method = {RequestMethod.GET})
//在Spring MVC中可以使用 @PathVariable 注解,让方法参数的值对应绑定到一个URI模板变量上。
    public String Hello(@PathVariable int a,  @PathVariable int b, Model model){

        int c = a+b;

        //Spring MVC会自动实例化一个Model对象用于向视图中传值
        model.addAttribute("msg","c的值是:"+c);

        //返回视图位置
        return "test";
    } 
}

方法级别的注解变体有如下几个: 组合注解

  1. @GetMapping
  2. @PostMapping
  3. @PutMapping
  4. @DeleteMapping
  5. @PatchMapping

就相当于: @RequestMapping(method =RequestMethod.GET) 的一个快捷方式。