栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot——SpringBoot中使用RESTful风格

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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 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 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);
    }
 
}

结果截图:

 

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/272435.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号