I spent some time trying to figure out how SpringBoot worked recently. I thought I would document the steps I took to get a project up and running.
Here is the final repository in Github.
Setup Steps
-
First, create a workspace.
mkdir -p ~/projects
cd ~/projects
mvn archetype:generate -DgroupId=com.tonyzampogna -DartifactId=base-springboot-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd ~/projects/base-springboot-app
-
In the pom.xml, add the Spring Boot dependencies. First, add the parent block. I put it above the dependencies block.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
-
Then, in the dependencies block, add the following dependencies.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
-
Add the plugin for building SpringBoot with maven. This goes either above or below the dependencies block. This solves an error stating: “no main manifest attribute”.
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
-
Move the App.java file in src/main/java/com/tonyzampogna to a new package called “main”.
mkdir -p src/main/java/com/tonyzampogna/main
mv src/main/java/com/tonyzampogna/App.java src/main/java/com/tonyzampogna/main
-
Replace the contents of the App.java file with the text below.
package com.tonyzampogna.main; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; /** * Main application file. */ @SpringBootApplication @ComponentScan("com.tonyzampogna") public class App { private static final Logger logger = LoggerFactory.getLogger(App.class); ///////////////////////////////////////////////// // Application Start Methods ///////////////////////////////////////////////// /** * This method runs on applications start. */ public static void main(String[] args) { logger.info("Starting application"); SpringApplication.run(App.class, args); } }
-
Make a controller so that we can test our server. First, create the package directory.
mkdir -p src/main/java/com/tonyzampogna/controller
-
Then, add HomeController.java code to the controller package.
package com.tonyzampogna.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HomeController { @RequestMapping("/home") public String getHome() { return "Hello"; } }
-
Create a resources directory for a log4j properties file.
mkdir -p src/main/resources
-
Add the log4.properties file below to src/main/resources.
################################################################# ## Log Levels ################################################################# log4j.rootLogger=INFO, STDOUT log4j.logger.com.tonyzampogna=DEBUG, STDOUT, FILE ################################################################# ## Appenders ################################################################# # Console log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # File log4j.appender.FILE=org.apache.log4j.RollingFileAppender log4j.appender.FILE.File=application.log log4j.appender.FILE.MaxFileSize=10MB log4j.appender.FILE.MaxBackupIndex=10 log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
To run the project (from either Eclipse, Intellij, or the command line):
From the command line
-
In the project directory, package the jar file and then run it.
mvn package
java -jar target/base-springboot-app-1.0-SNAPSHOT.jar
From Eclipse
-
First, run the maven eclipse target to setup some essential eclipse files.
mvn eclipse:eclipse
-
Open Eclipse. Make sure you get the SpringSource Tool Suite from the Eclipse Marketplace. Then, import the project into the workspace as an existing eclipse project.
-
Now, you can right-click on the project and choose either “Run as SpringBoot application” or “Debug as SpringBoot application”.
From Intellij
-
Open Intellij and choose File -> New Project.
-
Select Java project and choose Next.
-
For project name, type in base-springboot-app. For project location, type in ~/projects/base-springboot-app.
-
In the Run menu, choose Edit Configurations. Click the plus sign and select JAR Application. Fill in the information below.
- Name: Server
- Path to JAR: ~/projects/base-springboot-app/target/base-springboot-app-1.0-SNAPSHOT.jar
- VM Arguments: -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044
- Working directory: ~/projects/base-springboot-app
-
Select ok.
-
Now, from the Run menu, you can choose either