项目实战接口开发SpringBoot
一、springboot官方demo开发
- 依赖包和父:pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.14</version>
</dependency>
</dependencies>
- 新建 SampleController.java
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home(){
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(SampleController.class,args);
}
}
- 运行结果

说明:内置了web服务器
二、使用SpringBoot开发get方法接口
返回cookie信息的get接口开发
- 新建Application.java 入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.course.server")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
- com.course.server 新建MyGetMethod.java
@RestController
public class MyGetMethod{
@RequestMapping(value="/getCookies",method=RequestMethod.GET)
public String getCookies(){
return "恭喜你获得cookies信息成功";
}
}
- Resource下新建文件:application.properties
server.port=${port:8888}
启动后访问

获得cookies
修改com.course.server.MyGetMethod.java 代码:
package com.course.server;
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 {
@RequestMapping(value = "/getCookies",method= RequestMethod.GET)
public String getCookies(HttpServletResponse response){
// HttpServletRequest 装请求信息得类
// HttpServletResponse 装响应信息得类
Cookie cookie = new Cookie("login", "true");
response.addCookie(cookie);
return "恭喜你获得cookies信息成功";
}
}
运行:

三、一个要求携带cookie信息访问的get接口开发
- MyGetMethod.java 新增方法:
@RestController
public class MyGetMethod{
@RequestMapping(value="/get/with/Cookies",method=RequestMethod.GET)
public String getWithCookies(HttpServletRequest request){
// HttpServletRequest 装请求信息的类
// HttpServletResponse 装响应信息的类
Cookie[] cookies = request.getCookies();
if(Objects.isNull(cookies)){
return "你必须携带cookies信息来";
}
for(Cookie cookie:cookies){
if(cookie.getName().equals("login") &&
cookie.getValue().equals("true")){
return "恭喜你访问成功!";
}
}
return "你必须携带cookies信息来";
}
}
- Jemeter访问
1)加一个线程组
2)加一个HTTP请求
3)加一个HTTP Cookie管理器

4)加一个查看结果树

四、需要携带参数的get请求两种开发方式
4.1 方式1:url:key=value&key=value
@RestController
public class MyGetMethod{
@RequestMapping(value="/get/with/param",method=RequestMethod.GET)
public Map<String,Integer> getList(@RequestParam Integer start,
@RequestParam Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",500);
myList.put("衣服",200);
myList.put("干脆面",1);
return myList;
}
}
结果:

4.2 方式2:url:ip:port/get/with/param/10/20
@RequestMapping(value = "/get/with/param/{start}/{end}",method = RequestMethod.GET)
public Map<String,Integer> getList(@RequestParam(required = false) Integer start,
@RequestParam(required = false) Integer end){
Map<String,Integer> myList = new HashMap<>();
myList.put("鞋",500);
myList.put("衣服",200);
myList.put("干脆面",1);
return myList;
}
结果:

五、使用SpringBoot开发post方法接口
- 新增MyPostMethod.java
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
@RestController
@RequestMapping("/v1")
public class MyPostMethod{
// 这个变量用来装我们的cookies信息
private static Cookie cookie;
// 用户登录成功获取到cookies,然后再访问其他接口获取到列表
@RequestMapping(value="/login",method=RequestMethod.POST)
@ApiOperation(value="登陆接口,成功后获取cookies信息",httpMethod="POST")
public String login(HttpServletResponse response,
@RequestParam(value="userName",required=true) String userName,
@RequestParam(value="password",required=true) String password){
if(userName.equals("zhangsan")&&password.equals("123456")){
cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你登录成功了!";
}
return "用户名或者密码错误!";
}
}
- 在Jmeter中测试该接口




六、Cookie验证和返回用户列表的post接口开发
- 新增lombok依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
- 新增类 com/course/bean/User.java
package com.course.bean;
import lombok.Data;
@Data
public class User {
private String userName;
private String password;
private String name;
private String age;
private String sex;
}
- 新增类 com/course/server/MyPostMethod.java
package com.course.server;
import com.course.bean.User;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/v1")
public class MyPostMethod {
// 这个变量用来装我们的cookies信息
private static Cookie cookie;
// 用户登录成功获取到cookies,然后再访问其他接口获取到列表
@RequestMapping(value="/login",method= RequestMethod.POST)
public String login(HttpServletResponse response,
@RequestParam(value="userName",required=true) String userName,
@RequestParam(value="password",required=true) String password){
if(userName.equals("zhangsan")&&password.equals("123456")){
cookie = new Cookie("login","true");
response.addCookie(cookie);
return "恭喜你登录成功了!";
}
return "用户名或者密码错误!";
}
@RequestMapping(value="/getUserList",method = RequestMethod.POST)
public String getUserList(HttpServletRequest request,
@RequestBody User u){
// 获取cookies
Cookie[] cookies = request.getCookies();
// 验证cookies是否合法
for (Cookie c:cookies){
if (c.getName().equals("login") && c.getValue().equals("true") && u.getUserName().equals("zhangsan") && u.getPassword().equals("123456")){
User user = new User();
user.setName("lisi");
user.setAge("14");
user.setSex("man");
return user.toString();
}
}
return "参数不合法";
}
}
- 启动Application.java
- 使用Jemeter测试接口
1)新建线程组
2)新增HTTP Header Manager

3)新增HTTP Cookie Manager

4)新增HTTP Request

5)添加结果树


