Heres how I got the Spring Boot ‘Hello World’ up and running in Eclipse…
I am using the Eclipse IDE that comes with Maven installed for my evelopment.
Following the Spring Boot Reference Guide, I followed the steps under Developing Your First Spring Boot Application.
I created my java project in Eclipse, on the menu click File–>New–>Java Project
- Then I gave the project a name (Then click finish)
- Create your project source folders
By default the project will use a folder called ‘src’ for you to keep your source code. I always like to keep my files organised using the maven defaults. So I create a src/main/java folder, src/main/resources folder, src/test/java folder and a src/test/resources folder. This make it easier to split your code between production code and test, and also between code and other file types such as properties files. To set up these folders, right click on the project, then properties–>build path–>click the source tab, then –>add folder. Click on the ‘src’ then click to add new folder and name the new folder.
- Next thing to do is generate a POM for your project.
Go to Spring Initializr. Select a Maven project and the latest version, and type in ‘web’ in the dependencies. Press alt and click on the Generate Project tab which should give you the option to download a zip file
Save the zip file, then go to the folder where it has been saved, open it and grab the pom. Copy this Pom into the root of your project, and at this stage your project should look like this…
- Now lets create the code we want to run
If you look at the Spring Boot Reference we are now ready to add the source code at 11.3 Writing the code.
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
So we copy this code and create a java class (Right click project–>New–>Class, enter name as x, finish). Paste the code into the class in the editor. You should see some errors. Hover over the class name, take the option to rename the compilation unit name to example, and the save. You will still have some errors, and thats because we havent downloaded any of the required dependencies yet.
- Build the project with maven to resolve the dependencies
To resolve all the dependencies we need to create a maven build task for the project.From Menu–>Run–>Run Configurations–>Right Click on Maven Build then click New… Give it a name at the top, file in the base directory by clicking the workspace button then selecting the correct project, then on the goals line add ‘eclipse:clean eclipse:eclipse -DdownloadSources -DdownloadJavadocs’. This goal tells maven to set up your project for eclipse and maven, grab any dependencies and add them to the classpath etc. Once you have done that click apply and run, and the process should start.
In the console view, you should see lots of activity as the dependencies start being downloaded. Once it is comple you should see “build successful” message and in your project there should be lots of jar listed under referenced libraries. You ‘hello world’ Example.java class should now show no errors in the editor view. So at this stage you are ready to run the project.
- Run the ‘hello world’
To run the project, we simply need to create another maven task. Create a new maven task but this time call it ‘mvn run myspring’ and have the goal as ‘mvn spring-boot:run’ and below.
You should see that Sprint boot has started and starts Tomcat running your project on port 8080. Navigate to localhost:8080 and you should see your results.
More Reading
Creating a Spring Boot Project With Eclipse and Maven