SpringBoot——SpringBoot中使用RESTful风格

目录

1 认识 RESTFul

 1.1 Spring boot 开发 RESTFul 主要是几个注解实现

1.1.1 @PathVariable

1.1.2 @RequestMapping(常用)

1.1.3 @PostMapping 

1.1.4  @DeleteMapping

 1.1.5 @PutMapping

 1.1.6 @GetMapping

2 实现代码


1 认识 RESTFul

REST(英文:Representational State Transfer,简称 REST)
一种互联网软件架构设计的风格,但它并不是标准,它只是提出了一组客户端和服务器交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更简洁,更有层次,REST
这个词,是 Roy Thomas Fielding 在他 2000 年的博士论文中提出的。

任何的技术都可以实现这种理念,如果一个架构符合 REST 原则,就称它为 RESTFul 架构

 1.1 Spring boot 开发 RESTFul 主要是几个注解实现

1.1.1 @PathVariable

获取 url 中的数据,该注解是实现 RESTFul 最主要的一个注解

1.1.2 @RequestMapping(常用)

支持Get请求,也支持Post请求。

1.1.3 @PostMapping 

接收和处理 Post 方式的请求,Post 请求主要用在用户新增数据。

1.1.4  @DeleteMapping

接收 delete 方式的请求,可以使用 GetMapping 代替,通常用于删除数据。

 1.1.5 @PutMapping

接收 put 方式的请求,可以用 PostMapping 代替,Put 通常用于修改数据

 1.1.6 @GetMapping

接收 get 方式的请求,Get 请求主要用于查询操作

2 实现代码

先写一个实体类

package com.liuhaiyang.springboot.entity;

public class Student {
    private Integer id;

    private String name;

    private Integer age;

  //set()和get()
}

控制层

import org.springframework.web.bind.annotation.*;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 *
 */
@RestController //相当于是 @Controller 与 @ResponseBody 的组合注解,意味着当前控制层类中的所有方法返回的都是Json对象
public class StudentController {
 
    @GetMapping(value = "/student/{id}/{age}")
    public Object student(@PathVariable("id") Integer id,
                          @PathVariable("age") Integer age) {
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("age",age);
        return map;
    }
 
    @DeleteMapping(value = "/student/detail/{id}/{status}")
    public Object student2(@PathVariable("id") Integer id,
                           @PathVariable("status") Integer status) {
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("status",status);
        return map;
    }
 
    @DeleteMapping(value = "/student/{id}/detail/{phone}")
    public Object student3(@PathVariable("id") Integer id,
                           @PathVariable("phone") Integer phone) {
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("phone",phone);
        return map;
    }
 
    @PostMapping(value = "/student/{id}")
    public String addStudent(@PathVariable("id") Integer id) {
        return "add student ID: " + id;
    }
 
    @PutMapping(value = "/student/{id}")
    public String updateStudent(@PathVariable("id") Integer id) {
        return "update student ID: " + id;
    }
}

核心配置文件

server.port=8082
 
server.servlet.context-path=/springboot

启动类

package com.liuhaiyang.springboot;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
 
}

结果截图:

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值