當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Springboot 常见请求方式
生活随笔
收集整理的這篇文章主要介紹了
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IntelliJ IDEA Licens
- 下一篇: SpringBoot Quartz 定时