springboot 访问api 测试

springboot 测试访问APIimport org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.ht
摘要由CSDN通过智能技术生成

springboot 测试访问API


import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;


@Transactional //测试后数据会回滚
@WebAppConfiguration
@Sp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它提供了许多便利的功能,使得开发API接口变得更加简单和高效。 在Spring Boot中开发API接口,可以借助Swagger2来构建强大的RESTful API文档。Swagger2是一个用于描述、构建和生成RESTful风格API的工具集。它可以自动生成API文档,并提供了一个可视化的界面,方便开发人员查阅和测试API接口。 以下是使用Spring Boot和Swagger2开发API接口的步骤: 1. 添加依赖:在项目的pom.xml文件中添加Swagger2和Spring Boot的相关依赖。 2. 配置Swagger2:创建一个Swagger2配置类,使用注解@EnableSwagger2启用Swagger2,并配置API文档的基本信息,如标题、描述、版本等。 3. 编写API接口:在Spring Boot的控制器类中编写API接口的方法,并使用Swagger2的注解来描述接口的信息,如接口的路径、请求方法、参数等。 4. 启动应用程序:运行Spring Boot应用程序,访问Swagger2的可视化界面,即可查看和测试API接口。 以下是一个示例代码,演示了如何使用Spring Boot和Swagger2开发API接口: ```java @RestController @Api(tags = "用户管理接口") public class UserController { @ApiOperation("获取用户列表") @GetMapping("/users") public List<User> getUsers() { // 获取用户列表的逻辑 } @ApiOperation("创建用户") @PostMapping("/users") public User createUser(@RequestBody User user) { // 创建用户的逻辑 } @ApiOperation("更新用户") @PutMapping("/users/{id}") public User updateUser(@PathVariable("id") Long id, @RequestBody User user) { // 更新用户的逻辑 } @ApiOperation("删除用户") @DeleteMapping("/users/{id}") public void deleteUser(@PathVariable("id") Long id) { // 删除用户的逻辑 } } ``` 通过以上步骤,我们可以使用Spring Boot和Swagger2快速开发API接口,并且可以方便地查阅和测试API文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值