日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Springboot 常见请求方式

發(fā)布時間:2024/9/30 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot 常见请求方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/zhaoyanjun6/article/details/80404645
本文出自【趙彥軍的博客】

文章目錄

  • 用戶模型類
  • Get 請求
    • 1、無參
    • 2、帶參數(shù)
    • 3、RESTful API
  • POST 請求
    • 1、表單請求
    • 2、參數(shù)為對象
  • 請求 header 獲取
    • 獲取單個 header
    • 獲取所有 header
  • SpringBoot 上傳多文件 MultipartFile
    • 服務(wù)器接收一個文件
    • 服務(wù)器接收多個文件,方式一
    • 服務(wù)器接收多個文件,方式二
    • 服務(wù)器接收多個文件+單個普通參數(shù)
    • 服務(wù)器接收多個文件+多個普通參數(shù)

用戶模型類

package com.yiba.wifi.news.bean.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;@Entity public class User {@Id@GeneratedValueInteger id;String name;Integer age ;//....set 省略//....get 省略 }

Get 請求

1、無參

請求api

http://localhost:8083/api/find

接口設(shè)計

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 數(shù)據(jù)* @return*/@GetMapping("find")public User findOne() {//查詢用戶邏輯.....return new User();}}

2、帶參數(shù)

請求api

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

接口設(shè)計

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 數(shù)據(jù)* 當(dāng) name 為 null 的時候,用默認(rèn)值 yanjun 代替* @return*/@GetMapping("find")public User findOne(@RequestParam(value = "name", defaultValue = "yanjun") String name) {//查詢用戶邏輯.....logger.info("name:" + name);return new User();}}

3、RESTful API

請求api

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

接口設(shè)計

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 數(shù)據(jù)** @param id* @return*/@GetMapping("find/{id}")public User findOne(@PathVariable("id") Integer id) {logger.info("id:" + id);//查詢用戶邏輯.....return new User();}}

POST 請求

1、表單請求

1、 請求api 方式一

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

實例圖:

2、 請求api 方式二:表單

http://localhost:8083/api/find

實例圖:

接口設(shè)計

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 數(shù)據(jù)** @return*/@PostMapping("find")public User findOne(@RequestParam(value = "name", defaultValue = "yanjun",required = false) String name) {//查詢用戶邏輯.....logger.info("name:" + name);return new User();}}

2、參數(shù)為對象

請求api

http://localhost:8083/api/find

請求 body

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

接口設(shè)計

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 數(shù)據(jù)** @return*/@PostMapping("find")public User findOne(@RequestBody User user) {//查詢用戶邏輯.....logger.info("name:" + user.getName());return user;}}

請求示例:

請求 header 獲取

獲取單個 header

接口設(shè)計

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

接口設(shè)計

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());@Autowiredprivate HttpServletRequest request;@PostMapping("find")public User findOne(@RequestBody User user) {logger.info("CONTENT_TYPE:" + request.getHeader(HttpHeaders.CONTENT_TYPE)); //獲取headerlogger.info("TOKEN:" + request.getHeader("token")); //獲取headerreturn user;} }

SpringBoot 上傳多文件 MultipartFile

服務(wù)器接收一個文件

@RestController public class UserController {@PostMapping("find")public String findOne(@RequestParam("file") MultipartFile part) {//part 名字String fileName = part.getName();//文件名字String originalFilename = part.getOriginalFilename();//獲取文件內(nèi)容String fileContent = getResult(part);return "成功";}//讀取文件內(nèi)容public String getResult(MultipartFile part) {BufferedInputStream bufferedInputStream = null;try {InputStream inputStream = part.getInputStream();bufferedInputStream = new BufferedInputStream(inputStream);byte[] buffer = new byte[1024];int length; //代表實際讀取的字節(jié)數(shù)String result = "";while ((length = bufferedInputStream.read(buffer)) != -1) {//length 代表實際讀取的字節(jié)數(shù)result = result + new String(buffer, 0, length);}return result;} catch (IOException e) {e.printStackTrace();} finally {if (bufferedInputStream != null) {try {bufferedInputStream.close();} catch (IOException e) {e.printStackTrace();}}}return "";} }

服務(wù)器接收多個文件,方式一

@RestController public class UserController {@PostMapping("find")public String findOne(@RequestParam("file") MultipartFile part, @RequestParam("file2") MultipartFile part2) {//獲取第一個文件內(nèi)容String fileContent = getResult(part);//獲取第二個文件內(nèi)容String fileContent2 = getResult(part2);return "成功";} }

服務(wù)器接收多個文件,方式二

@RestController public class UserController {@PostMapping("find")public String findOne(@RequestParam("file") MultipartFile[] files) {//接收多個文件 for (MultipartFile part : files) {//獲取文件內(nèi)容String fileContent = getResult(part);System.out.printf("內(nèi)容:" + part.getName() + " " + fileContent);}return "成功";} }

服務(wù)器接收多個文件+單個普通參數(shù)

@RestController public class UserController {@PostMapping("find")public String findOne(@RequestParam("file") MultipartFile[] files, @RequestParam String age) {//接收多個文件for (MultipartFile part : files) {//獲取文件內(nèi)容String fileContent = getResult(part);System.out.printf("內(nèi)容:" + part.getName() + " " + fileContent);}//接收普通參數(shù)String ageValue = age;System.out.printf("普通參數(shù)age:" + ageValue);return "成功";} }

服務(wù)器接收多個文件+多個普通參數(shù)

@RestController public class UserController {@PostMapping("find")public String findOne(@RequestParam("file") MultipartFile[] files, @RequestParam String age, @RequestParam String name) {//接收多個文件for (MultipartFile part : files) {//獲取文件內(nèi)容String fileContent = getResult(part);System.out.printf("內(nèi)容:" + part.getName() + " " + fileContent);}//接收普通參數(shù)String ageValue = age;System.out.printf("普通參數(shù)age:" + ageValue);return "成功";} }

個人微信號:zhaoyanjun125 , 歡迎關(guān)注

總結(jié)

以上是生活随笔為你收集整理的Springboot 常见请求方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。