请求处理-Rest风格映射及form表单发送put、delete请求

一、Rest风格映射

Rest映射是一种将HTTP请求映射到处理程序方法的机制。在Spring MVC中,Rest映射是通过@RequestMapping注解来实现的。RequestMapping注解可以用于类级别和方法级别,用于指定请求的URL和HTTP方法。

在处理请求时,Spring MVC会根据请求的URL和HTTP方法来查找匹配的处理程序方法。如果找到了匹配的方法,Spring MVC会将请求参数绑定到方法的参数上,并调用该方法来处理请求。处理程序方法可以返回一个视图名称或一个响应体。

RequestMapping注解有很多属性,可以用于指定请求的URL、HTTP方法、请求参数、请求头等。下面是一个简单的例子:

@RestController

@RequestMapping("/users")

public class UserController {



    @GetMapping("/{id}")

    public User getUser(@PathVariable Long id) {

    // 根据id查询用户

    return user;

    }



    @PostMapping

    public User createUser(@RequestBody User user) {

    // 创建用户

    return user;

    }



    @PutMapping("/{id}")

    public User updateUser(@PathVariable Long id, @RequestBody User user) {

    // 更新用户

    return user;

    }



    @DeleteMapping("/{id}")

    public void deleteUser(@PathVariable Long id) {

    // 删除用户

    }

    }

在上面的例子中,UserController类被标记为@RestController,表示它是一个REST控制器。它还被标记为@RequestMapping("/users"),表示它处理以“/users”开头的请求。

getUser方法被标记为@GetMapping("/{id}"),表示它处理HTTP GET请求,并且请求的URL是“/users/{id}”,其中{id}是一个占位符,表示请求的参数。@PathVariable注解用于将{id}绑定到方法的参数上。

createUser方法被标记为@PostMapping,表示它处理HTTP POST请求。@RequestBody注解用于将请求体绑定到方法的参数上。

updateUser方法被标记为@PutMapping("/{id}"),表示它处理HTTP PUT请求。它还使用了@GetMapping("/{id}")相同的占位符{id}。

deleteUser方法被标记为@DeleteMapping("/{id}"),表示它处理HTTP DELETE请求。它还使用了@GetMapping("/{id}")相同的占位符{id}。

以上就是Rest映射的基本用法。在Spring MVC中,Rest映射是非常重要的,因为它可以帮助我们构建RESTful风格的Web服务。

二、form表单发送put、delete请求

  • 页面中需要使用form表单提交,且提交非get/post请求时需要提交一个隐藏域,name = “_method” value = “请求”。

  • 首先表单中必须是method = post,然后提交后会解析隐藏域,将post请求转化为对应的请求value值。

用法如下:

2.1 开启页面表单的Rest功能

application.yml写法如下:

spring:
  mvc:
    hiddenmethod:
      filter:
        enabled: true   #开启页面表单的Rest功能

2.2  页面form的属性method=post,隐藏域 _method=put、delete等(如果直接get或post,无需隐藏域)

对应的页面如下:

<h1>测试rest风格: </h1>
<form action="/user" method="get">
    <input type="submit" value="get-提交">
</form>

<form action="/user" method="post">
    <input type="submit" value="post-提交">
</form>

<form action="/user" method="post">
    <input type="hidden" name="_method" value="PUT">
    <input type="submit" value="put-提交">
</form>

<form action="/user" method="post">
    <input type="hidden" name="_method" value="DELETE">
    <input type="submit" value="delete-提交">
</form>

2.3  对应的controller

  • 控制层所有的path路径全都是一样的,但是需要给@RequestMapping注解设置属性处理的方式或者使用@xxxMapping注解

  • @ResponseBody表示直接响应到页面中不经过视图处理器。

@GetMapping("/user")
//@RequestMapping(value = "/user",method = RequestMethod.GET)
public String getUser(){
    return "GET-张三";
}

@PostMapping("/user")
//@RequestMapping(value = "/user",method = RequestMethod.POST)
public String saveUser(){
    return "POST-张三";
}

@PutMapping("/user")
//@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String putUser(){
    return "PUT-张三";
}

@DeleteMapping("/user")
//@RequestMapping(value = "/user",method = RequestMethod.DELETE)
public String deleteUser(){
    return "DELETE-张三";
}

2.4  展示结果如下

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_55772633/article/details/131881497