[SpringBoot Usage] SpringBoot (2) Restful请求及其相关标签

前言

上一篇文章, 我们主要介绍了SpringBoot的快速启动, 本章我们主要讲解SpringBoot如何快速开发出一个Restful接口. 及其常用的标签.


知识准备

  • 什么是REST?
    REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移。
    虽然我也看不太懂. 个人粗浅的认为, 使用HTTP协议, 并使用JSON或XML作为数据交互形式的接口, 既可以称为Restful的接口. 直观的, 其常见的请求方式为https://github.com/git/git/pulls?state=closed

  • HTTP接口的类型和种类
    HTTP接口请求类型主要分为GET,POST, PUT, ·DELETE·这4种类型. 还有XX等其余类型. 其实, 因为早期由Servlet 演变过来的缘故, 我们经常使用的只有GETPOST方法. 个人认为, 这与其doGet()doPost()2个接口的方法是分不开的.

  • HTTP常见的对象
    Request. Response. Cookie. Session. JSP应当有内置9大对象, 在我们目前的后端开发过程中, 主要常用的应该为前面4种.


接口实现

Get方法
    @RequestMapping(path = "/get", method = RequestMethod.GET)
    @ResponseBody
    public String testGetMethod() {
        return "hello";
    }
POST方法 - @RequestParam传递参数
    @RequestMapping(path = "/post1", method = RequestMethod.POST)
    @ResponseBody
    public String testPostMethod(@RequestParam("params1") String params1, @RequestParam("params2") String params2) {
        return "params1" + params1 + " ; " + "params2" + params2;
    }
POST 方法 - @RequestBody 传递参数
    @RequestMapping(path = "/post2", method = RequestMethod.POST)
    @ResponseBody
    public String testPostMethod2(@RequestBody String params1, @RequestBody(required = false) String params2) {
        return "params1" + params1 + " ; " + "params2" + params2;
    }

标签解析

  • @RestController & @Controller
  • @RequestMapping
  • @ResponseBody
  • @RequestBody
  • @RequestParams
  • @PathVariable

全量代码

  • Application.java
package com.yanxml.arsenal.springboot.quickstart;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.yanxml.arsenal.springboot"})

public class Application {

    public static void main(String[] args) {
        SpringApplication application =  new SpringApplication(Application.class);
        application.run(args);
    }
}

  • Controller.java
package com.yanxml.arsenal.springboot.quickstart.controller;

import org.springframework.web.bind.annotation.*;

/**
 * HelloWorld Controller.
 *
 * @author seanyanxml
 * @date 2023-01-31
 */

@RestController
@RequestMapping(path = "/helloworld")
public class HelloWorldController {

    @RequestMapping(path = "/get", method = RequestMethod.GET)
    @ResponseBody
    public String testGetMethod() {
        return "hello";
    }

    @RequestMapping(path = "/post1", method = RequestMethod.POST)
    @ResponseBody
    public String testPostMethod(@RequestParam("params1") String params1, @RequestParam("params2") String params2) {
        return "params1" + params1 + " ; " + "params2" + params2;
    }

    @RequestMapping(path = "/post2", method = RequestMethod.POST)
    @ResponseBody
    public String testPostMethod2(@RequestBody String params1, @RequestBody(required = false) String params2) {
        return "params1" + params1 + " ; " + "params2" + params2;
    }


}


Reference

[1]. RESTful 架构详解
[2]. RESTful-百度百科


后记 - 碎碎念

本周是我年后上班第一周, 但是总感觉莫名的疲倦… 也不知道是心累, 还是人累. 但是个人的生物钟调整了一下, 早期早睡, 确实对人的心情和效率有较大的提升.

猜你喜欢

转载自blog.csdn.net/u010416101/article/details/129001591
今日推荐