经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
项目实战接口开发SpringBoot
来源:cnblogs  作者:hqq的进阶日记  时间:2023/12/15 8:46:26  对本文有异议

一、springboot官方demo开发

  1. 依赖包和父:pom.xml
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.7.14</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. <version>2.7.14</version>
  11. </dependency>
  12. </dependencies>
  1. 新建 SampleController.java
  1. import org.springframework.boot.*;
  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. @Controller
  7. @EnableAutoConfiguration
  8. public class SampleController {
  9. @RequestMapping("/")
  10. @ResponseBody
  11. String home(){
  12. return "Hello World!";
  13. }
  14. public static void main(String[] args) {
  15. SpringApplication.run(SampleController.class,args);
  16. }
  17. }
  1. 运行结果

    说明:内置了web服务器

二、使用SpringBoot开发get方法接口

返回cookie信息的get接口开发

  1. 新建Application.java 入口
  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.context.annotation.ComponentScan;
  4. @SpringBootApplication
  5. @ComponentScan("com.course.server")
  6. public class Application {
  7. public static void main(String[] args) {
  8. SpringApplication.run(Application.class,args);
  9. }
  10. }
  1. com.course.server 新建MyGetMethod.java
  1. @RestController
  2. public class MyGetMethod{
  3. @RequestMapping(value="/getCookies",method=RequestMethod.GET)
  4. public String getCookies(){
  5. return "恭喜你获得cookies信息成功";
  6. }
  7. }
  1. Resource下新建文件:application.properties
  1. server.port=${port:8888}
  1. 启动后访问

  2. 获得cookies
    修改com.course.server.MyGetMethod.java 代码:

  1. package com.course.server;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RequestMethod;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import javax.servlet.http.Cookie;
  6. import javax.servlet.http.HttpServletResponse;
  7. @RestController
  8. public class MyGetMethod {
  9. @RequestMapping(value = "/getCookies",method= RequestMethod.GET)
  10. public String getCookies(HttpServletResponse response){
  11. // HttpServletRequest 装请求信息得类
  12. // HttpServletResponse 装响应信息得类
  13. Cookie cookie = new Cookie("login", "true");
  14. response.addCookie(cookie);
  15. return "恭喜你获得cookies信息成功";
  16. }
  17. }

运行:

三、一个要求携带cookie信息访问的get接口开发

  1. MyGetMethod.java 新增方法:
  1. @RestController
  2. public class MyGetMethod{
  3. @RequestMapping(value="/get/with/Cookies",method=RequestMethod.GET)
  4. public String getWithCookies(HttpServletRequest request){
  5. // HttpServletRequest 装请求信息的类
  6. // HttpServletResponse 装响应信息的类
  7. Cookie[] cookies = request.getCookies();
  8. if(Objects.isNull(cookies)){
  9. return "你必须携带cookies信息来";
  10. }
  11. for(Cookie cookie:cookies){
  12. if(cookie.getName().equals("login") &&
  13. cookie.getValue().equals("true")){
  14. return "恭喜你访问成功!";
  15. }
  16. }
  17. return "你必须携带cookies信息来";
  18. }
  19. }
  1. Jemeter访问
    1)加一个线程组
    2)加一个HTTP请求
    3)加一个HTTP Cookie管理器

    4)加一个查看结果树

四、需要携带参数的get请求两种开发方式

4.1 方式1:url:key=value&key=value

  1. @RestController
  2. public class MyGetMethod{
  3. @RequestMapping(value="/get/with/param",method=RequestMethod.GET)
  4. public Map<String,Integer> getList(@RequestParam Integer start,
  5. @RequestParam Integer end){
  6. Map<String,Integer> myList = new HashMap<>();
  7. myList.put("鞋",500);
  8. myList.put("衣服",200);
  9. myList.put("干脆面",1);
  10. return myList;
  11. }
  12. }

结果:

