RestFul 风格

package com.example.controller;

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

@Controller
public class RestFulController {
    @GetMapping("/restful/{id}/{name}")
    public String restful(@PathVariable() Integer id, @PathVariable() String name, Model model) {
        model.addAttribute("msg", "id=" + id + ",name=" + name);
        return "app";
    }
}

如果你想把请求路径中的id设置给方法形参uuid,则可以像下面这样写(name同理):

@Controller
public class RestFulController {
    @GetMapping("/restful/{id}/{name}")
    public String restful(@PathVariable("id") Integer uuid, @PathVariable() String name, Model model) {
        model.addAttribute("msg", "id=" + uuid + ",name=" + name);
        return "app";
    }
}

@Controller
public class RestFulController {
    @GetMapping("/restful/{id}/{name}")
    public String restful(@PathVariable() Integer id, @PathVariable() String name, Model model) {
        model.addAttribute("msg", "id=" + id + ",name=" + name);
        return "app";
    }
    
    @PostMapping("/restful/{id}/{name}")
    public String restful2(@PathVariable() Integer id, @PathVariable() String name, Model model) {
        model.addAttribute("msg", "id=" + id + ",name=" + name);
        return "app";
    }
    
    @DeleteMapping("/restful/{id}/{name}")
    public String restful3(@PathVariable() Integer id, @PathVariable() String name, Model model) {
        model.addAttribute("msg", "id=" + id + ",name=" + name);
        return "app";
    }
    @PatchMapping("/restful/{id}/{name}")
    public String restful4(@PathVariable() Integer id, @PathVariable() String name, Model model) {
        model.addAttribute("msg", "id=" + id + ",name=" + name);
        return "app";
    }
}

最后更新于

这有帮助吗?