rest风格开发controller层

 rest(representational state transfer),表现形式状态转换,简单来说是访问网络资源的格式。

 rest风格优点:隐藏资源的访问行为,无法通过地址得知资源进行的是什么操作,书写简化。

 这只是一种风格,一种约束方式,不是规范,可以不遵守,仅仅是提供一种访问资源的思想。

 Controller层代码:

package org.example.Controller;

import org.example.pojo.user;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/users")
public class userRestful {

    @RequestMapping(method = RequestMethod.POST) //写在类上设置浏览器访问具体路径,想访问该方法只能用post请求
    @ResponseBody//设置返回值类型,就是把返回的内容作为响应数据
    public String save(@RequestBody user user){
        System.out.println("springMVC save...");
        return "{'a':'save'}";
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE) //设置浏览器访问路径
    @ResponseBody//设置返回值类型,就是把返回的内容作为响应数据
    public String delete(@PathVariable Integer id){
        System.out.println("springMVC delete..."+id);
        return "{'b':'delete'}";
    }
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody user user){
        System.out.println("springMVC update..."+user);
        return "{'c':'update'}";
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getById(@PathVariable Integer id){
        System.out.println("springMVC getById..."+id);
        return "{'d':'getById'}";
    }
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public String getAll(){
        System.out.println("springMVC getAll...");
        return "{'e':'getAll'}";
    }
}

新增方法save 只能用post请求访问 http://localhost:3306/usershttp://localhost:3306/users

删除方法delete 只能用delete请求并传入id参数进行访问http://localhost:3306/usershttp://localhost:3306/users/1

更新方法update 只能用put请求并传入一个对象进行访问http://localhost:3306/usershttp://localhost:3306/users

查询方法只能用get请求访问,getAll查询所有,不需要传参;

http://localhost:3306/usershttp://localhost:3306/users

getById根据id查询,需要传入id值,     http://localhost:3306/usershttp://localhost:3306/users/1

@PathVariable该注解是把参数以路径变量的形式写入路径,而不是以键值对的形式写在?后面。该注解仅仅是说明变量来源于路径,没说在路径的哪个位置。需要在设置路径时指明位置

 

 上面这种rest风格的书写我们看着会觉得有点繁杂,我们可以简化一下:

package org.example.Controller;

import org.example.pojo.book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@Controller
//@ResponseBody
@RestController
@RequestMapping("/books")
public class booksRestful {

    //@RequestMapping(method = RequestMethod.POST) //写在类上设置浏览器访问具体路径,想访问该方法只能用post请求
    @PostMapping//  此注解完全可以代替上面的注解
    public String save(){
        System.out.println("springMVC save...");
        return "{'a':'save'}";
    }

    //@RequestMapping(value = "/{id}",method = RequestMethod.DELETE) //设置浏览器访问路径
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id){ //该注解是把参数以路径变量的形式写入路径,而不是以键值对的形式写在?后面。该注解仅仅是说明变量来源于路径,没说在路径的哪个位置。需要在设置路径时指明位置
        System.out.println("springMVC delete..."+id);
        return "{'b':'delete'}";
    }

    //@RequestMapping(method = RequestMethod.PUT)
    @PutMapping
    public String update(@RequestBody book book){
        System.out.println("springMVC update..."+book);
        return "{'c':'update'}";
    }

    //@RequestMapping(value = "{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("springMVC getById..."+id);
        return "{'d':'getById'}";
    }

    //@RequestMapping(method = RequestMethod.GET)
    @GetMapping
    public String getAll(){
        System.out.println("springMVC getAll...");
        return "{'e':'getAll'}";
    }
}

@RestController 注解是@Controller和@ResponseBody的合并。

@PostMapping此注解完全可以代替原来的注解@RequestMapping(method = RequestMethod.POST)

@DeleteMapping("/{id}")代替 @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)

@PutMapping代替@RequestMapping(method = RequestMethod.PUT)

@GetMapping代替@RequestMapping(method = RequestMethod.GET)

@GetMapping("/{id}")代替@RequestMapping(value = "{id}",method = RequestMethod.GET)

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值