springboot 常見請求方式

轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/80404645 本文出自【趙彥軍的博客】java

用戶模型類

package com.yiba.wifi.news.bean.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {

    @Id
    @GeneratedValue
    Integer id;

    String name;

    Integer age ;

    //....set 省略
    //....get 省略
}

Get 請求

一、無參

請求apiweb

http://localhost:8083/api/find

接口設計spring

package com.yiba.wifi.news.controller;

import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api")
public class UserController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查詢 id 數據
     * @return
     */
    @GetMapping("find")
    public User findOne() {

        //查詢用戶邏輯.....
        return new User();
    }

}

二、帶參數

請求apiapi

http://localhost:8083/api/find?name=zhaoyanjun

接口設計微信

package com.yiba.wifi.news.controller;

import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api")
public class UserController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查詢 id 數據
     * 當 name 爲 null 的時候,用默認值 yanjun 代替
     * @return
     */
    @GetMapping("find")
    public User findOne(@RequestParam(value = "name", defaultValue = "yanjun") String name) {

        //查詢用戶邏輯.....
        logger.info("name:" + name);
        return new User();
    }

}

三、RESTful API

請求apiapp

http://localhost:8083/api/find/5

接口設計dom

package com.yiba.wifi.news.controller;

import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api")
public class UserController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查詢 id 數據
     *
     * @param id
     * @return
     */
    @GetMapping("find/{id}")
    public User findOne(@PathVariable("id") Integer id) {
        logger.info("id:" + id);
        
        //查詢用戶邏輯.....
        return new User();
    }

}

POST 請求

一、表單請求

一、 請求api 方式一ui

http://localhost:8083/api/find?name=zhaoyanjun

實例圖: 這裏寫圖片描述this

二、 請求api 方式二:表單spa

http://localhost:8083/api/find

實例圖:

這裏寫圖片描述

接口設計

package com.yiba.wifi.news.controller;

import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api")
public class UserController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查詢 id 數據
     *
     * @return
     */
    @PostMapping("find")
    public User findOne(@RequestParam(value = "name", defaultValue = "yanjun",required = false) String name) {

        //查詢用戶邏輯.....
        logger.info("name:" + name);
        return new User();
    }

}

二、參數爲對象

請求api

http://localhost:8083/api/find

請求 body

{
    "id": 1,
    "name": "yanjun",
    "age": 18
}

接口設計

package com.yiba.wifi.news.controller;

import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api")
public class UserController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查詢 id 數據
     *
     * @return
     */
    @PostMapping("find")
    public User findOne(@RequestBody User user) {

        //查詢用戶邏輯.....
        logger.info("name:" + user.getName());
        return user;
    }

}

請求示例:

這裏寫圖片描述

請求 header 獲取

獲取單個 header

接口設計

package com.yiba.wifi.news.controller;

import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api")
public class UserController {

    Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     *
     * @param user
     * @param token 獲取 header 裏面的 token 字段
     * @return
     */
    @PostMapping("find")
    public User findOne(@RequestBody User user,
                        @RequestHeader(value = "token") String token) {

        //查詢用戶邏輯.....
        logger.info("token:" + token);
        return user;
    }

}

請求示例

這裏寫圖片描述

二、獲取全部 header

接口設計

package com.yiba.wifi.news.controller;

import com.yiba.wifi.news.bean.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("api")
public class UserController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private HttpServletRequest request;

    @PostMapping("find")
    public User findOne(@RequestBody User user) {

        logger.info("CONTENT_TYPE:" + request.getHeader(HttpHeaders.CONTENT_TYPE));  //獲取header
        logger.info("TOKEN:" + request.getHeader("token")); //獲取header

        return user;
    }
}

我的微信號:zhaoyanjun125 , 歡迎關注

相關文章
相關標籤/搜索