{"id":78902,"date":"2020-09-24T14:52:20","date_gmt":"2020-09-24T14:52:20","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=idea&#038;p=78902"},"modified":"2023-12-19T09:33:51","modified_gmt":"2023-12-19T08:33:51","slug":"at-your-request-use-the-http-client-in-intellij-idea-for-spring-boot-restful-web-services","status":"publish","type":"idea","link":"https:\/\/blog.jetbrains.com\/ja\/idea\/2020\/09\/at-your-request-use-the-http-client-in-intellij-idea-for-spring-boot-restful-web-services","title":{"rendered":"How to use HTTP Client in IntelliJ IDEA for Spring Boot RESTful Web Services"},"content":{"rendered":"<p>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\u2019t need to leave your IDE. The integrated HTTP Client can handle it for you.<\/p>\n<p>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\u2019s more, you can generate HTTP requests from the source code of your RESTful web service, for example, built with Spring Boot. Let\u2019s see how this works.<\/p>\n<p align=\"center\"><a class=\"jb-download-button\" href=\"https:\/\/jb.gg\/button-idea-download\" target=\"_blank\" rel=\"noopener\">Download IntelliJ IDEA<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/09\/idea-blog-banner-1024x527.png\" alt=\"IntelliJ IDEA: how2pro banner\" \/><\/p>\n<p>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 <code>sayHello()<\/code> method is annotated with <code>@GetMapping<\/code>, making it a GET request handler for <code>\/hello<\/code>.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-421057\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/09\/spring-demo.png\" alt=\"Spring demo application in IntelliJ IDEA\" width=\"1500\" height=\"600\" \/><\/p>\n<p>If you click the <strong>Open in HTTP Client<\/strong> gutter icon next to the <code>sayHello()<\/code> method, IntelliJ IDEA will generate the GET request and add it to a scratch file named <em>generated-requests.http<\/em>.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-421024\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/09\/generate-http-request.png\" alt=\"Generate HTTP request right in the IDE\" width=\"1500\" height=\"600\" \/><\/p>\n<p>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 <strong>Endpoints<\/strong> tool window.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-421013\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/09\/endpoints.png\" alt=\"Endpoints tool window in IntelliJ IDEA\" width=\"1500\" height=\"600\" \/><\/p>\n<p>If you have the <a href=\"https:\/\/docs.spring.io\/spring-boot\/docs\/current\/reference\/html\/production-ready-features.html\" target=\"_blank\" rel=\"noopener\">Spring Boot Actuator<\/a> dependency, it exposes all of your web service endpoints at runtime. In the <strong>Run<\/strong> tool window or the <strong>Services<\/strong> tool window, select your running application and open the <strong>Actuator<\/strong> tab. Under <strong>Mappings<\/strong>, 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.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-420980\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/09\/actuator-http-request.png\" alt=\"Actuator tab in IntelliJ IDEA\" width=\"1500\" height=\"600\" \/><\/p>\n<p>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, <em>SpringBootTutorialApplication.http<\/em>).<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-421046\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/09\/scratch-http-request.png\" alt=\"Scratch file in HTTP Client\" width=\"1500\" height=\"600\" \/><\/p>\n<p>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 <strong>Project<\/strong> tool window and select <strong>New | HTTP Request<\/strong>. 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.<\/p>\n<p>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 <strong>Alt + Enter <\/strong>on Windows or <strong>\u2325Enter<span style=\"font-weight: 400;\"> on macOS<\/span><\/strong>\u00a0and select the <strong>Move HTTP Requests<\/strong> 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.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-421035\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/09\/move-http-request.png\" alt=\"Move HTTP request in HTTP Client\" width=\"1500\" height=\"600\" \/><\/p>\n<p>If you specify a file that doesn\u2019t 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.<\/p>\n<p>If you already have a number of <code>curl<\/code> 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.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-421002\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/09\/curl-http-request.png\" alt=\"Run cURL request in HTTP Client\" width=\"1500\" height=\"600\" \/><\/p>\n<p>When you compose requests manually, IntelliJ IDEA provides completion for URLs based on the REST endpoints defined in your code. This means you don\u2019t 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 <strong>\u2303Ctrl + Space<\/strong>\u00a0or select\u00a0<strong>Code | Code Completion | Basic<\/strong>\u00a0to show completion suggestions.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-420991\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/09\/autocompletion-http-request.png\" alt=\"Request autocompletion in HTTP Client\" width=\"1500\" height=\"600\" \/><\/p>\n<p>For more information, see the following topics in the IntelliJ IDEA help:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.jetbrains.com\/help\/idea\/http-client-in-product-code-editor.html\" target=\"_blank\" rel=\"noopener\">HTTP Client in the code editor<\/a><\/li>\n<li><a href=\"https:\/\/www.jetbrains.com\/help\/idea\/scratches.html\" target=\"_blank\" rel=\"noopener\">Scratch files<\/a><\/li>\n<li><a href=\"https:\/\/www.jetbrains.com\/help\/idea\/your-first-spring-application.html\" target=\"_blank\" rel=\"noopener\">Spring Boot tutorial<\/a><\/li>\n<\/ul>\n<p>Download IntelliJ IDEA\u00a0today to try out these features. To learn more about the features for working with Spring in IntelliJ IDEA, visit <a href=\"https:\/\/www.jetbrains.com\/idea\/spring\/\" target=\"_blank\" rel=\"noopener\">this web page<\/a>.<\/p>\n<p>Happy developing!<\/p>\n","protected":false},"author":959,"featured_media":78794,"comment_status":"open","ping_status":"open","template":"","categories":[601],"tags":[2635,202,276,3211,3092],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/idea\/78902"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/idea"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/types\/idea"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/users\/959"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/comments?post=78902"}],"version-history":[{"count":7,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/idea\/78902\/revisions"}],"predecessor-version":[{"id":421153,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/idea\/78902\/revisions\/421153"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/media\/78794"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/media?parent=78902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/categories?post=78902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/tags?post=78902"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/cross-post-tag?post=78902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}