REST风格 -2025/1/20

REST风格入门

package com.stdu.controller;

import com.stdu.domain.Book;
import com.stdu.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class BookController {
    @RequestMapping(value = "/books",method = RequestMethod.POST)
    @ResponseBody
    public String save(){
        System.out.println("Book... save...");
        return "{'Book':'simple love'}";
    }

    @RequestMapping(value = "/books",method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody Book book){
        System.out.println("Book... update..." + book);
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/books/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable Integer id){
        System.out.println("Book... delete..." + id);
        return "success";
    }


    @ResponseBody
    @RequestMapping(value = "/books/{id}",method = RequestMethod.GET)
    public String getById(@PathVariable Integer id){
        System.out.println("Book... get..." + id);
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/books",method = RequestMethod.GET)
    public String delete(){
        System.out.println("Book... getAll...");
        return "success";
    }

}

知识点1:@PathVariable

名称 @PathVariable
类型 形参注解
位置 SpringMVC控制器方法形参定义前面
作用 绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参名一一对应
posted @ 2025-01-27 15:36  XYu1230  阅读(13)  评论(0)    收藏  举报