java spring get请求_Springboot+get请求接口

packagecom.course.server;importio.swagger.annotations.Api;importio.swagger.annotations.ApiOperation;import org.springframework.web.bind.annotation.*;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.util.HashMap;importjava.util.Map;importjava.util.Objects;/*** 返回一个cookies的get请求接口*/@RestController

@Api(value= "/", description = "这是我全部的get方法")public classMyGetMethod {

@RequestMapping(value= "/getCookies", method =RequestMethod.GET)

@ApiOperation(value= "通过这个方法可以获取到cookies", httpMethod = "Get")publicString getCookies(HttpServletResponse response) {//HttpServletRequest 装请求信息的类//HttpServletResponse 装响应信息的类 是一个类,不是参数

Cookie cookie = new Cookie("login", "true");

response.addCookie(cookie);return "恭喜你获得cookies信息成功!";

}/*** 要求客户端带cookies访问*/@RequestMapping(value= "get/with/cookies", method =RequestMethod.GET)

@ApiOperation(value= "要求客户端带cookies访问", httpMethod = "Get")publicString getWithCookies(HttpServletRequest request) {

Cookie[] cookies=request.getCookies();if(Objects.isNull(cookies)) {return "你必须携带一个cookie!";

}for(Cookie cookie : cookies) {if (cookie.getName().equals("login") && cookie.getValue().equals("true")) {return "恭喜你访问成功!";

}else{return "你的cookie值错误";

}

}return "你必须携带一个cookie!";

}/*** 开发一个需要携带参数的get 请求

* 第一种实现方式 url: key=value&key=value

* 模拟获取商品列表*/@RequestMapping(value= "/get/with/param", method =RequestMethod.GET)

@ApiOperation(value= "开发一个需要携带参数的get 请求, url: key=value&key=value", httpMethod = "GET")public MapgetList(@RequestParam Integer start, @RequestParam Integer end) {

Map mylist = new HashMap<>();

mylist.put("鞋", 400);

mylist.put("衬衫", 300);

mylist.put("裤子", 400);returnmylist;

}/*** 第二种需要携带参数访问的get请求

* url: ip:port/get/with/param/10/20http://127.0.0.1:8080/get/with/param/10/20*/@RequestMapping(value= "/get/with/param/{start}/{end}")

@ApiOperation(value= "开发一个需要携带参数的get 请求, url: ip:port/get/with/param/10/20", httpMethod = "GET")publicMap myGetList(@PathVariable Integer start, @PathVariable Integer end) {

Map mylist = new HashMap<>();

mylist.put("鞋", 400);

mylist.put("衬衫", 300);

mylist.put("裤子", 400);returnmylist;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值