Spring Boot Helloworld Example Jar File Not Read or Invalid
eleven. Developing your first Spring Boot application
Allow's develop a elementary "Hello World!" web application in Java that highlights some of Spring Boot'due south key features. Nosotros'll use Maven to build this project since most IDEs support information technology.
Tip | |
---|---|
The spring.io web site contains many "Getting Started" guides that use Spring Boot. If y'all're looking to solve a specific problem; cheque there start. |
Before nosotros brainstorm, open a terminal to cheque that yous have valid versions of Java and Maven installed.
$ java -version coffee version "1.7.0_51" Java(TM) SE Runtime Surroundings (build 1.seven.0_51-b13) Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed way)
$ mvn -five Apache Maven 3.ii.iii (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T13:58:10-07:00) Maven home: /Users/user/tools/apache-maven-3.1.1 Coffee version: 1.7.0_51, vendor: Oracle Corporation
Note | |
---|---|
This sample needs to be created in its own folder. Subsequent instructions assume that you accept created a suitable binder and that it is your "current directory". |
11.1 Creating the POM
We need to starting time by creating a Maven pom.xml
file. The pom.xml
is the recipe that volition be used to build your project. Open your favorite text editor and add the post-obit:
<?xml version="ane.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-iv.0.0.xsd" > <modelVersion>iv.0.0</modelVersion> <groupId>com.case</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-kicking-starter-parent</artifactId> <version>one.2.iii.RELEASE</version> </parent> </project>
This should requite you a working build, yous can examination information technology out by running mvn package
(you tin can ignore the "jar will be empty - no content was marked for inclusion!'" alert for now).
Annotation | |
---|---|
At this point you could import the project into an IDE (near mod Java IDE's include born support for Maven). For simplicity, we will proceed to use a plain text editor for this example. |
eleven.2 Calculation classpath dependencies
Leap Kick provides a number of "Starter POMs" that make easy to add jars to your classpath. Our sample awarding has already used spring-kick-starter-parent
in the parent
section of the POM. The jump-kicking-starter-parent
is a special starter that provides useful Maven defaults. It also provides a dependency-management
department so that y'all tin can omit version
tags for "blessed" dependencies.
Other "Starter POMs" only provide dependencies that y'all are probable to need when developing a specific type of awarding. Since nosotros are developing a web application, we will add together a jump-boot-starter-web
dependency — merely before that, let'south look at what we currently accept.
$ 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 tin see that spring-kicking-starter-parent
provides no dependencies by itself. Let's edit our pom.xml
and add the leap-boot-starter-web
dependency just beneath the parent
department:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
If you run mvn dependency:tree
again, you lot will encounter that there are now a number of additional dependencies, including the Tomcat web server and Spring Kick itself.
11.3 Writing the lawmaking
To end our application nosotros need to create a single Java file. Maven volition compile sources from src/main/java
by default so you lot need to create that binder structure, and so add together a file named src/main/java/Example.java
:
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.demark.annotation.*; @RestController @EnableAutoConfiguration public class Case { @RequestMapping("/") String home() { render "Hello Earth!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Instance.class, args); } }
Although there isn't much lawmaking hither, quite a lot is going on. Permit'due south step through the of import parts.
11.3.i The @RestController and @RequestMapping annotations
The first notation on our Case
class is @RestController
. This is known every bit a stereotype annotation. Information technology 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 will consider it when handling incoming web requests.
The @RequestMapping
note provides "routing" information. It is telling Spring that whatever HTTP request with the path "/" should be mapped to the abode
method. The @RestController
annotation tells Leap to render the resulting string directly back to the caller.
Tip | |
---|---|
The |
11.3.2 The @EnableAutoConfiguration note
The second course-level notation is @EnableAutoConfiguration
. This annotation tells Spring Boot to "guess" how you will want to configure Spring, based on the jar dependencies that you have added. Since leap-boot-starter-spider web
added Tomcat and Spring MVC, the auto-configuration will assume that you are developing a web awarding and setup Spring accordingly.
11.3.3 The "chief" method
The final function of our awarding is the main
method. This is simply a standard method that follows the Coffee convention for an application entry signal. Our principal method delegates to Spring Kick's SpringApplication
class by calling run
. SpringApplication
will bootstrap our application, starting Spring which will in plough offset the motorcar-configured Tomcat spider web server. We need to laissez passer Instance.class
as an argument to the run
method to tell SpringApplication
which is the master Spring component. The args
array is also passed through to expose any command-line arguments.
11.4 Running the example
At this point our application should work. Since we have used the leap-kick-starter-parent
POM we have a useful run
goal that nosotros can utilise to kickoff the awarding. Type mvn spring-boot:run
from the root project directory to outset the application:
$ mvn spring-boot:run . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.ii.3.RELEASE) ....... . . . ....... . . . (log output here) ....... . . . ........ Started Example in two.222 seconds (JVM running for 6.514)
If you open a web browser to localhost:8080 you lot should run across the following output:
Hello Globe!
To gracefully exit the application hit ctrl-c
.
11.v Creating an executable jar
Let'south cease our example by creating a completely self-contained executable jar file that we could run in product. Executable jars (sometimes chosen "fat jars") are archives containing your compiled classes along with all of the jar dependencies that your code needs to run.
To create an executable jar we demand to add the spring-boot-maven-plugin
to our pom.xml
. Insert the following lines just below the dependencies
section:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-kick-maven-plugin</artifactId> </plugin> </plugins> </build>
Note | |
---|---|
The |
Salve your pom.xml
and run mvn package
from the control line:
$ mvn package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building myproject 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] .... .. [INFO] --- maven-jar-plugin:2.iv:jar (default-jar) @ myproject --- [INFO] Edifice jar: /Users/programmer/example/leap-kicking-example/target/myproject-0.0.ane-SNAPSHOT.jar [INFO] [INFO] --- spring-kick-maven-plugin:1.2.3.RELEASE:repackage (default) @ myproject --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
If you look in the target
directory you should run across myproject-0.0.1-SNAPSHOT.jar
. The file should be around 10 Mb in size. If you want to peek inside, you tin employ jar tvf
:
$ jar tvf target/myproject-0.0.1-SNAPSHOT.jar
You should as well run into a much smaller file named myproject-0.0.1-SNAPSHOT.jar.original
in the target
directory. This is the original jar file that Maven created before it was repackaged by Bound Kicking.
To run that application, use the coffee -jar
command:
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Leap Boot :: (v1.2.3.RELEASE) ....... . . . ....... . . . (log output here) ....... . . . ........ Started Example in 2.536 seconds (JVM running for 2.864)
As earlier, to gracefully exit the application hit ctrl-c
.
Source: https://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/html/getting-started-first-application.html
0 Response to "Spring Boot Helloworld Example Jar File Not Read or Invalid"
Post a Comment