Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Jackson Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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.

In this tutorial, we'll see how to upload files and JSON data when using Postman.

2. Application Setup

Let's set up a basic Spring Boot application that exposes endpoints to upload data.

2.1. Dependencies

We defined a basic spring application with spring-boot-starter-web dependency in pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.2. Model

Next, let's define a simple model class for the JSON input:

public class JsonRequest {
    int id;
    String name;
}

For brevity, we've removed the declarations for constructors, getters/setters, etc.

2.3. Endpoints

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.

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.

Let's also create an endpoint that handles JSON data:

@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.

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.

3. Uploading Data

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

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.

The default method is set to GET. So once we've added the localhost URL, we need to select POST as the method:

Let's click on the Body tab, then select raw. In the dropdown which displays Text, let's select JSON as the input:

We need to paste the input JSON and then click 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.

3.2. Uploading a File Into 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:

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:

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.

Finally, let's click on 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.

4. Conclusion

In this article, we built a simple Spring Boot application and looked at two different ways to provide data to exposed endpoints via Postman.

As always, code samples are available over on GitHub.

Jackson bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!