|
|
|
|
|
|
3
|
What you'll build
|
3
|
What you'll build
|
|
4
|
-----------------
|
4
|
-----------------
|
|
5
|
|
5
|
|
|
6
|
-This guide provides an introduction to [Spring Boot][spring-boot] by building a simple web application. This doesn't demonstrate all of its features but will help you get started with the concepts Spring Boot has to offer.
|
|
|
|
|
|
6
|
+This guide walks you through the process of building a simple web application with [Spring Boot][spring-boot]. The guide provides a sampling of how Spring Boot helps you accelerate and facilitate app development. As you read more Spring getting started guides, you will see more use cases for Spring Boot.
|
|
7
|
|
7
|
|
|
8
|
What you'll need
|
8
|
What you'll need
|
|
9
|
----------------
|
9
|
----------------
|
|
|
|
|
|
|
11
|
- About 15 minutes
|
11
|
- About 15 minutes
|
|
12
|
- <@prereq_editor_jdk_buildtools/>
|
12
|
- <@prereq_editor_jdk_buildtools/>
|
|
13
|
|
13
|
|
|
14
|
-
|
|
|
|
15
|
-## <@how_to_complete_this_guide jump_ahead="Warming up with Spring Boot"/>
|
|
|
|
|
|
14
|
+## <@how_to_complete_this_guide jump_ahead='Learn what you can do with Spring Boot'/>
|
|
16
|
|
15
|
|
|
17
|
|
16
|
|
|
18
|
<a name="scratch"></a>
|
17
|
<a name="scratch"></a>
|
|
|
|
|
|
|
27
|
|
26
|
|
|
28
|
<@snippet path="pom.xml" prefix="initial"/>
|
27
|
<@snippet path="pom.xml" prefix="initial"/>
|
|
29
|
|
28
|
|
|
30
|
-Warming up with Spring Boot
|
|
|
|
31
|
----------------------------
|
|
|
|
|
|
29
|
+Learn what you can do with Spring Boot
|
|
|
|
30
|
+--------------------------------------
|
|
32
|
|
31
|
|
|
33
|
-What does Spring Boot provide? At the core, it offers a much faster way to build applications because it make reasonable assumptions such as looking at your classpath and other beans you have configured to see what you're missing.
|
|
|
|
|
|
32
|
+Spring Boot offers a fast way to build applications. It looks at your classpath and at beans you have configured, makes reasonable assumptions about what you're missing, and adds it. With Spring Boot you can focus more on business features and less on infrastructure.
|
|
34
|
|
33
|
|
|
35
|
For example:
|
34
|
For example:
|
|
36
|
-- Got Spring MVC? There are a handful of needed beans people almost always use in that situation. Spring Boot adds them automatically. But why stop there? A Spring MVC app needs a servlet container so Spring Boot automatically configures embedded Tomcat.
|
|
|
|
37
|
-- Got Jetty? You probably do NOT want Tomcat, but instead embedded Jetty. Don't lift a finger; Spring Boot handles it for you.
|
|
|
|
38
|
-- Got Thymeleaf? There are a few beans that must always be added to your application context. Why should you have to deal with that? Let Spring Boot handle it for you.
|
|
|
|
39
|
-- Doing multipart file uploads? The [MultipartConfigElement](http://docs.oracle.com/javaee/6/api/javax/servlet/MultipartConfigElement.html) is part of the servlet 3.0 spec and let's you define upload parameters in pure Java. Why should you have to worry about plugging that into your servlet? Define one in your application context and Spring Boot will snatch it up and plug it into Spring MVC's battle tested `DispatcherServlet`.
|
|
|
|
|
|
35
|
+- Got Spring MVC? There are several specific beans you almost always need, and Spring Boot adds them automatically. A Spring MVC app also needs a servlet container, so Spring Boot automatically configures embedded Tomcat.
|
|
|
|
36
|
+- Got Jetty? If so, you probably do NOT want Tomcat, but instead embedded Jetty. Spring Boot handles that for you.
|
|
|
|
37
|
+- Got Thymeleaf? There are a few beans that must always be added to your application context; Spring Boot adds them for you.
|
|
|
|
38
|
+- Doing multipart file uploads? [MultipartConfigElement](http://docs.oracle.com/javaee/6/api/javax/servlet/MultipartConfigElement.html) is part of the servlet 3.0 spec and lets you define upload parameters in pure Java. With Spring Boot, you don't have to plug that into your servlet. Define one in your application context and Spring Boot will plug it into Spring MVC's battle-tested `DispatcherServlet`.
|
|
40
|
|
39
|
|
|
41
|
-It doesn't stop there. These are just a few examples of the automatic configuration provided by Spring Boot. But it doesn't get in your way. For example, Spring Boot may make assumptions and add a `SpringTemplateEngine` for your Thymeleaf-based application, unless you've already defined one. At that point, Spring Boot automatically steps aside and lets you take control.
|
|
|
|
|
|
40
|
+These are just a few examples of the automatic configuration Spring Boot provides. At the same time, Spring Boot doesn't get in your way. For example, Spring Boot may make assumptions and add a `SpringTemplateEngine` for your Thymeleaf-based application, unless you've already defined one. At that point, Spring Boot automatically steps aside and lets you take control.
|
|
42
|
|
41
|
|
|
43
|
-Creating a simple web application
|
|
|
|
|
|
42
|
+Create a simple web application
|
|
44
|
---------------------------------
|
43
|
---------------------------------
|
|
45
|
-You already have the base build file at the top. Next step is to create a web controller for a simple web application.
|
|
|
|
|
|
44
|
+You already have the base build file at the top. Now you can create a web controller for a simple web application.
|
|
46
|
|
45
|
|
|
47
|
<@snippet path="src/main/java/hello/HelloController.java" prefix="initial"/>
|
46
|
<@snippet path="src/main/java/hello/HelloController.java" prefix="initial"/>
|
|
48
|
|
47
|
|
|
49
|
-The class is flagged as a `@Controller` meaning it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `/` to the `index()` method. When invoked from a browser or using curl on the command line, it returns pure text thanks to the `@ResponseBody` annotation.
|
|
|
|
|
|
48
|
+The class is flagged as a `@Controller`, meaning it's ready for use by Spring MVC to handle web requests. `@RequestMapping` maps `/` to the `index()` method. When invoked from a browser or using curl on the command line, the method returns pure text thanks to the `@ResponseBody` annotation.
|
|
50
|
|
49
|
|
|
51
|
-To make it executable, create an `Application` class:
|
|
|
|
|
|
50
|
+Create an Application class
|
|
|
|
51
|
+---------------------------
|
|
|
|
52
|
+Here you create an `Application` class with the components:
|
|
52
|
|
53
|
|
|
53
|
<@snippet path="src/main/java/hello/Application.java" prefix="initial"/>
|
54
|
<@snippet path="src/main/java/hello/Application.java" prefix="initial"/>
|
|
54
|
|
55
|
|
|
55
|
- `@Configuration` tags the class as a source of bean definitions for the application context.
|
56
|
- `@Configuration` tags the class as a source of bean definitions for the application context.
|
|
56
|
-- `@EnableAutoConfiguration` tells Spring Boot to get going and start adding beans based on classpath settings, other beans, and various property settings.
|
|
|
|
|
|
57
|
+- `@EnableAutoConfiguration` tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
|
|
57
|
- `@EnableWebMvc` signals Spring MVC that this application is a web application and to activate key behaviors such as setting up a `DispatcherServlet`.
|
58
|
- `@EnableWebMvc` signals Spring MVC that this application is a web application and to activate key behaviors such as setting up a `DispatcherServlet`.
|
|
58
|
- `@ComponentScanning` tells Spring to look for other components, configurations, and services in the the `hello` package, allowing it to find the `HelloController`.
|
59
|
- `@ComponentScanning` tells Spring to look for other components, configurations, and services in the the `hello` package, allowing it to find the `HelloController`.
|
|
59
|
|
60
|
|
|
|
|
|
|
|
61
|
|
62
|
|
|
62
|
The `run()` method returns an `ApplicationContext` and this application then retrieves all the beans that were created either by your app or were automatically added thanks to Spring Boot. It sorts them and prints them out.
|
63
|
The `run()` method returns an `ApplicationContext` and this application then retrieves all the beans that were created either by your app or were automatically added thanks to Spring Boot. It sorts them and prints them out.
|
|
63
|
|
64
|
|
|
64
|
-To run it, execute:
|
|
|
|
|
|
65
|
+Run the application
|
|
|
|
66
|
+-------------------
|
|
|
|
67
|
+To run the application, execute:
|
|
65
|
|
68
|
|
|
66
|
```sh
|
69
|
```sh
|
|
67
|
$ mvn package spring-boot:run
|
70
|
$ mvn package spring-boot:run
|
|
|
|
|
|
|
116
|
Greetings from Spring Boot!
|
119
|
Greetings from Spring Boot!
|
|
117
|
```
|
120
|
```
|
|
118
|
|
121
|
|
|
119
|
-Switching to Jetty
|
|
|
|
120
|
-------------------
|
|
|
|
121
|
-What if you preferred Jetty over Tomcat? They're both compliant servlet containers, so it should be darn simple to switch. And it is!
|
|
|
|
|
|
122
|
+Switch from Tomcat to Jetty
|
|
|
|
123
|
+---------------------------
|
|
|
|
124
|
+Jetty and Tomcat are both compliant servlet containers, so it's easy to switch.
|
|
122
|
|
125
|
|
|
123
|
Add this to your build file's list of dependencies:
|
126
|
Add this to your build file's list of dependencies:
|
|
124
|
|
127
|
|
|
|
|
|
|
|
129
|
</dependency>
|
132
|
</dependency>
|
|
130
|
```
|
133
|
```
|
|
131
|
|
134
|
|
|
132
|
-Adding multipart upload support
|
|
|
|
|
|
135
|
+Add multipart upload support
|
|
133
|
-------------------------------
|
136
|
-------------------------------
|
|
134
|
-Imagine you also want to add file upload support. To do that, update your configuration and add a `MultipartConfigElement` to the application context.
|
|
|
|
|
|
137
|
+Suppose you want to add file upload support. To do that, update your configuration and add a `MultipartConfigElement` to the application context.
|
|
135
|
|
138
|
|
|
136
|
<@snippet path="src/main/java/hello/Application.java" prefix="complete"/>
|
139
|
<@snippet path="src/main/java/hello/Application.java" prefix="complete"/>
|
|
137
|
|
140
|
|
|
138
|
-> **Note:** A production version of `MultipartConfigElement` would not be empty but instead specify things like target upload path, file size upload limits, etc.
|
|
|
|
|
|
141
|
+> **Note:** A production version of `MultipartConfigElement` would not be empty; it would specify target upload path, file size upload limits, and so forth.
|
|
139
|
|
142
|
|
|
140
|
-Re-run the app
|
|
|
|
141
|
---------------
|
|
|
|
|
|
143
|
+Re-run the application
|
|
|
|
144
|
+----------------------
|
|
142
|
|
145
|
|
|
143
|
Run the app again:
|
146
|
Run the app again:
|
|
144
|
|
147
|
|
|
|
|
|
|
|
146
|
$ mvn package spring-boot:run
|
149
|
$ mvn package spring-boot:run
|
|
147
|
```
|
150
|
```
|
|
148
|
|
151
|
|
|
149
|
-Now check out the output:
|
|
|
|
|
|
152
|
+Now check the output:
|
|
150
|
|
153
|
|
|
151
|
```sh
|
154
|
```sh
|
|
152
|
Let's inspect the beans provided by Spring Boot:
|
155
|
Let's inspect the beans provided by Spring Boot:
|
|
|
|
|
|
|
189
|
viewControllerHandlerMapping
|
192
|
viewControllerHandlerMapping
|
|
190
|
```
|
193
|
```
|
|
191
|
|
194
|
|
|
192
|
-There is little change from the previous output, except there is no longer a `tomcatEmbeddedServletContainerFactory`. Instead, there is a new `jettyEmbeddedServletContainer`.
|
|
|
|
|
|
195
|
+Little changes from the previous output, except there is no longer a `tomcatEmbeddedServletContainerFactory`. Instead, there is a new `jettyEmbeddedServletContainer`.
|
|
193
|
|
196
|
|
|
194
|
There is also the `multipartConfigElement` you added. But along with it came a `multipartResolver` [courtesy of Spring Boot](https://github.com/SpringSource/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java), a bean recommended to support file uploads with Spring MVC.
|
197
|
There is also the `multipartConfigElement` you added. But along with it came a `multipartResolver` [courtesy of Spring Boot](https://github.com/SpringSource/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java), a bean recommended to support file uploads with Spring MVC.
|
|
195
|
|
198
|
|
|
196
|
-Other than that, everything else appears the same, as it should be. Most the beans listed above provide Spring MVC's production-grade features. Just swapping one part, the servlet container, and adding upload support shouldn't cause a system wide ripple.
|
|
|
|
|
|
199
|
+Otherwise, everything is the same, as it should be. Most beans listed above provide Spring MVC's production-grade features. Simply swapping one part, the servlet container, and adding upload support shouldn't cause a system-wide ripple.
|
|
197
|
|
200
|
|
|
198
|
-Adding consumer-grade services
|
|
|
|
|
|
201
|
+Add consumer-grade services
|
|
199
|
------------------------------
|
202
|
------------------------------
|
|
200
|
-If you are building a web site for your business, there are probably some management services you are thinking about adding. Spring Boot provides several out of the box with its [actuator module][spring-boot-actuator] like health, audits, beans, and more.
|
|
|
|
|
|
203
|
+If you are building a web site for your business, you probably need to add some management services. Spring Boot provides several out of the box with its [actuator module][spring-boot-actuator], such as health, audits, beans, and more.
|
|
201
|
|
204
|
|
|
202
|
Add this to your pom.xml:
|
205
|
Add this to your pom.xml:
|
|
203
|
```xml
|
206
|
```xml
|
|
|
|
|
|
|
243
|
$ curl -X POST localhost:8080/shutdown
|
246
|
$ curl -X POST localhost:8080/shutdown
|
|
244
|
```
|
247
|
```
|
|
245
|
|
248
|
|
|
246
|
-The response shows that shutdown through REST is currently disabled by default (thank goodness!):
|
|
|
|
|
|
249
|
+The response shows that shutdown through REST is currently disabled by default:
|
|
247
|
```sh
|
250
|
```sh
|
|
248
|
{"message":"Shutdown not enabled, sorry."}
|
251
|
{"message":"Shutdown not enabled, sorry."}
|
|
249
|
```
|
252
|
```
|
|
250
|
|
253
|
|
|
251
|
-For more details about each of these REST points and how you can tune their settings with an ``application.properties` file, feel free to dig into the [Spring Boot][spring-boot] project.
|
|
|
|
|
|
254
|
+For more details about each of these REST points and how you can tune their settings with an ``application.properties` file, check out the [Spring Boot][spring-boot] project.
|
|
252
|
|
255
|
|
|
253
|
-Spring Boot's starters
|
|
|
|
|
|
256
|
+View Spring Boot's starters
|
|
254
|
----------------------
|
257
|
----------------------
|
|
255
|
You have seen some of Spring Boot's **starters**. Here is a complete list:
|
258
|
You have seen some of Spring Boot's **starters**. Here is a complete list:
|
|
256
|
- spring-boot-starter-actuator
|
259
|
- spring-boot-starter-actuator
|
|
|
|
|
|
|
265
|
- spring-boot-starter-web
|
268
|
- spring-boot-starter-web
|
|
266
|
|
269
|
|
|
267
|
|
270
|
|
|
268
|
-That is not all
|
|
|
|
269
|
----------------
|
|
|
|
270
|
-That last example showed how Spring Boot makes it easy to wire beans you may not be aware you need. And it showed how to turn on convenient management services.
|
|
|
|
|
|
271
|
+JAR support and Groovy support
|
|
|
|
272
|
+------------------------------
|
|
|
|
273
|
+The last example showed how Spring Boot makes it easy to wire beans you may not be aware that you need. And it showed how to turn on convenient management services.
|
|
271
|
|
274
|
|
|
272
|
But Spring Boot does yet more. It supports not only traditional WAR file deployments, but also makes it easy to put together executable JARs thanks to Spring Boot's loader module. The various guides demonstrate this dual support through the `spring-boot-maven-plugin`.
|
275
|
But Spring Boot does yet more. It supports not only traditional WAR file deployments, but also makes it easy to put together executable JARs thanks to Spring Boot's loader module. The various guides demonstrate this dual support through the `spring-boot-maven-plugin`.
|
|
273
|
|
276
|
|
|
|
|
|
|
|
290
|
|
293
|
|
|
291
|
Next, [install Spring Boot's CLI](https://github.com/SpringSource/spring-boot#installing-the-cli).
|
294
|
Next, [install Spring Boot's CLI](https://github.com/SpringSource/spring-boot#installing-the-cli).
|
|
292
|
|
295
|
|
|
293
|
-Finally, run it as follows:
|
|
|
|
|
|
296
|
+Run it as follows:
|
|
294
|
|
297
|
|
|
295
|
```sh
|
298
|
```sh
|
|
296
|
$ spring run app.groovy
|
299
|
$ spring run app.groovy
|
|
|
|
|
|
|
300
|
|
303
|
|
|
301
|
Spring Boot dynamically adds key annotations to your code and leverages [Groovy Grapes](http://groovy.codehaus.org/Grape) to pull down needed libraries to make the app run.
|
304
|
Spring Boot dynamically adds key annotations to your code and leverages [Groovy Grapes](http://groovy.codehaus.org/Grape) to pull down needed libraries to make the app run.
|
|
302
|
|
305
|
|
|
303
|
-Congratulations!
|
|
|
|
|
|
306
|
+Summary
|
|
304
|
----------------
|
307
|
----------------
|
|
305
|
-Spring Boot is powerful, but frankly too big to fit into a single guide. This is just a sampling of how much it can ramp up your development pace, letting you focus on business features and not worry about infrastructure.
|
|
|
|
306
|
-
|
|
|
|
307
|
-As you read more of this site's getting started guides, you will see it used all over the place. It might not be obvious at first, because Spring Boot is so good at adding the things you need without getting in your way. But after using it for a bit, you may wonder how you lived without it.
|
|
|
|
|
|
308
|
+Congratulations! You built a simple web application with Spring Boot and learned how it can ramp up your development pace.
|
|
308
|
|
309
|
|
|
309
|
[spring-boot]: https://github.com/SpringSource/spring-boot
|
310
|
[spring-boot]: https://github.com/SpringSource/spring-boot
|
|
310
|
[spring-boot-actuator]: https://github.com/SpringSource/spring-boot/blob/master/spring-boot-actuator/README.md
|
311
|
[spring-boot-actuator]: https://github.com/SpringSource/spring-boot/blob/master/spring-boot-actuator/README.md
|