This section describes how to develop a simple “Hello World!” web application that highlights some of Spring Boot’s key features. We use Maven to build this project, since most IDEs support it.
Spring Initializer
One of the ways to Bootstrapping a Spring Boot application is by using Spring Initializer. To do this, you will have to visit the Spring Initializer web page www.start.spring.io and choose your Build, Spring Boot Version and platform. Also, you need to provide a Group, Artifact and required dependencies to run the application.
Observe the following screenshot that shows an example where we added the spring-boot-starter-web dependency to write REST Endpoints.
Once you provided the Group, Artifact, Dependencies, Build Project, Platform and Version, click Generate Project button. The zip file will download and the files will be extracted.
This section explains you the examples by using Maven.
Project that downloaded in the zip file need to be opened by the IDE. Project can be runned by the IDE and Command Prompt.
Maven
After you download the project, unzip the file. The pom.xml is the recipe that is used to build your project. Open your favorite text editor and add the following:
Adding Classpath Dependencies
Spring Boot provides a number of “Starters” that let you add jars to your classpath. Our sample application has already used
spring-boot-starter-parent in the parent section of the POM. The spring-boot-starter-parent is a special starter that provides useful Maven defaults. It also provides a dependency-managementsection so that you can omit version tags for “blessed” dependencies.
Other “Starters” provide dependencies that you are likely to need when developing a specific type of application. Since we are developing a web application, we add a
spring-boot-starter-web dependency. Before that, we can look at what we currently have by running the following command:$ mvn dependency:tree [INFO] com.example:myproject:jar:0.0.1-SNAPSHOT
The
mvn dependency:tree command prints a tree representation of your project dependencies. You can see that spring-boot-starter-parent provides no dependencies by itself. To add the necessary dependencies, edit your pom.xml and add the spring-boot-starter-web dependency immediately below the parentsection:<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
As mentioned above dependencies can be added to pom.xml file after creating project by adding dependencies.But we already add
spring-boot-starter-web dependency when creating the project by Spring Initializer. You can see it below:
If you run
mvn dependency:tree again, you see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.Controlling Maven and Java versions:
Before we begin, open a terminal and run the following commands to ensure that you have valid versions of Java and Maven installed:
$ java -version java version "1.8.0_102" Java(TM) SE Runtime Environment (build 1.8.0_102-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
$ mvn -v Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T14:33:14-04:00) Maven home: /usr/local/Cellar/maven/3.3.9/libexec Java version: 1.8.0_102, vendor: Oracle Corporation
Maven and Java SDK must be installed for running application with command prompt.
- Maven download link: click here
- Java SDK download link: click here
Ensure that define the maven and java sdk as enviroment variables. JAVA_HOME for java sdk and PATH for maven.
Adding to PATH: Add the unpacked distribution’s bin directory to your user PATH environment variable by opening up the system properties (WinKey + Pause), selecting the “Advanced” tab, and the “Environment Variables” button, then adding or selecting the PATH variable in the user variables with the value C:\Program Files\apache-maven-3.5.4\bin. The same dialog can be used to set JAVA_HOME to the location of your JDK, e.g. C:\Program Files\Java\jdk1.7.0_51 . %JAVA_HOME%\bin must be added to Path in the System Variables.
Writing the Code
In this example, the main class file is located at the src/java/main directories with the default package com.HelloWorld.demo. Observe the code shown here for a better understanding −
The @RestController and @RequestMapping Annotations:
The first annotation on our DemoApplication class is
@RestController. This is known as a stereotype annotation. It provides hints for people reading the code and for Spring that the class plays a specific role. In this case, our class is a web @Controller, so Spring considers it when handling incoming web requests.
The
@RequestMapping annotation provides “routing” information. It tells Spring that any HTTP request with the / path should be mapped to the home method. The@RestController annotation tells Spring to render the resulting string directly back to the caller.Create an Executable JAR
Let us create an executable JAR file to run the Spring Boot application by using Maven commands in the command prompt. You can run the application without creating executable jar file with using IDE. You should be inside the folder of project with using command prompt. Then enter mvn clean install command. With that a executable jar file must be created in the target file. To run it enter java -jar target\"executableFileName".jar command. If you open a web browser to
localhost:8080, you should see the following output:
No comments:
Post a Comment