1. Overview
1.概述
Postman is a popular API platform that optimizes the various steps of the API development lifecycle. Postman can be used to test our API without writing any code. We can use either the standalone app or the browser extension.
Postman是一个流行的API平台,它优化了API开发生命周期的各个步骤。Postman可用于测试我们的API,无需编写任何代码。我们可以使用独立的应用程序或浏览器扩展。
In this tutorial, we’ll see how to upload files and JSON data when using Postman.
在本教程中,我们将看到如何在使用Postman时上传文件和JSON数据。
2. Application Setup
2.应用设置
Let’s set up a basic Spring Boot application that exposes endpoints to upload data.
让我们建立一个基本的Spring Boot应用程序,暴露出上传数据的端点。
2.1. Dependencies
2.1. 依赖性
We defined a basic spring application with spring-boot-starter-web dependency in pom.xml:
我们在pom.xml中定义了一个带有spring-boot-starter-web依赖的基本spring应用程序。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.2. Model
2.2 模式
Next, let’s define a simple model class for the JSON input:
接下来,让我们为JSON输入定义一个简单的模型类。
public class JsonRequest {
int id;
String name;
}
For brevity, we’ve removed the declarations for constructors, getters/setters, etc.
为了简洁起见,我们删除了构造函数、getters/setters等的声明。
2.3. Endpoints
2.3.端点
Finally, let’s set up an endpoint as per the use case to handle requests as a file:
最后,让我们按照用例设置一个端点,以处理作为文件的请求。
@PostMapping("/uploadFile")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file){
return ResponseEntity.ok().body("file received successfully");
}
In the method handleFileUpload(), we expect a MultipartFile as input and subsequently return a 200 status message with a static text. We’ve kept it simple and haven’t explored saving or processing the input file.
在方法handleFileUpload()中,我们期待一个MultipartFile作为输入,并随后返回一个带有静态文本的200状态消息。我们保持简单,没有探讨保存或处理输入文件的问题。
The MultipartFile is provided by Spring-Web and it represents an uploaded file. This file is then stored in memory or temporarily on disk, which is subsequently flushed out once the request processing is complete.
MultipartFile由Spring-Web提供,它代表一个上传的文件。然后这个文件被存储在内存中或暂时存储在磁盘上,随后在请求处理完成后被刷新出来。
Let’s also create an endpoint that handles JSON data:
让我们也创建一个处理JSON数据的端点。
@PostMapping("/uploadJson")
public ResponseEntity<String> handleJsonInput(@RequestBody JsonRequest json){
return ResponseEntity.ok().body(json.getId()+json.getName());
}
Here, handleJsonInput(), we expect an object of type JsonRequest, the model class we’ve defined. The method returns a 200 HTTP status code with the input details id and name in the response.
在这里,handleJsonInput(),我们期待一个JsonRequest类型的对象,我们定义的模型类。该方法返回一个200 HTTP状态代码,并在响应中提供输入的详细信息id和name。
We’ve used the annotation @RequestBody which deserializes the input into the JsonRequest object. This way, we’ve seen simplistic processing of the JSON to verify the input.
我们使用了注解@RequestBody ,该注解将输入反序列化为JsonRequest 对象。这样,我们已经看到了对JSON的简单处理,以验证输入。
3. Uploading Data
3.上传数据
We’ve set up the application and now let’s check the two ways to provide input to the application.
我们已经设置了应用程序,现在让我们检查一下向应用程序提供输入的两种方式。
3.1. Uploading JSON Into Postman
3.1.将JSON上传至Postman
JSON is one of the text input types for an endpoint. We’ll follow the below steps to pass the same to the exposed endpoint.
JSON是一个端点的文本输入类型之一。我们将按照下面的步骤,把同样的内容传递给暴露的端点。
The default method is set to GET. So once we’ve added the localhost URL, we need to select POST as the method:
默认方法被设置为GET。所以一旦我们添加了localhost URL,我们需要选择POST作为方法:
Let’s click on the Body tab, then select raw. In the dropdown which displays Text, let’s select JSON as the input:
让我们点击Body标签,然后选择raw。在显示Text的下拉菜单中,让我们选择JSON作为输入。
We need to paste the input JSON and then click Send:
我们需要粘贴输入的JSON,然后点击Send。
We’re getting a 200 status code in response as we can see at the bottom of the snapshot. Additionally, the id and name from the input are returned in the response body, confirming that the JSON was processed correctly at the endpoint.
我们得到了一个200状态代码的响应,我们可以在快照的底部看到。此外,输入的id和name在响应体中被返回,确认JSON在端点被正确处理。
3.2. Uploading a File Into Postman
3.2.将文件上传到Postman中
Let’s take a document file for our example here as we’ve not defined any constraints as to which file types can be consumed by the endpoint.
让我们在这里以文档文件为例,因为我们没有对哪些文件类型可以被端点消费定义任何约束。
Let’s add the localhost URL and select POST as the method since the method defaults to GET:
让我们添加localhost URL并选择POST作为方法,因为该方法默认为GET。
Let’s click on the Body tab, then select form-data. On the first row for the key-value pair, let’s click on the dropdown at the right corner for the key field, and select File as the input:
让我们点击Body标签,然后选择form-data。在第一行的键值对上,让我们点击右角的键字段下拉菜单,选择文件作为输入。
We need to add the text file which is our @RequestParam for the endpoint, in the key column and browse the desired file for the value column.
我们需要在关键列中添加文本file,这是我们的@RequestParam的端点,并在值列中浏览所需文件。
Finally, let’s click on Send:
最后,让我们点击Send。
When we click on Send, we get a 200 HTTP status code with the static text defined in our endpoint definition. This means our file was successfully delivered to the endpoint with no errors or exceptions.
当我们点击Send时,我们得到一个200HTTP状态代码,其中包含我们端点定义中的静态文本。这意味着我们的文件被成功地传送到端点,没有任何错误或异常。
4. Conclusion
4.总结
In this article, we built a simple Spring Boot application and looked at two different ways to provide data to exposed endpoints via Postman.
在这篇文章中,我们建立了一个简单的Spring Boot应用程序,并研究了通过Postman向暴露的端点提供数据的两种不同方式。
As always, code samples are available over on GitHub.
像往常一样,代码样本可在GitHub上获得。