项目实战接口开发SpringBoot的更多相关文章
- 实战接口开发:python + flask + mysql + redis(根据反馈,持续细化更新。。。)
前言 自动化已经成为测试的必备技能之一了,所以,很多想跳槽的测试朋友都在自学,特别是最实用的接口自动化, 但是很多人因为没有可以练手的项目而苦恼,最终导致缺乏实战经验,其实,完全可以自己开发个简单项目 ...
- 【Spring Cloud & Alibaba全栈开源项目实战】:SpringBoot整合ELK实现分布式登录日志收集和统计
一. 前言 其实早前就想计划出这篇文章,但是最近主要精力在完善微服务.系统权限设计.微信小程序和管理前端的功能,不过好在有群里小伙伴的一起帮忙反馈问题,基础版的功能已经差不多,也在此谢过,希望今后大家 ...
- luffy项目的接口开发
处理跨域请求 主要的思路: 设置一个基于CORS的中间件来处理,关于跨域的产生与处理手段 settings.py: MIDDLEWARE = [ 'django.middleware.security ...
- Laravel实现大型商城网站之用户注册短信发送项目实战功能开发
确定短信运营商 我这里采用的云片,不过大家使用其它的也可以. 首先自己注册一个帐号,然后找到这个 点击开始接入,完成新手引导过程. 第二部的签名和模板必须填写,类似我下面填写的这样 值得注意的是这个模 ...
- React.js 入门与实战之开发适配PC端及移动端新闻头条平台课程上线了
原文发表于我的技术博客 我在慕课网的「React.js 入门与实战之开发适配PC端及移动端新闻头条平台」课程已经上线了,文章中是目前整个课程的大纲,以后此课程还会保持持续更新,此大纲文档也会保持更新, ...
- Java 18套JAVA企业级大型项目实战分布式架构高并发高可用微服务电商项目实战架构
Java 开发环境:idea https://www.jianshu.com/p/7a824fea1ce7 从无到有构建大型电商微服务架构三个阶段SpringBoot+SpringCloud+Solr ...
- 基于Linux下的C语言项目实战--本地账号管理系统
C语言开发项目实战: C语言是一门通用计算机编程语言,广泛应用于底层开发.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言.尽 ...
- SpringBoot项目后台对接微信支付开发——微信统一下单接口开发
开始没找到微信支付的sdk.自己根据官方给的接口文档纯手写,各种xml转JSON,JSON转xml,加密解密,签名....整个人都是崩溃的 开发的第三天,发现有官方的sdk.心情一下子豁然开朗,整个人 ...
- SpringBoot电商项目实战 — 前后端分离后的优雅部署及Nginx部署实现
在如今的SpringBoot微服务项目中,前后端分离已成为业界标准使用方式,通过使用nginx等代理方式有效的进行解耦,并且前后端分离会为以后的大型分布式架构.弹性计算架构.微服务架构.多端化服务(多 ...
- Spring Boot微服务电商项目开发实战 --- 分布式开发要避的那些坑
今天已经进入第七讲了,整个微服务架构的搭建工作也基本完成.那到目前为止究竟使用了那些技术及实现了什么功能呢?我们先回顾一下. 使用的技术:SpringBoot.Dubbo.Zookeeper.Redi ...
随机推荐
- Python 基础面试第二弹
1. 解释下Python中的面向对象,以及面向对象的三大特点: 在Python中,面向对象编程(Object-Oriented Programming,简称OOP)是一种编程范式,它将数据和操作数据的 ...
- Ceph-介绍
Ceph架构简介及使用场景介绍 一.Ceph简介 Ceph是一个统一的分布式存储系统,设计初衷是提供较好的性能.可靠性和可扩展性. 二.Ceph特点 1.高性能 - 采用CRUSH算法,数据分布均衡, ...
- 从达梦数据库到Oracle数据库的性能测试数据迁移和导入优化
为了在同样的数据基础上对比达梦数据库和Oracle数据库的业务性能,我们需要将达梦数据库的数据导入到Oracle数据库中.本文将提供一种思路来解决导入过程中遇到的问题及存在问题记录. 数据库版本信息 ...
- Nomad 系列-Nomad 挂载存储卷
系列文章 Nomad 系列文章 概述 显然,如果 Nomad 要运行有状态存储,那么挂载存储卷就是必备功能. Nomad 允许用户通过多种方式将持久数据从本地或远程存储卷装载到任务环境中: 容器存储接 ...
- k8s证书到期处理
证书续期提示 当执行kubectl get nodes等提示 Unable to connect to the server: x509: certificate has expired or is ...
- GO 中的时间操作(time & dateparse)【GO 基础】
〇.前言 日常开发过程中,对于时间的操作可谓是无处不在,但是想实现时间自由还是不简单的,多种时间格式容易混淆,那么本文将进行梳理,一起学习下. 官方提供的库是 time,功能很全面,本文也会详细介绍. ...
- mpi转以太网连接200PLC转以太网modbusTCP服务器通信配置方法
兴达易控200PLC转以太网modbusTCP服务器通信配置方法 产品简介 兴达易控PPI-ETH-XD1.0用于西门子S7-200/SMART S7-200PLC的以太网数据采集,非常方便构建生产管 ...
- MySQL系列之MHA高可用——主从复制架构演变介绍、高可用MHA、管理员在高可用架构维护的职责
文章目录 1. 主从复制架构演变介绍 1.1 基本结构 1.2 高级应用架构演变 1.2.1 高性能架构 1.2.2 高可用架构 2. 高可用MHA ***** 2.1 架构工作原理 2.2 架构介绍 ...
- os --- 多种操作系统接口¶
os.path --- 常用路径操作 源代码: Lib/posixpath.py (用于 POSIX) 和 Lib/ntpath.py (用于 Windows). 此模块实现了一些有用的路径名称相关函 ...
- ActivityNotFoundException
activity 加入 AndroidManifest android.content.ActivityNotFoundException: Unable to find explicit acti ...