IDE Setup
Relevant source files
The following files were used as context for generating this wiki page:This document provides a guide for setting up the gs-rest-service project in a development environment. The project is a standard Spring Boot application built using either Maven or Gradle, making it compatible with most modern Java IDEs such as IntelliJ IDEA, Eclipse, or VS Code.
The setup process involves ensuring prerequisites are met, obtaining the project files, and importing them into an IDE. The IDE will use the build configuration files (pom.xml for Maven or Gradle scripts) to resolve dependencies, configure the project structure, and define run/debug configurations.
Prerequisites
Before setting up the project, ensure your development environment has the following tools installed.
- A favorite text editor or IDE.
- A JDK, version 17 or later. The Java version is specified in the Maven configuration.
- A build tool, either Gradle or Maven.
Sources: README.adoc, complete/pom.xml:19
Project Initialization
There are two primary methods to get the project code for IDE import: using the Spring Initializr service or cloning the repository directly.
Using Spring Initializr
The recommended approach is to use Spring Initializr, which generates a new project structure with the necessary dependencies. This can be done via the web interface or directly within a supported IDE.
The following diagram illustrates the workflow for initializing the project using the Spring Initializr web service.
Sources: README.adoc
Cloning from GitHub
Alternatively, you can fork and clone the project directly from its GitHub repository. Your IDE can then be used to open or import the cloned directory.
Sources: README.adoc
Build System Configuration
The project supports both Maven and Gradle. The IDE will automatically detect the configuration files in the root directory (pom.xml for Maven, build.gradle for Gradle) to set up the project.
Maven Configuration
The project is configured as a Maven project with a parent dependency on spring-boot-starter-parent. This provides sensible default configurations for a Spring Boot application.
Sources: complete/pom.xml:6-11
The core dependencies are listed in the pom.xml file.
| Group ID | Artifact ID | Scope | Description |
|---|---|---|---|
org.springframework.boot | spring-boot-starter-web | compile | Provides dependencies for building web applications, including RESTful applications, using Spring MVC. It includes an embedded Tomcat server by default. |
org.springframework.boot | spring-boot-starter-test | test | Provides dependencies for testing Spring Boot applications with libraries like JUnit Jupiter, Hamcrest, and Mockito. |
Sources: complete/pom.xml:21-31
The spring-boot-maven-plugin is included to create an executable JAR file.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Sources: complete/pom.xml:34-41
Gradle Configuration
The project also includes Gradle wrapper scripts (gradlew and gradlew.bat), indicating that it can be built and managed using Gradle. The run.sh script demonstrates the use of Gradle for compiling and building the project.
Sources: complete/gradlew.bat, test/run.sh:12-17
Running the Application from the IDE
Once the project is imported, the application can be run directly from the IDE. The main entry point for the application is the main method within the RestServiceApplication class.
This diagram shows the sequence of events when running the application from an IDE.
The RestServiceApplication class is annotated with @SpringBootApplication, which enables auto-configuration, component scanning, and other core Spring Boot features.
package com.example.restservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestServiceApplication {
public static void main(String[] args) {
SpringApplication.run(RestServiceApplication.class, args);
}
}
Sources: complete/src/main/java/com/example/restservice/RestServiceApplication.java:3-13
Testing the Application from the IDE
The project includes a test suite that can be executed from the IDE. Most IDEs provide a "Run Tests" option on test classes or directories. The GreetingControllerTests class contains unit tests for the GreetingController.
These tests use MockMvc to perform requests to the web application without needing a full HTTP server.
@SpringBootTest
@AutoConfigureMockMvc
public class GreetingControllerTests {
@Autowired
private MockMvc mockMvc;
@Test
public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, World!"));
}
// ...
}
Sources: complete/src/test/java/com/example/restservice/GreetingControllerTests.java:29-42
Running these tests from the IDE will build the project and execute the test methods, with results displayed in the IDE's test runner panel.