Build a Java Spring Boot app in IntelliJ with Duet AI assistance

11 months ago 44
News Banner

Looking for an Interim or Fractional CTO to support your business?

Read more

Duet AI in Google Cloud offers AI assistance for developers, operators, security pros, data experts, and more. Duet AI 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 Duet AI inside Jetbrains IntelliJ IDEA.

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

What you need

1. A Duet AI enabled Google Cloud project where:

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. 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 > Google Cloud Code > Sign in to Google Cloud Platform.

1 Duet AI assistance

5. Duet AI enabled in Cloud Code

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. Under Project Metadata

4. Leave the other options as they are

2 Duet AI assistance

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

6. Select Spring Web from the list

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

8. 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 Duet AI 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

3 Duet AI assistance

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

code_block<ListValue: [StructValue([('code', '// Create a Java web controller for a web application that respond on the /hello endpoint, takes a string parameter called name, and then combine this parameter with the word "Hello", the name default value is “World”'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x3e4d3d6527c0>)])]>

6. Hit Enter and then, to generate code, press Alt+\ (Windows/Linux) or Option+\ (macOS). Alternatively you can click on the magic wand

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:

code_block<ListValue: [StructValue([('code', 'package com.example.greetingapp;\r\n\r\n\r\nimport org.springframework.web.bind.annotation.GetMapping;\r\nimport org.springframework.web.bind.annotation.RequestParam;\r\nimport org.springframework.web.bind.annotation.RestController;\r\n\r\n\r\n@RestController\r\npublic class HomeController {\r\n\r\n\r\n @GetMapping("/hello")\r\n public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {\r\n return String.format("Hello %s!", name);\r\n }\r\n}'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x3e4d3d652be0>)])]>

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. Duet AI 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.

4 Duet AI assistance

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

code_block<ListValue: [StructValue([('code', 'mvn spring-boot:run'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x3e4d3d652820>)])]>

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

5 Duet AI assistance

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 Duet AI 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

6 Duet AI assistance

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

code_block<ListValue: [StructValue([('code', '<!-- Create an html template titled "Greetings App" that has a form, named "Greetings Form", where it gets the user name and a button that points to the "/hello" endpoint, sets the name variable to the user name and greets the user -->'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x3e4d3d6524f0>)])]>

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

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

code_block<ListValue: [StructValue([('code', '<!DOCTYPE html>\r\n<html>\r\n<head>\r\n <title>Greetings App</title>\r\n</head>\r\n<body>\r\n <h1>Greetings From</h1>\r\n <form action="/hello" method="GET">\r\n <label for="name">Name:</label>\r\n <input type="text" id="name" name="name">\r\n <input type="submit" value="Greet me">\r\n </form>\r\n</body>\r\n</html>'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x3e4d3d6520a0>)])]>

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:

code_block<ListValue: [StructValue([('code', 'mvn spring-boot:run'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x3e4d3d727cd0>)])]>

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

7 Duet AI assistance

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

8 Duet AI assistance

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

code_block<ListValue: [StructValue([('code', '// Write unit tests for code in HomeController to test that the application responds correctly'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x3e4d3d727d90>)])]>

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

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:

code_block<ListValue: [StructValue([('code', 'package com.example.greetingapp;\r\n\r\n\r\nimport org.junit.jupiter.api.Test;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;\r\nimport org.springframework.test.web.servlet.MockMvc;\r\n\r\n\r\nimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;\r\nimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;\r\nimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;\r\n\r\n\r\n@WebMvcTest(HomeController.class)\r\npublic class HomeControllerTest {\r\n\r\n\r\n @Autowired\r\n private MockMvc mockMvc;\r\n\r\n\r\n @Test\r\n public void testHello() throws Exception {\r\n mockMvc.perform(get("/hello"))\r\n .andExpect(status().isOk())\r\n .andExpect(content().string("Hello World!"));\r\n }\r\n\r\n\r\n @Test\r\n public void testHelloWithName() throws Exception {\r\n mockMvc.perform(get("/hello?name=John"))\r\n .andExpect(status().isOk())\r\n .andExpect(content().string("Hello John!"));\r\n }\r\n}'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x3e4d3da40400>)])]>

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

code_block<ListValue: [StructValue([('code', 'mvn test'), ('language', ''), ('caption', <wagtail.rich_text.RichText object at 0x3e4d3da40310>)])]>

7. Test should run successfully

8. Now replace the word “Hello” with “Hi” in the line 13 of 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 Duet AI 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 Duet AI capabilities for developers in:

Read Entire Article