RESTful风格;

RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。RESTFUL适用于移动互联网厂商作为业务使能接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源。

一:

二:

三:

package com.nbg.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;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Controller注解配置的类表示被扫描,被spring托管
 */
@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/haha")
    public String one(Model model) {
        model.addAttribute("one", "第一个注解配置的springmvc");
        /**
         * 会被实体解析器处理
         */
        return "one";
    }

    /**
     * RESTful风格;http://localhost:8080/springmvc_03_Anno/hello/restFul/1/2  1→a,2→b
     * 需要使用@PathVariable注解;
     *
     * @param a
     * @param b
     * @param model
     * @return
     */
    @RequestMapping("/restFul/{a}/{b}")
    public String restFul(@PathVariable int a, @PathVariable int b, Model model) {
        model.addAttribute("one", "" + a + b);
        return "one";
    }

    /**
     * 使用了 @PostMapping 这个注解,那就只有使用post请求才能被访问到,其余一样@GetMapping
     *
     * @param a
     * @param b
     * @param model
     * @return
     */
    @PostMapping("/post/{a}/{b}")
    public String postM(@PathVariable int a, @PathVariable int b, Model model) {
        model.addAttribute("one", "" + a + b);
        return "one";
    }
  /**
   * 使用这种方法@RequestMapping(name = "/nameAndMethod/{a}/{b}",method = {RequestMethod.GET})
   * 也可以做到@GetMapping的效果
   * @param a
   * @param b
   * @param model
   * @return
   */
  @RequestMapping(value= "/valueAndMethod/{a}/{b}",method = {RequestMethod.GET})
  public String getM(@PathVariable int a, @PathVariable int b, Model model) {
   model.addAttribute("one", "" + a + b);
   return "one";
  }
}

 

posted @ 2020-12-18 23:11  nbg  阅读(63)  评论(0编辑  收藏  举报