當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot开发接口
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot开发接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、模擬get請求
2、模擬get請求返回cookie
3、模擬get請求攜帶cookie信息
4、模擬get請求攜帶參數
5、模擬get請求攜帶參數二
6、模擬post請求返回cookies信息
7、模擬post請求攜帶cookies信息
1、模擬get請求
package com.course.server;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;@RestController public class MyGetMethod {@RequestMapping(value = "/useSpringBoot/getcookies",method = RequestMethod.GET)public String getCookie(){return "這是我的返回結果";} }2、模擬get請求返回cookie
package com.course.server;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse;@RestController public class MyGetMethod {//模擬get請求@RequestMapping(value = "/useSpringBoot/get",method = RequestMethod.GET)public String get(){return "這是我的get請求的返回結果";}//模擬get請求返回cookie信息@RequestMapping(value = "/useSpringBoot/getcookies",method = RequestMethod.GET)public String getCookie(HttpServletResponse response){Cookie cookie=new Cookie("login","true");response.addCookie(cookie);return "這是我的get請求返回cookie的請求結果";}}3、模擬get請求攜帶cookie信息
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Objects;@RestController public class MyGetMethod {//模擬get請求@RequestMapping(value = "/useSpringBoot/get",method = RequestMethod.GET)public String get(){return "這是我的get請求的返回結果";}//模擬get請求返回cookie信息//HttpServletResponse類是一個存儲器,可以把cookie等信息加入到response中返回@RequestMapping(value = "/useSpringBoot/getResponseCookies",method = RequestMethod.GET)public String getResponseCookie(HttpServletResponse response){Cookie cookie=new Cookie("login","true");response.addCookie(cookie);return "這是我的get請求返回cookie的請求結果";}//模擬get請求攜帶cookie信息//HttpServletRequest類是一個存儲器,可以把cookie等信息加入到請求中去訪問get請求@RequestMapping(value = "/useSpringBoot/getwithCookies",method = RequestMethod.GET)public String getwithCookies(HttpServletRequest request){//從get請求攜帶的cookie中獲取cookie,并賦值給cookies數組Cookie[] cookies = request.getCookies();//判斷cookies數組是否為空,如果為空返回錯誤,不為空判斷cookies是不是獲取到的cookiesif(Objects.isNull(cookies)){return "必須攜帶cookie信息";}//foreach循環cookies,對象類型是cookie類,利用cookie類的getName和getValue方法判斷cookies信息for(Cookie cookie : cookies){if(cookie.getName().equals("login") && cookie.getValue().equals("true")){return "這是一個攜帶cookie信息的get請求";}}return "cookies信息校驗失敗";}}4、模擬get請求攜帶參數
package com.course.server;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; import java.util.Objects;@RestController public class MyGetMethod {//模擬get請求@RequestMapping(value = "/useSpringBoot/get",method = RequestMethod.GET)public String get(){return "這是我的get請求的返回結果";}//模擬get請求返回cookie信息//HttpServletResponse類是一個存儲器,可以把cookie等信息加入到response中返回@RequestMapping(value = "/useSpringBoot/getResponseCookies",method = RequestMethod.GET)public String getResponseCookie(HttpServletResponse response){Cookie cookie=new Cookie("login","true");response.addCookie(cookie);return "這是我的get請求返回cookie的請求結果";}//模擬get請求攜帶cookie信息//HttpServletRequest類是一個存儲器,可以把cookie等信息加入到請求中去訪問get請求@RequestMapping(value = "/useSpringBoot/getwithCookies",method = RequestMethod.GET)public String getwithCookies(HttpServletRequest request){//從get請求攜帶的cookie中獲取cookie,并賦值給cookies數組Cookie[] cookies = request.getCookies();//判斷cookies數組是否為空,如果為空返回錯誤,不為空判斷cookies是不是獲取到的cookiesif(Objects.isNull(cookies)){return "必須攜帶cookie信息";}//foreach循環cookies,對象類型是cookie類,利用cookie類的getName和getValue方法判斷cookies信息for(Cookie cookie : cookies){if(cookie.getName().equals("login") && cookie.getValue().equals("true")){return "這是一個攜帶cookie信息的get請求";}}return "cookies信息校驗失敗";}//模擬get請求攜帶參數@RequestMapping(value = "/useSpringBoot/getwithParam",method = RequestMethod.GET)public Map<String,String> getwithParam(@RequestParam("goodsid") String goodsid,@RequestParam("goodsdes") String goodsdes){Map<String,String> map=new HashMap<>();map.put("goodsid",goodsid);map.put("goodsdes",goodsdes);return map;} }5、模擬get請求攜帶參數二
package com.course.server;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*;import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; import java.util.Objects;@RestController public class MyGetMethod {//模擬get請求@RequestMapping(value = "/useSpringBoot/get",method = RequestMethod.GET)public String get(){return "這是我的get請求的返回結果";}//模擬get請求返回cookie信息//HttpServletResponse類是一個存儲器,可以把cookie等信息加入到response中返回@RequestMapping(value = "/useSpringBoot/getResponseCookies",method = RequestMethod.GET)public String getResponseCookie(HttpServletResponse response){Cookie cookie=new Cookie("login","true");response.addCookie(cookie);return "這是我的get請求返回cookie的請求結果";}//模擬get請求攜帶cookie信息//HttpServletRequest類是一個存儲器,可以把cookie等信息加入到請求中去訪問get請求@RequestMapping(value = "/useSpringBoot/getwithCookies",method = RequestMethod.GET)public String getwithCookies(HttpServletRequest request){//從get請求攜帶的cookie中獲取cookie,并賦值給cookies數組Cookie[] cookies = request.getCookies();//判斷cookies數組是否為空,如果為空返回錯誤,不為空判斷cookies是不是獲取到的cookiesif(Objects.isNull(cookies)){return "必須攜帶cookie信息";}//foreach循環cookies,對象類型是cookie類,利用cookie類的getName和getValue方法判斷cookies信息for(Cookie cookie : cookies){if(cookie.getName().equals("login") && cookie.getValue().equals("true")){return "這是一個攜帶cookie信息的get請求";}}return "cookies信息校驗失敗";}//模擬get請求攜帶參數@RequestMapping(value = "/useSpringBoot/getwithParam",method = RequestMethod.GET)public Map<String,String> getwithParam(@RequestParam("goodsid") String goodsid,@RequestParam("goodsdes") String goodsdes){Map<String,String> map=new HashMap<>();map.put("goodsid",goodsid);map.put("goodsdes",goodsdes);return map;}//模擬get請求攜帶參數 @PathVariable@RequestMapping(value = "/useSpringBoot/getwithParamtwo/{goodsid}/{goodsdes}",method = RequestMethod.GET)public Map<String,String> getwithParamtwo(@PathVariable("goodsid") String goodsid,@PathVariable("goodsdes") String goodsdes){Map<String,String> map=new HashMap<>();map.put("goodsid",goodsid);map.put("goodsdes",goodsdes);return map;} }6、模擬post請求返回cookies信息
package com.course.server;import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse;@RestController @Api(value = "/",description = "post請求接口") public class MyPostMethod {//定義cookie全局變量,供需要攜帶cookie的接口使用Cookie cookie;//定義一個login的post方法,然后返回cookie信息@RequestMapping(value = "/useSpringBoot/postlogin",method = RequestMethod.POST)@ApiOperation(value = "登錄接口返回cookie信息",httpMethod = "POST")public String Login(HttpServletResponse response,@RequestParam("name") String name,@RequestParam("password") String password){//如果用戶名和密碼成功,就添加cookie信息,然后添加到response中,然后成功信息if(name.equals("qinzhenxia") && password.equals("123456")){cookie=new Cookie("token","1234567890");response.addCookie(cookie);return "返回cookie,登錄成功";}return "登錄失敗";}}7、模擬post請求攜帶cookies信息
package com.course.server;import com.course.Bean.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;@RestController @Api(value = "/",description = "post請求接口") public class MyPostMethod {//定義cookie全局變量,供需要攜帶cookie的接口使用Cookie cookie;//定義一個login的post方法,然后返回cookie信息@RequestMapping(value = "/useSpringBoot/postlogin", method = RequestMethod.POST)@ApiOperation(value = "登錄接口返回cookie信息", httpMethod = "POST")public String Login(HttpServletResponse response,@RequestParam("name") String name,@RequestParam("password") String password) {//如果用戶名和密碼成功,就添加cookie信息,然后添加到response中,然后成功信息if (name.equals("qinzhenxia") && password.equals("123456")) {cookie = new Cookie("token", "1234567890");//在response中添加cookie并返回cookieresponse.addCookie(cookie);return "返回cookie,登錄成功";}return "success";}//創建users對象,getusers接口會用到該對象創建用戶登錄信息。User users;//用postman請求Login接口,會自動存儲cookie,再請求getusers接口時會自動把cookie帶上,所以請求時不需要再單獨處理cookie//攜帶cookie信息請求接口,同時返回用戶列表的post接口@RequestMapping(value = "/useSpringBoot/postResponseUsers", method = RequestMethod.POST)@ApiOperation(value = "攜帶cookie信息請求接口,同時返回用戶列表的post接口", httpMethod = "POST")public User getusers(HttpServletRequest request, @RequestBody User user) {//上一個接口返回的cookie,本接口去獲取Cookie[] cookie=request.getCookies();for(Cookie cookie1 : cookie){if(cookie1.getName().equals("token") && cookie1.getValue().equals("1234567890") &&user.getLoginname().equals("qinzhenxia") && user.getPassword().equals("123456")){//接口的請求參數是Loginname和Password//接口的返回參數是Loginname、Password、Name、Age、Sex//請求和返回的格式都是json//給user類中的屬性賦值并返回users=new User();users.setLoginname("qinzhenxia");users.setPassword("123456");users.setName("sunqihua");users.setAge(28);users.setSex("男");return users;}}return null;} }總結
以上是生活随笔為你收集整理的SpringBoot开发接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做题总结(一)
- 下一篇: Axiso解决跨域访问 !!!!