springboot使用SpringBoot基础HTTP接口GET|POST|DELETE|PUT请求

Posted suncm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot使用SpringBoot基础HTTP接口GET|POST|DELETE|PUT请求相关的知识,希望对你有一定的参考价值。

1.Get请求

  1)测试restful协议,从路径中获取字段

  单一参数@RequestMapping(path = "/{id}", method = RequestMethod.GET)

       public String getUser(@PathVariable String id ) {}

  2)测试@GetMapping = @RequestMapping(Method={RequestMethod.GET})

  @PostMapping = @RequestMapping(Method={RequestMethod.POST})

  @PutMapping = @RequestMapping(Method={RequestMethod.PUT})

  @DeleteMapping = @RequestMapping(Method={RequestMethod.DELETE})

  3)@RequestParam(value = "name", required = true)
  可以设置默认值,比如分页

 

  4)@RequestBody 请求体映射实体类
  需要指定http头为 content-type为application/json charset=utf-8

  5)@RequestHeader 请求头,比如鉴权
  @RequestHeader("access_token") String accessToken

  6)HttpServletRequest request自动注入获取参数

  

package com.example.demo.controller;

import com.example.demo.pojo.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Suncm
 * @ClassName GetController
 * @Date 2019/5/15 17:20
 * @Version 1.0
 */
//测试http协议的get请求以及注解的使用
@RestController
public class GetController {

    private Map<String, Object> params = new HashMap<String,Object>();
    /**
     *@Author Suncm
     *@Despription //测试restful协议,从路径中获取字段
     *@Date 10:31 2019/5/16
     *@Param [cityId, userId]
     *@return java.lang.Object
     **/
    @RequestMapping(path = "/{city_id}/{user_id}",method = RequestMethod.GET)
    public Object findUser(@PathVariable("city_id") String cityId, @PathVariable(value = "user_id",required = true) String userId){
        params.clear();
        params.put("cityId",cityId);
        params.put("userId",userId);
        for (Map.Entry<String,Object> entry:params.entrySet()
             ) {
            System.out.println(entry.getKey()+",value="+entry.getValue());
        }
        return params;
    }
    /*
     *@Author Suncm
     *@Despription //测试@getMapping
     *@Date 11:02 2019/5/16
     *@Param [form, size]
     *@return java.lang.Object
     **/
    @GetMapping(value = "/v1/page_user1")
    public Object pageUser(int from,int size){

        params.clear();
        params.put("from",from);
        params.put("size",size);
        return params;
    }

    /*
     *@Author Suncm
     *@Despription //测试参数默认值
     *@Date 11:13 2019/5/16
     *@Param [from, size]
     *@return java.lang.Object
     **/
    @GetMapping(value = "/v1/page_user2")
    public Object pageUser2(@RequestParam(defaultValue = "0",name = "page") int from,int size){
        params.clear();
        params.put("from",from);
        params.put("size",size);
        return params;
    }
    /*
     *@Author Suncm
     *@Despription //bean 对象传参
     * 1、注意需要指定http头为 content-type为application/json
     * 2、使用body传输数据
     *@Date 11:27 2019/5/16
     *@Param [user]
     *@return java.lang.Object
     **/
    @RequestMapping(value = "/v1/save_user")
    public Object saveUser(@RequestBody User user){
        params.clear();
        params.put("user",user);
        return params;
    }
    /*
     *@Author Suncm
     *@Despription //获取请求头
     *@Date 11:47 2019/5/16
     *@Param []
     *@return java.lang.Object
     **/
    @RequestMapping("/v1/get_header")
    public Object getHeader(@RequestHeader("access_token")String accessToken,String id){
        params.clear();
        params.put("accessToken",accessToken);
        params.put("id",id);
        return params;
    }

    /*
     *@Author Suncm
     *@Despription //参数存储到request里
     *@Date 11:59 2019/5/16
     *@Param [request]
     *@return java.lang.Object
     **/
    @GetMapping("/v1/test_request")
    public Object testRequest(HttpServletRequest request){
        params.clear();
        String id = request.getParameter("id");
        params.put("id", id);
        return params;
    }
    /*
     *@Author Suncm
     *@Despription //测试@PostMapping
     *@Date 14:41 2019/5/16
     *@Param [id, pwd]
     *@return java.lang.Object
     **/
    @PostMapping("/v1/test_post")
    public Object postTest(String id,String pwd){
        params.clear();
        params.put("id",id);
        params.put("pwd",pwd);
        return params;
    }
    /*
     *@Author Suncm
     *@Despription //测试@PutMapping
     *@Date 14:44 2019/5/16
     *@Param [id]
     *@return java.lang.Object
     **/
    @PutMapping("/v1/test_put")
    public Object changeUser(String id){
        params.clear();
        params.put("id",id);
        return params;
    }

    @DeleteMapping("/v1/test_del")
    public Object delUser(String id){
        params.clear();
        params.put("id",id);
        return params;
    }
}

 

以上是关于springboot使用SpringBoot基础HTTP接口GET|POST|DELETE|PUT请求的主要内容,如果未能解决你的问题,请参考以下文章

Springboot联结万物学习笔记--Springboot微服务基础搭建篇-- SpringBoot中日志的使用

三天讲透SpringBoot-初识基础使用

springboot:基础学习一 linux下后台启动springboot项目

2018最新SpringBoot2.0教程(零基础入门)

SpringBoot学习--02基础配置--SpringBoot配置

springboot使用SpringBoot基础HTTP接口GET|POST|DELETE|PUT请求