Tips & Tricks

How to use HTTP Client in IntelliJ IDEA for Spring Boot RESTful Web Services

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.

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.

Download IntelliJ IDEA

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.

Spring demo application in IntelliJ IDEA

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.

Generate HTTP request right in the IDE

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.

Endpoints tool window in IntelliJ IDEA

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 Actuator 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 HTTP Client.

Actuator tab in IntelliJ IDEA

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, SpringBootTutorialApplication.http).

Scratch file in HTTP Client

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 on Windows or ⌥Enter on macOS 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 request in HTTP Client

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.

Run cURL request in HTTP Client

When you compose requests manually, IntelliJ IDEA provides completion for URLs based on the REST endpoints defined in your code. This means you don’t need to find the REST controllers to generate requests from them. Simply start typing the request in the HTTP request file, and the IDE will show you suggestions from the available URLs. You can also press ⌃Ctrl + Space or select Code | Code Completion | Basic to show completion suggestions.

Request autocompletion in HTTP Client

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

Download IntelliJ IDEA today to try out these features. To learn more about the features for working with Spring in IntelliJ IDEA, visit this web page.

Happy developing!

image description