mscharhag, Programming and Stuff;

A blog about programming and software development topics, mostly focused on Java technologies including Java EE, Spring and Grails.

Posts tagged with IntelliJ IDEA

  • Monday, 29 June, 2020

    IntelliJ's text based HTTP client

    IntelliJ provides a HTTP client that is purely text based. While this might sound strange at the beginning it turns out that this is a very useful feature.

    Getting started

    First we need to create a file whose name ends with .http or .rest. For example: my-requests.http.

    To issue a simple GET request we have to write down the request in our newly created file.

    For example:

    GET http://localhost:8080/products

    IntelliJ now adds a small Run-Icon next to the line which allows you to execute the request.

    IntelliJ showing a run-icon next to the HTTP request line

     

    If we want to POST a piece of JSON, we simply have to add a Content-Type header and the request body:

    POST http://localhost:8080/products
    Content-Type: application/json
    
    {
      "name": "My other Product",
      "description": "hu?"
    }
    

    Please note that there has to be a blank line between headers and request body.

    Of course IntelliJ has syntax highlighting and auto completion for writing down headers and JSON:

    IntelliJ showing Syntax-completion for HTTP request headers

     

    Multiple requests in the same file need to be separated with ###. For example:

    GET http://localhost:8080/products
    
    ###
    
    POST http://localhost:8080/products
    Content-Type: application/json
    
    {
      "name": "My other Product",
      "description": "hu?"
    }

    Using variables

    With {{ .. }} we can add variables to our requests. Maybe we want to issue the same request against different environments. To support this, we can update our request with a host variable:

    GET http://{{host}}/products

    Next we need to define the {{host}} variable. For this we create a http-client.env.json file and add the following content:

    {
      "development": {
        "host": "http://localhost:8080"
      },
      "production": {
        "host": "http://my-cool-api.com"
      }
    }

    This defines two environments: development and production. Both environments define the host variable with a different value.

    When running the request, we can now choose the environment we want:

    IntelliJ showing environment in run options for HTTP request

     

    Share requests with your team

    The simple text-based request definition allows easy sharing with your team. You can even check in request files into your version control system. Of course you do not want to check in passwords or API keys that might be needed for request execution. IntelliJ supports this with a separate private environment file (http-client.private.env.json). Like in the previous environment example, we can use this file to define variables.

    For example:

    {
      "dev": {
        "api-key": "S3CR3T"
      }
    }

    To make sure no secrets are checked in, we can explicitly exclude this file from our version control system.

     

  • Friday, 12 June, 2020

    Kotlin / IntelliJ quick hint: Operator navigation

    This is just a quick hint if you are programming Kotlin with IntelliJ:

    In IntelliJ you can ctrl-click on operators to navigate to the operator definition (similar to ctrl-clicking on methods).

    Intellij-kotlin-operator

    This also works for the get operator (you can click on the [] brackets) and for ranges (you can click on the dots (..) between the start and end values).

     

  • Wednesday, 6 August, 2014

    Using IntelliJ bookmarks

    This is a quick post about IntelliJ's nice bookmark feature.

    IntelliJ gives you the option to bookmark single lines of code. After a line has been bookmarked, you can use various ways to jump directly back to this line. So it can be a good idea to bookmarks code locations you often work with.

    To create a new bookmark you only have to press F11 inside the code editor. Bookmarked lines show a small checkmark next to the line number.
     

    intellij bookmark

    Bookmarks can be removed by selecting the bookmarked line and pressing F11 again.

    To see all bookmarks you can press Shift - F11. This opens a small popup window which shows a list of all bookmarks you have created.
     

    intellij bookmark popup


    Note that this window can completely controlled using the keyboard:

     

    • With Up / Down you can browse the list of bookmarks
    • With Enter you jump to the selected bookmark
    • Esc closes the window
    • A bookmark can be moved up or down using Alt - Up / Alt - Down

    Note that you can also add a mnemonic identifier to a bookmark. You do this by selecting a line and pressing Ctrl - F11. This opens a small menu in which you can choose a mnemonic identifier (which is a character or a number).

     

    intellij bookmark identifier popup

    You can choose an identifier by clicking on one of the menu buttons or by simply pressing the corresponding key on your keyboard.

    Bookmark mnemonics are also shown next to the line number. In the following image 1 was choosen as mnemonic.

     

    intellij bookmark mnemonic


    Mnemonics give you the option to move even quicker between bookmarks. You can directly jump to a mnemonic bookmark by opening the bookmark popup (Shift - F11) and pressing the mnemonic key (1 in this example).

    For numerical bookmarks even more shortcuts are available. You can toggle a numeric mnemonic on a selected line by pressing Ctrl - Shift - <number>. If you want to jump to a numeric mnemonic you use the Ctrl - <number> shortcut.

    For example: Ctrl - 5 brings you directly to the mnemonic bookmark 5.