4.2 方式2:url:ip:port/get/with/param/10/20

  1. @RequestMapping(value = "/get/with/param/{start}/{end}",method = RequestMethod.GET)
  2. public Map<String,Integer> getList(@RequestParam(required = false) Integer start,
  3. @RequestParam(required = false) Integer end){
  4. Map<String,Integer> myList = new HashMap<>();
  5. myList.put("鞋",500);
  6. myList.put("衣服",200);
  7. myList.put("干脆面",1);
  8. return myList;
  9. }

结果:

五、使用SpringBoot开发post方法接口

  1. 新增MyPostMethod.java
  1. import io.swagger.annotations.Api;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. import javax.servlet.http.Cookie;
  5. @RestController
  6. @RequestMapping("/v1")
  7. public class MyPostMethod{
  8. // 这个变量用来装我们的cookies信息
  9. private static Cookie cookie;
  10. // 用户登录成功获取到cookies,然后再访问其他接口获取到列表
  11. @RequestMapping(value="/login",method=RequestMethod.POST)
  12. @ApiOperation(value="登陆接口,成功后获取cookies信息",httpMethod="POST")
  13. public String login(HttpServletResponse response,
  14. @RequestParam(value="userName",required=true) String userName,
  15. @RequestParam(value="password",required=true) String password){
  16. if(userName.equals("zhangsan")&&password.equals("123456")){
  17. cookie = new Cookie("login","true");
  18. response.addCookie(cookie);
  19. return "恭喜你登录成功了!";
  20. }
  21. return "用户名或者密码错误!";
  22. }
  23. }
  1. 在Jmeter中测试该接口



六、Cookie验证和返回用户列表的post接口开发

  1. 新增lombok依赖
  1. <dependency>
  2. <groupId>org.projectlombok</groupId>
  3. <artifactId>lombok</artifactId>
  4. <version>RELEASE</version>
  5. <scope>compile</scope>
  6. </dependency>
  1. 新增类 com/course/bean/User.java
  1. package com.course.bean;
  2. import lombok.Data;
  3. @Data
  4. public class User {
  5. private String userName;
  6. private String password;
  7. private String name;
  8. private String age;
  9. private String sex;
  10. }
  1. 新增类 com/course/server/MyPostMethod.java
  1. package com.course.server;
  2. import com.course.bean.User;
  3. import org.springframework.web.bind.annotation.*;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. @RestController
  8. @RequestMapping("/v1")
  9. public class MyPostMethod {
  10. // 这个变量用来装我们的cookies信息
  11. private static Cookie cookie;
  12. // 用户登录成功获取到cookies,然后再访问其他接口获取到列表
  13. @RequestMapping(value="/login",method= RequestMethod.POST)
  14. public String login(HttpServletResponse response,
  15. @RequestParam(value="userName",required=true) String userName,
  16. @RequestParam(value="password",required=true) String password){
  17. if(userName.equals("zhangsan")&&password.equals("123456")){
  18. cookie = new Cookie("login","true");
  19. response.addCookie(cookie);
  20. return "恭喜你登录成功了!";
  21. }
  22. return "用户名或者密码错误!";
  23. }
  24. @RequestMapping(value="/getUserList",method = RequestMethod.POST)
  25. public String getUserList(HttpServletRequest request,
  26. @RequestBody User u){
  27. // 获取cookies
  28. Cookie[] cookies = request.getCookies();
  29. // 验证cookies是否合法
  30. for (Cookie c:cookies){
  31. if (c.getName().equals("login") && c.getValue().equals("true") && u.getUserName().equals("zhangsan") && u.getPassword().equals("123456")){
  32. User user = new User();
  33. user.setName("lisi");
  34. user.setAge("14");
  35. user.setSex("man");
  36. return user.toString();
  37. }
  38. }
  39. return "参数不合法";
  40. }
  41. }
  1. 启动Application.java
  2. 使用Jemeter测试接口
    1)新建线程组
    2)新增HTTP Header Manager

    3)新增HTTP Cookie Manager

    4)新增HTTP Request

    5)添加结果树

原文链接:https://www.cnblogs.com/hqq2019-10/p/17901677.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号