At Your Request: Use the HTTP Client in IntelliJ IDEA for Spring Boot RESTful Web Services

Alexey Zhebel

Spring Boot is great for developing web services. A request handler (for example, a REST controller) is where you define methods that handle requests to specific endpoints. To test those requests, you could use an external tool, but with IntelliJ IDEA, you don’t need to leave your IDE. The integrated HTTP client can handle it for you.

The HTTP client in IntelliJ IDEA is built directly into the editor and it is purely text-based. This means you get full coding assistance for your HTTP requests, including highlighting, completion, refactorings, inline documentation, and so on. What’s more, you can generate HTTP requests from the source code of your RESTful web service, for example, built with Spring Boot. Let’s see how this works in IntelliJ IDEA Ultimate 2020.2.

IntelliJ IDEA: how2pro banner

Wherever you define a request handling method in your code, IntelliJ IDEA displays a special icon next to it in the gutter. In the following example, the sayHello() method is annotated with @GetMapping, making it a GET request handler for /hello.

Example Spring Boot REST application

If you click the Open in HTTP Client gutter icon next to the sayHello() method, IntelliJ IDEA will generate the GET request and add it to a scratch file named generated-requests.http.

Generated GET request

You can easily execute requests from this file during development to verify that a web service is behaving as expected. IntelliJ IDEA can generate requests from various places and add them to this scratch file. For example, you can generate requests from endpoints listed in the Endpoints tool window.

Generate HTTP request from the Endpoints tool window

If you have the Spring Boot Actuator dependency, it exposes all of your web service endpoints at runtime. In the Run tool window or the Services tool window, select your running application and open the Endpoints tab. Under Mappings, you will see a list of all the request mappings. Click one of them to either execute the corresponding request or open it in the HTTP client.

Generate HTTP request from the Mappings tab

Note that since these are runtime endpoints, the requests are added to a different scratch file, which is named after the specific run configuration (in this case, DemoApplication.http).

Request file for a runtime endpoint

Scratch files are not stored in your project. To create a physical file with HTTP requests in your project, right-click on a directory in the Project tool window and select New | HTTP Request. In this file, you can compose a set of HTTP requests for validating your web service. This file will not be modified unless you edit it directly.

You do not need to manually copy generated HTTP requests from the scratch file to the physical HTTP requests file: there is a context action for that. Place the cursor on the generated request, press Alt + Enter and select the Move HTTP Requests action. In the Move HTTP Requests dialog, specify the file to which you want to move the requests and select the specific requests to move.

Move HTTP Requests dialog

If you specify a file that doesn’t exist, IntelliJ IDEA will create it for you. Your project will contain a file with HTTP requests that you or anyone else working with your project can use to validate the web service.

If you already have a number of curl requests that you use (for example, in a shell script), you can simply paste them into the HTTP requests file and IntelliJ IDEA will magically convert them to the format used by the integrated HTTP client.

HTTP request converted from curl

When you compose requests manually, IntelliJ IDEA provides completion for URLs based on the REST endpoints defined in your code. So you don’t need to find your REST controllers to generate requests from them, just start typing the request in the HTTP request file and the IDE will give you suggestions from available URLs.

HTTP request completion

For more information, see the following topics in the IntelliJ IDEA help:

Download IntelliJ IDEA Ultimate today to try out these features.

Happy developing!

Subscribe

Subscribe for updates