Jump to Content
Application Development

Build a Java Spring Boot app in IntelliJ with Gemini assistance

November 10, 2023
Giovanni Galloro

Customer Engineer

Gemini Code Assist offers AI assistance for developers it comes with IDE integrations for Visual Studio Code and JetBrains IDEs, including IntelliJ IDEA.

This article explains how to create a simple Java Spring Boot web application from scratch with the help of Gemini Code Assist inside Jetbrains IntelliJ IDEA.

You will start from an empty project created with Spring Initializr and will experiment how to use Gemini assisted code generation to create your application and add unit tests.

In addition to the instructions below, you can also follow this video walkthrough.

https://storage.googleapis.com/gweb-cloudblog-publish/images/maxresdefault_3Qv8H1E.max-1300x1300.jpg

What you need

  1. A Gemini enabled Google Cloud project where:
    1. The Gemini for Google Cloud has been enabled
    2. Billing has been enabled
  2. A computer with IntelliJ IDEA Community or Ultimate edition installed.
  3. Cloud Code for IntelliJ plugin installed
  4. Signed In to Google Cloud from the IDE and with a project selected. If you don’t see the Tools menu in IntelliJ you can open the action menu with Ctrl + Shift + A (Windows) or Cmd + Shift + A (Mac), type “main menu” in the search, and then select Main Menu. From there you can select Tools > Gemini + Google Cloud Code > Sign in to Google Cloud Platform.
https://storage.googleapis.com/gweb-cloudblog-publish/images/1_Duet_AI_assistance.max-1100x1100.png

Build a starting Spring Boot project

For creating your application you need to start from a sample Spring Boot project created with Spring Initializr, you can do that following the steps below. If you have IntelliJ IDEA Ultimate edition you can also do that directly from the “New” > “Project” flow in the IDE selecting the same options

  1. Go to https://start.spring.io/
  2. In the Spring Initializer page, under Project, select Maven
  3. Select the latest Spring Boot version (not snapshot)
  4. Under Project Metadata
    a. Change Artifact and Name to “greeting-app”
    b. Change Description to “Greeting App made with Gemini assistance”
  5. Leave the other options as they are
https://storage.googleapis.com/gweb-cloudblog-publish/images/image3_NMVo3rj.max-1200x1200.png

6. Click on ADD DEPENDENCIES in the higher right corner of the page

7. Select Spring Web from the list

8. Click GENERATE in the bottom of the page, you will be prompted to download the greeting-app.zip file

9. Save the file where it works best for you (in a temporary location if you will delete the project after when you’ll finish) and unzip the file.

Build your code with Gemini code generation assistance

  1. In IntelliJ go to File > Open (or click Open in the welcome screen)
  2. Select the folder where you have unzipped your file and click Open
  3. IntelliJ will open the project folder, double click the GreetingAppApplication class to view its content
  4. Under src/main/java/com.example.greetingapp, create a new file called HomeController.java
https://storage.googleapis.com/gweb-cloudblog-publish/images/3_Duet_AI_assistance.max-1300x1300.png

5. At the top of the HomeController.java file type the following comment:

Loading...

6. Hit Enter and then, to generate code, press Alt+Shift+\ (Windows/Linux) or Option+Shift+\ (macOS). Alternatively you can click on the Gemini icon near the comment line

7. You should get code generated in the form of ghost text. The code should be functionally similar to the following, with a @GetMapping("/hello") annotation and the hello() method that greets an user based on the value of the name variable in the request, or uses the value “World” if the variable has no value:

Loading...

8. If you get the same code as above, hit Tab to accept the suggestion. If code is different you can evaluate if it will work for your goal, decide to make some changes or use the example code above.

9. Probably part of the proposed code will be highlighted and you will have warnings in IntelliJ, you can check them in the problems pane. Gemini Code Assist license checking is warning that the code generated is, at length, taken from one of the samples in the training data so it could be subject to licenses. The warning message includes the source of the training data so that you can check.

https://storage.googleapis.com/gweb-cloudblog-publish/images/4_Duet_AI_assistance.max-500x500.png

10. Now let’s check if generated code works, in the IntelliJ terminal, type:

Loading...

11. You will see the output of Spring log messages in the terminal.

https://storage.googleapis.com/gweb-cloudblog-publish/images/5_Duet_AI_assistance.max-1400x1400.png

12. By default, the built-in Apache Tomcat server is listening on port 8080. Open your web browser and go to http://localhost:8080/hello . If the code generated by Gemini works, you should see your application respond with Hello World!

13. You can also provide your name in your web request to let the application know how to greet you properly. For example you can try http://localhost:8080/hello?name=John

14. Go back to IntelliJ and type Ctrl+C in the terminal to stop the application

15. Now let’s try to improve this app a bit: create an index.html file inside the resources/static folder

https://storage.googleapis.com/gweb-cloudblog-publish/images/6_Duet_AI_assistance.max-400x400.png

16. At the top of the file, type the following comment:

Loading...

17. Hit Enter and then, to generate code, press Alt+\ (Windows/Linux) or Option+\ (macOS). Alternatively you can click on the Gemini icon near the comment line

18. You should get code generated in the form of ghost text, functionally similar to the following

Loading...

19. If you get the same code as above, hit Tab to accept the suggestion. If code is different you can evaluate if it will work for your goal, decide to make some changes or use the example code above

20. Run your application again with:

Loading...

21. Now, since you added an index.html file, access your application at the root from your browser: http://localhost:8080/. You should see the Greetings Form

https://storage.googleapis.com/gweb-cloudblog-publish/images/7_Duet_AI_assistance.max-800x800.png

22. Type your name and click Greet me, you will be greeted

23. Go back to IntelliJ, type Ctrl+C in the terminal to stop the application

Add unit tests to your application

1. Under src/test/java/com.example.greetingapp create a new file called HomeControllerTest.java

https://storage.googleapis.com/gweb-cloudblog-publish/images/8_Duet_AI_assistance.max-800x800.png

2. At the top of the file, type the following comment:

Loading...

3. Hit Enter and then, to generate code, press Alt+\ (Windows/Linux) or Option+\ (macOS). Alternatively you can click on the Gemini icon near the comment line

4. You should get code generated in the form of ghost text, functionally similar to the following, testing both for the default "Hello World!" string than for a custom value of the name variable:

Loading...

5. If you get the same code as above, hit Tab to accept the suggestion. If code is different you can evaluate if it will cover your test cases, decide to make some changes or use the example code above

6. In the terminal, run

Loading...

7. Test should run successfully

8. Now replace the word “Hello” with “Hi” in the /hello method in your HomeController.java file and run tests again, tests should fail since the application is not responding with the expected string

Conclusions

Reading and following the instructions in this article you learned how to use Gemini Code Assist in IntelliJ to generate Java code, HTML and unit tests to start from an empty Spring Boot application and get to a working “Greeting App”.

You can learn more about Gemini capabilities for developers in:

Posted in