SpringMVC in RestFul style

RestFul it's just a style rather than a norm. Restful style is a resource location and resource operations. Not standard protocol is not just a style. Based on this style of software can be more concise, more structured and easier to implement caching mechanisms.

The traditional way of operating resources: to achieve different effects by different parameters! A single method, post and get

http://127.0.0.1/item/queryItem.action?id=1 query, GET
http://127.0.0.1/item/saveItem.action new, POST
http://127.0.0.1/item/updateItem.action update, POST
http://127.0.0.1/item/deleteItem.action?id=1 delete, GET or POST

Operation using RESTful resources: different effects may be achieved by different request! As follows: request the same address, but the function can be different!

http://127.0.0.1/item/1 query, GET
http://127.0.0.1/item new, POST
http://127.0.0.1/item update, PUT
http://127.0.0.1/item/1 delete, dELETE

No RestFul style? After adding parameters such url address it is used / separator About this address looks but also has good security users can not through its parameters to judge him what type of requests

@Controller
public class MyController {

    @GetMapping("/test/{a}/{b}")
    public String test(@PathVariable String a, @PathVariable String b, Model model){
        String msg = String.valueOf(Integer.parseInt(a) + Integer.parseInt(b));
        System.out.println(msg);
        model.addAttribute("msg","结果为" + msg);
        return "test";
    }
}

@GetMapping comment is life this is the way to Get request to the address of Post request if it is a way to use @PostMapping behind it {} corresponding to the value of the parameter is the name of the method while this parameter names should have a @PathVariable annotation statement so that they correspond to each other thus forming a RestFul style url address ..

@Controller
public class MyController {

    //RestFul风格的传参  RestFul可以有4中类型的函数 Get Put Post Delete
    //其中 Get类型的只能通过Get请求才能跳转到这个Controller Post类型的就只能通过Post请求跳转到该Controller
    //其他的同理  重点是Post和Get 这两种方式
    //@GetMapping 注解代表使用了RestFul风格 并且只能通过Get请求才能跳转到这个Controller中
    @GetMapping("/fun1/{a}/{b}")//路径中的a和b表示传递的参数 使用{}包起来 在Controller中 使用@PathVariable注解拿到传递的这个参数
    public String fun1(@PathVariable int a, @PathVariable String b, Model model){
        System.out.println("i am in");
        String r = a + b;
        model.addAttribute("msg",r);
        return "welcome";
    }

    //使用@PostMapping注解 表示只能通过Post请求才能进入  其中的参数也是通过@PathVariable注解拿到
    @PostMapping("/fun1/{a}/{b}")
    public String fun2(@PathVariable int a, @PathVariable int b,Model model){
        System.out.println("i am in");
        int r = a + b;
        model.addAttribute("msg",r);
        return "welcome";
    }

    @RequestMapping("/fun1")
    public String fun3(Model model){
        System.out.println("i am in");
        int r = 111;
        model.addAttribute("msg",r);
        return "welcome";
    }
}
Published 53 original articles · won praise 0 · Views 1957

Guess you like

Origin blog.csdn.net/XXuan_/article/details/104120369