(window.webpackJsonp=window.webpackJsonp||[]).push([[52],{479:function(e,t,a){"use strict";a.r(t);var n=a(56),o=Object(n.a)({},(function(){var e=this,t=e.$createElement,a=e._self._c||t;return a("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[a("h1",{attrs:{id:"getting-started"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#getting-started"}},[e._v("#")]),e._v(" Getting Started")]),e._v(" "),a("p",[e._v("If you are getting started with Spring Boot, or “Spring” in general, start by reading this section.\nIt answers the basic “what?”, “how?” and “why?” questions.\nIt includes an introduction to Spring Boot, along with installation instructions.\nWe then walk you through building your first Spring Boot application, discussing some core principles as we go.")]),e._v(" "),a("h2",{attrs:{id:"_1-introducing-spring-boot"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_1-introducing-spring-boot"}},[e._v("#")]),e._v(" 1. Introducing Spring Boot")]),e._v(" "),a("p",[e._v("Spring Boot helps you to create stand-alone, production-grade Spring-based applications that you can run.\nWe take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss.\nMost Spring Boot applications need very little Spring configuration.")]),e._v(" "),a("p",[e._v("You can use Spring Boot to create Java applications that can be started by using "),a("code",[e._v("java -jar")]),e._v(" or more traditional war deployments.\nWe also provide a command line tool that runs “spring scripts”.")]),e._v(" "),a("p",[e._v("Our primary goals are:")]),e._v(" "),a("ul",[a("li",[a("p",[e._v("Provide a radically faster and widely accessible getting-started experience for all Spring development.")])]),e._v(" "),a("li",[a("p",[e._v("Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults.")])]),e._v(" "),a("li",[a("p",[e._v("Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration).")])]),e._v(" "),a("li",[a("p",[e._v("Absolutely no code generation and no requirement for XML configuration.")])])]),e._v(" "),a("h2",{attrs:{id:"_2-system-requirements"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-system-requirements"}},[e._v("#")]),e._v(" 2. System Requirements")]),e._v(" "),a("p",[e._v("Spring Boot 2.6.4 requires "),a("a",{attrs:{href:"https://www.java.com",target:"_blank",rel:"noopener noreferrer"}},[e._v("Java 8"),a("OutboundLink")],1),e._v(" and is compatible up to and including Java 17."),a("a",{attrs:{href:"https://docs.spring.io/spring-framework/docs/5.3.16/reference/html/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Spring Framework 5.3.16"),a("OutboundLink")],1),e._v(" or above is also required.")]),e._v(" "),a("p",[e._v("Explicit build support is provided for the following build tools:")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th",[e._v("Build Tool")]),e._v(" "),a("th",[e._v("Version")])])]),e._v(" "),a("tbody",[a("tr",[a("td",[e._v("Maven")]),e._v(" "),a("td",[e._v("3.5+")])]),e._v(" "),a("tr",[a("td",[e._v("Gradle")]),e._v(" "),a("td",[e._v("6.8.x, 6.9.x, and 7.x")])])])]),e._v(" "),a("h3",{attrs:{id:"_2-1-servlet-containers"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_2-1-servlet-containers"}},[e._v("#")]),e._v(" 2.1. Servlet Containers")]),e._v(" "),a("p",[e._v("Spring Boot supports the following embedded servlet containers:")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th",[e._v("Name")]),e._v(" "),a("th",[e._v("Servlet Version")])])]),e._v(" "),a("tbody",[a("tr",[a("td",[e._v("Tomcat 9.0")]),e._v(" "),a("td",[e._v("4.0")])]),e._v(" "),a("tr",[a("td",[e._v("Jetty 9.4")]),e._v(" "),a("td",[e._v("3.1")])]),e._v(" "),a("tr",[a("td",[e._v("Jetty 10.0")]),e._v(" "),a("td",[e._v("4.0")])]),e._v(" "),a("tr",[a("td",[e._v("Undertow 2.0")]),e._v(" "),a("td",[e._v("4.0")])])])]),e._v(" "),a("p",[e._v("You can also deploy Spring Boot applications to any servlet 3.1+ compatible container.")]),e._v(" "),a("h2",{attrs:{id:"_3-installing-spring-boot"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-installing-spring-boot"}},[e._v("#")]),e._v(" 3. Installing Spring Boot")]),e._v(" "),a("p",[e._v("Spring Boot can be used with “classic” Java development tools or installed as a command line tool.\nEither way, you need "),a("a",{attrs:{href:"https://www.java.com",target:"_blank",rel:"noopener noreferrer"}},[e._v("Java SDK v1.8"),a("OutboundLink")],1),e._v(" or higher.\nBefore you begin, you should check your current Java installation by using the following command:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ java -version\n")])])]),a("p",[e._v("If you are new to Java development or if you want to experiment with Spring Boot, you might want to try the "),a("a",{attrs:{href:"#getting-started.installing.cli"}},[e._v("Spring Boot CLI")]),e._v(" (Command Line Interface) first.\nOtherwise, read on for “classic” installation instructions.")]),e._v(" "),a("h3",{attrs:{id:"_3-1-installation-instructions-for-the-java-developer"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-1-installation-instructions-for-the-java-developer"}},[e._v("#")]),e._v(" 3.1. Installation Instructions for the Java Developer")]),e._v(" "),a("p",[e._v("You can use Spring Boot in the same way as any standard Java library.\nTo do so, include the appropriate "),a("code",[e._v("spring-boot-*.jar")]),e._v(" files on your classpath.\nSpring Boot does not require any special tools integration, so you can use any IDE or text editor.\nAlso, there is nothing special about a Spring Boot application, so you can run and debug a Spring Boot application as you would any other Java program.")]),e._v(" "),a("p",[e._v("Although you "),a("em",[e._v("could")]),e._v(" copy Spring Boot jars, we generally recommend that you use a build tool that supports dependency management (such as Maven or Gradle).")]),e._v(" "),a("h4",{attrs:{id:"_3-1-1-maven-installation"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-1-1-maven-installation"}},[e._v("#")]),e._v(" 3.1.1. Maven Installation")]),e._v(" "),a("p",[e._v("Spring Boot is compatible with Apache Maven 3.3 or above.\nIf you do not already have Maven installed, you can follow the instructions at "),a("a",{attrs:{href:"https://maven.apache.org",target:"_blank",rel:"noopener noreferrer"}},[e._v("maven.apache.org"),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("On many operating systems, Maven can be installed with a package manager."),a("br"),e._v("If you use OSX Homebrew, try "),a("code",[e._v("brew install maven")]),e._v("."),a("br"),e._v("Ubuntu users can run "),a("code",[e._v("sudo apt-get install maven")]),e._v("."),a("br"),e._v("Windows users with "),a("a",{attrs:{href:"https://chocolatey.org/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Chocolatey"),a("OutboundLink")],1),e._v(" can run "),a("code",[e._v("choco install maven")]),e._v(" from an elevated (administrator) prompt.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("Spring Boot dependencies use the "),a("code",[e._v("org.springframework.boot")]),e._v(" "),a("code",[e._v("groupId")]),e._v(".\nTypically, your Maven POM file inherits from the "),a("code",[e._v("spring-boot-starter-parent")]),e._v(" project and declares dependencies to one or more "),a("RouterLink",{attrs:{to:"/en/spring-boot/using.html#using.build-systems.starters"}},[e._v("“Starters”")]),e._v(".\nSpring Boot also provides an optional "),a("RouterLink",{attrs:{to:"/en/spring-boot/build-tool-plugins.html#build-tool-plugins.maven"}},[e._v("Maven plugin")]),e._v(" to create executable jars.")],1),e._v(" "),a("p",[e._v("More details on getting started with Spring Boot and Maven can be found in the "),a("a",{attrs:{href:"https://docs.spring.io/spring-boot/docs/2.6.4/maven-plugin/reference/htmlsingle/#getting-started",target:"_blank",rel:"noopener noreferrer"}},[e._v("Getting Started section"),a("OutboundLink")],1),e._v(" of the Maven plugin’s reference guide.")]),e._v(" "),a("h4",{attrs:{id:"_3-1-2-gradle-installation"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-1-2-gradle-installation"}},[e._v("#")]),e._v(" 3.1.2. Gradle Installation")]),e._v(" "),a("p",[e._v("Spring Boot is compatible with Gradle 6.8, 6.9, and 7.x.\nIf you do not already have Gradle installed, you can follow the instructions at "),a("a",{attrs:{href:"https://gradle.org",target:"_blank",rel:"noopener noreferrer"}},[e._v("gradle.org"),a("OutboundLink")],1),e._v(".")]),e._v(" "),a("p",[e._v("Spring Boot dependencies can be declared by using the "),a("code",[e._v("org.springframework.boot")]),e._v(" "),a("code",[e._v("group")]),e._v(".\nTypically, your project declares dependencies to one or more "),a("RouterLink",{attrs:{to:"/en/spring-boot/using.html#using.build-systems.starters"}},[e._v("“Starters”")]),e._v(".\nSpring Boot provides a useful "),a("RouterLink",{attrs:{to:"/en/spring-boot/build-tool-plugins.html#build-tool-plugins.gradle"}},[e._v("Gradle plugin")]),e._v(" that can be used to simplify dependency declarations and to create executable jars.")],1),e._v(" "),a("p",[e._v("Gradle Wrapper")]),e._v(" "),a("p",[e._v("The Gradle Wrapper provides a nice way of “obtaining” Gradle when you need to build a project.\nIt is a small script and library that you commit alongside your code to bootstrap the build process.\nSee "),a("a",{attrs:{href:"https://docs.gradle.org/current/userguide/gradle_wrapper.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("docs.gradle.org/current/userguide/gradle_wrapper.html"),a("OutboundLink")],1),e._v(" for details.")]),e._v(" "),a("p",[e._v("More details on getting started with Spring Boot and Gradle can be found in the "),a("a",{attrs:{href:"https://docs.spring.io/spring-boot/docs/2.6.4/gradle-plugin/reference/htmlsingle/#getting-started",target:"_blank",rel:"noopener noreferrer"}},[e._v("Getting Started section"),a("OutboundLink")],1),e._v(" of the Gradle plugin’s reference guide.")]),e._v(" "),a("h3",{attrs:{id:"_3-2-installing-the-spring-boot-cli"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-2-installing-the-spring-boot-cli"}},[e._v("#")]),e._v(" 3.2. Installing the Spring Boot CLI")]),e._v(" "),a("p",[e._v("The Spring Boot CLI (Command Line Interface) is a command line tool that you can use to quickly prototype with Spring.\nIt lets you run "),a("a",{attrs:{href:"https://groovy-lang.org/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Groovy"),a("OutboundLink")],1),e._v(" scripts, which means that you have a familiar Java-like syntax without so much boilerplate code.")]),e._v(" "),a("p",[e._v("You do not need to use the CLI to work with Spring Boot, but it is a quick way to get a Spring application off the ground without an IDE.")]),e._v(" "),a("h4",{attrs:{id:"_3-2-1-manual-installation"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-2-1-manual-installation"}},[e._v("#")]),e._v(" 3.2.1. Manual Installation")]),e._v(" "),a("p",[e._v("You can download the Spring CLI distribution from the Spring software repository:")]),e._v(" "),a("ul",[a("li",[a("p",[a("a",{attrs:{href:"https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.6.4/spring-boot-cli-2.6.4-bin.zip",target:"_blank",rel:"noopener noreferrer"}},[e._v("spring-boot-cli-2.6.4-bin.zip"),a("OutboundLink")],1)])]),e._v(" "),a("li",[a("p",[a("a",{attrs:{href:"https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.6.4/spring-boot-cli-2.6.4-bin.tar.gz",target:"_blank",rel:"noopener noreferrer"}},[e._v("spring-boot-cli-2.6.4-bin.tar.gz"),a("OutboundLink")],1)])])]),e._v(" "),a("p",[e._v("Cutting edge"),a("a",{attrs:{href:"https://repo.spring.io/snapshot/org/springframework/boot/spring-boot-cli/",target:"_blank",rel:"noopener noreferrer"}},[e._v("snapshot distributions"),a("OutboundLink")],1),e._v(" are also available.")]),e._v(" "),a("p",[e._v("Once downloaded, follow the "),a("a",{attrs:{href:"https://raw.githubusercontent.com/spring-projects/spring-boot/v2.6.4/spring-boot-project/spring-boot-cli/src/main/content/INSTALL.txt",target:"_blank",rel:"noopener noreferrer"}},[e._v("INSTALL.txt"),a("OutboundLink")],1),e._v(" instructions from the unpacked archive.\nIn summary, there is a "),a("code",[e._v("spring")]),e._v(" script ("),a("code",[e._v("spring.bat")]),e._v(" for Windows) in a "),a("code",[e._v("bin/")]),e._v(" directory in the "),a("code",[e._v(".zip")]),e._v(" file.\nAlternatively, you can use "),a("code",[e._v("java -jar")]),e._v(" with the "),a("code",[e._v(".jar")]),e._v(" file (the script helps you to be sure that the classpath is set correctly).")]),e._v(" "),a("h4",{attrs:{id:"_3-2-2-installation-with-sdkman"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-2-2-installation-with-sdkman"}},[e._v("#")]),e._v(" 3.2.2. Installation with SDKMAN!")]),e._v(" "),a("p",[e._v("SDKMAN! (The Software Development Kit Manager) can be used for managing multiple versions of various binary SDKs, including Groovy and the Spring Boot CLI.\nGet SDKMAN! from "),a("a",{attrs:{href:"https://sdkman.io",target:"_blank",rel:"noopener noreferrer"}},[e._v("sdkman.io"),a("OutboundLink")],1),e._v(" and install Spring Boot by using the following commands:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ sdk install springboot\n$ spring --version\nSpring CLI v2.6.4\n")])])]),a("p",[e._v("If you develop features for the CLI and want access to the version you built, use the following commands:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-2.6.4-bin/spring-2.6.4/\n$ sdk default springboot dev\n$ spring --version\nSpring CLI v2.6.4\n")])])]),a("p",[e._v("The preceding instructions install a local instance of "),a("code",[e._v("spring")]),e._v(" called the "),a("code",[e._v("dev")]),e._v(" instance.\nIt points at your target build location, so every time you rebuild Spring Boot, "),a("code",[e._v("spring")]),e._v(" is up-to-date.")]),e._v(" "),a("p",[e._v("You can see it by running the following command:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ sdk ls springboot\n\n================================================================================\nAvailable Springboot Versions\n================================================================================\n> + dev\n* 2.6.4\n\n================================================================================\n+ - local version\n* - installed\n> - currently in use\n================================================================================\n")])])]),a("h4",{attrs:{id:"_3-2-3-osx-homebrew-installation"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-2-3-osx-homebrew-installation"}},[e._v("#")]),e._v(" 3.2.3. OSX Homebrew Installation")]),e._v(" "),a("p",[e._v("If you are on a Mac and use "),a("a",{attrs:{href:"https://brew.sh/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Homebrew"),a("OutboundLink")],1),e._v(", you can install the Spring Boot CLI by using the following commands:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ brew tap spring-io/tap\n$ brew install spring-boot\n")])])]),a("p",[e._v("Homebrew installs "),a("code",[e._v("spring")]),e._v(" to "),a("code",[e._v("/usr/local/bin")]),e._v(".")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If you do not see the formula, your installation of brew might be out-of-date."),a("br"),e._v("In that case, run "),a("code",[e._v("brew update")]),e._v(" and try again.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_3-2-4-macports-installation"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-2-4-macports-installation"}},[e._v("#")]),e._v(" 3.2.4. MacPorts Installation")]),e._v(" "),a("p",[e._v("If you are on a Mac and use "),a("a",{attrs:{href:"https://www.macports.org/",target:"_blank",rel:"noopener noreferrer"}},[e._v("MacPorts"),a("OutboundLink")],1),e._v(", you can install the Spring Boot CLI by using the following command:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ sudo port install spring-boot-cli\n")])])]),a("h4",{attrs:{id:"_3-2-5-command-line-completion"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-2-5-command-line-completion"}},[e._v("#")]),e._v(" 3.2.5. Command-line Completion")]),e._v(" "),a("p",[e._v("The Spring Boot CLI includes scripts that provide command completion for the "),a("a",{attrs:{href:"https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29",target:"_blank",rel:"noopener noreferrer"}},[e._v("BASH"),a("OutboundLink")],1),e._v(" and "),a("a",{attrs:{href:"https://en.wikipedia.org/wiki/Z_shell",target:"_blank",rel:"noopener noreferrer"}},[e._v("zsh"),a("OutboundLink")],1),e._v(" shells.\nYou can "),a("code",[e._v("source")]),e._v(" the script (also named "),a("code",[e._v("spring")]),e._v(") in any shell or put it in your personal or system-wide bash completion initialization.\nOn a Debian system, the system-wide scripts are in "),a("code",[e._v("/shell-completion/bash")]),e._v(" and all scripts in that directory are executed when a new shell starts.\nFor example, to run the script manually if you have installed by using SDKMAN!, use the following commands:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring\n$ spring \n grab help jar run test version\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If you install the Spring Boot CLI by using Homebrew or MacPorts, the command-line completion scripts are automatically registered with your shell.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_3-2-6-windows-scoop-installation"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-2-6-windows-scoop-installation"}},[e._v("#")]),e._v(" 3.2.6. Windows Scoop Installation")]),e._v(" "),a("p",[e._v("If you are on a Windows and use "),a("a",{attrs:{href:"https://scoop.sh/",target:"_blank",rel:"noopener noreferrer"}},[e._v("Scoop"),a("OutboundLink")],1),e._v(", you can install the Spring Boot CLI by using the following commands:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("> scoop bucket add extras\n> scoop install springboot\n")])])]),a("p",[e._v("Scoop installs "),a("code",[e._v("spring")]),e._v(" to "),a("code",[e._v("~/scoop/apps/springboot/current/bin")]),e._v(".")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("If you do not see the app manifest, your installation of scoop might be out-of-date."),a("br"),e._v("In that case, run "),a("code",[e._v("scoop update")]),e._v(" and try again.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_3-2-7-quick-start-spring-cli-example"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_3-2-7-quick-start-spring-cli-example"}},[e._v("#")]),e._v(" 3.2.7. Quick-start Spring CLI Example")]),e._v(" "),a("p",[e._v("You can use the following web application to test your installation.\nTo start, create a file called "),a("code",[e._v("app.groovy")]),e._v(", as follows:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('@RestController\nclass ThisWillActuallyRun {\n\n @RequestMapping("/")\n String home() {\n "Hello World!"\n }\n\n}\n\n')])])]),a("p",[e._v("Then run it from a shell, as follows:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ spring run app.groovy\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The first run of your application is slow, as dependencies are downloaded."),a("br"),e._v("Subsequent runs are much quicker.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("Open "),a("code",[e._v("[localhost:8080](http://localhost:8080)")]),e._v(" in your favorite web browser.\nYou should see the following output:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("Hello World!\n")])])]),a("h2",{attrs:{id:"_4-developing-your-first-spring-boot-application"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_4-developing-your-first-spring-boot-application"}},[e._v("#")]),e._v(" 4. Developing Your First Spring Boot Application")]),e._v(" "),a("p",[e._v("This section describes how to develop a small “Hello World!” web application that highlights some of Spring Boot’s key features.\nWe use Maven to build this project, since most IDEs support it.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The "),a("a",{attrs:{href:"https://spring.io",target:"_blank",rel:"noopener noreferrer"}},[e._v("spring.io"),a("OutboundLink")],1),e._v(" web site contains many “Getting Started” "),a("a",{attrs:{href:"https://spring.io/guides",target:"_blank",rel:"noopener noreferrer"}},[e._v("guides"),a("OutboundLink")],1),e._v(" that use Spring Boot."),a("br"),e._v("If you need to solve a specific problem, check there first."),a("br"),a("br"),e._v("You can shortcut the steps below by going to "),a("a",{attrs:{href:"https://start.spring.io",target:"_blank",rel:"noopener noreferrer"}},[e._v("start.spring.io"),a("OutboundLink")],1),e._v(' and choosing the "Web" starter from the dependencies searcher.'),a("br"),e._v("Doing so generates a new project structure so that you can "),a("a",{attrs:{href:"#getting-started.first-application.code"}},[e._v("start coding right away")]),e._v("."),a("br"),e._v("Check the "),a("a",{attrs:{href:"https://github.com/spring-io/start.spring.io/blob/main/USING.adoc",target:"_blank",rel:"noopener noreferrer"}},[e._v("start.spring.io user guide"),a("OutboundLink")],1),e._v(" for more details.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("Before we begin, open a terminal and run the following commands to ensure that you have valid versions of Java and Maven installed:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('$ java -version\njava version "1.8.0_102"\nJava(TM) SE Runtime Environment (build 1.8.0_102-b14)\nJava HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)\n')])])]),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ mvn -v\nApache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T14:33:14-04:00)\nMaven home: /usr/local/Cellar/maven/3.3.9/libexec\nJava version: 1.8.0_102, vendor: Oracle Corporation\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("This sample needs to be created in its own directory."),a("br"),e._v("Subsequent instructions assume that you have created a suitable directory and that it is your current directory.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h3",{attrs:{id:"_4-1-creating-the-pom"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_4-1-creating-the-pom"}},[e._v("#")]),e._v(" 4.1. Creating the POM")]),e._v(" "),a("p",[e._v("We need to start by creating a Maven "),a("code",[e._v("pom.xml")]),e._v(" file.\nThe "),a("code",[e._v("pom.xml")]),e._v(" is the recipe that is used to build your project.\nOpen your favorite text editor and add the following:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('\n\n 4.0.0\n\n com.example\n myproject\n 0.0.1-SNAPSHOT\n\n \n org.springframework.boot\n spring-boot-starter-parent\n 2.6.4\n \n\n \x3c!-- Additional lines to be added here... --\x3e\n\n\n')])])]),a("p",[e._v("The preceding listing should give you a working build.\nYou can test it by running "),a("code",[e._v("mvn package")]),e._v(" (for now, you can ignore the “jar will be empty - no content was marked for inclusion!” warning).")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("At this point, you could import the project into an IDE (most modern Java IDEs include built-in support for Maven)."),a("br"),e._v("For simplicity, we continue to use a plain text editor for this example.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h3",{attrs:{id:"_4-2-adding-classpath-dependencies"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_4-2-adding-classpath-dependencies"}},[e._v("#")]),e._v(" 4.2. Adding Classpath Dependencies")]),e._v(" "),a("p",[e._v("Spring Boot provides a number of “Starters” that let you add jars to your classpath.\nOur applications for smoke tests use the "),a("code",[e._v("spring-boot-starter-parent")]),e._v(" in the "),a("code",[e._v("parent")]),e._v(" section of the POM.\nThe "),a("code",[e._v("spring-boot-starter-parent")]),e._v(" is a special starter that provides useful Maven defaults.\nIt also provides a "),a("RouterLink",{attrs:{to:"/en/spring-boot/using.html#using.build-systems.dependency-management"}},[a("code",[e._v("dependency-management")])]),e._v(" section so that you can omit "),a("code",[e._v("version")]),e._v(" tags for “blessed” dependencies.")],1),e._v(" "),a("p",[e._v("Other “Starters” provide dependencies that you are likely to need when developing a specific type of application.\nSince we are developing a web application, we add a "),a("code",[e._v("spring-boot-starter-web")]),e._v(" dependency.\nBefore that, we can look at what we currently have by running the following command:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ mvn dependency:tree\n\n[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT\n")])])]),a("p",[e._v("The "),a("code",[e._v("mvn dependency:tree")]),e._v(" command prints a tree representation of your project dependencies.\nYou can see that "),a("code",[e._v("spring-boot-starter-parent")]),e._v(" provides no dependencies by itself.\nTo add the necessary dependencies, edit your "),a("code",[e._v("pom.xml")]),e._v(" and add the "),a("code",[e._v("spring-boot-starter-web")]),e._v(" dependency immediately below the "),a("code",[e._v("parent")]),e._v(" section:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("\n \n org.springframework.boot\n spring-boot-starter-web\n \n\n")])])]),a("p",[e._v("If you run "),a("code",[e._v("mvn dependency:tree")]),e._v(" again, you see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.")]),e._v(" "),a("h3",{attrs:{id:"_4-3-writing-the-code"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_4-3-writing-the-code"}},[e._v("#")]),e._v(" 4.3. Writing the Code")]),e._v(" "),a("p",[e._v("To finish our application, we need to create a single Java file.\nBy default, Maven compiles sources from "),a("code",[e._v("src/main/java")]),e._v(", so you need to create that directory structure and then add a file named "),a("code",[e._v("src/main/java/MyApplication.java")]),e._v(" to contain the following code:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.EnableAutoConfiguration;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\n@EnableAutoConfiguration\npublic class MyApplication {\n\n @RequestMapping("/")\n String home() {\n return "Hello World!";\n }\n\n public static void main(String[] args) {\n SpringApplication.run(MyApplication.class, args);\n }\n\n}\n\n')])])]),a("p",[e._v("Although there is not much code here, quite a lot is going on.\nWe step through the important parts in the next few sections.")]),e._v(" "),a("h4",{attrs:{id:"_4-3-1-the-restcontroller-and-requestmapping-annotations"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_4-3-1-the-restcontroller-and-requestmapping-annotations"}},[e._v("#")]),e._v(" 4.3.1. The @RestController and @RequestMapping Annotations")]),e._v(" "),a("p",[e._v("The first annotation on our "),a("code",[e._v("MyApplication")]),e._v(" class is "),a("code",[e._v("@RestController")]),e._v(".\nThis is known as a "),a("em",[e._v("stereotype")]),e._v(" annotation.\nIt provides hints for people reading the code and for Spring that the class plays a specific role.\nIn this case, our class is a web "),a("code",[e._v("@Controller")]),e._v(", so Spring considers it when handling incoming web requests.")]),e._v(" "),a("p",[e._v("The "),a("code",[e._v("@RequestMapping")]),e._v(" annotation provides “routing” information.\nIt tells Spring that any HTTP request with the "),a("code",[e._v("/")]),e._v(" path should be mapped to the "),a("code",[e._v("home")]),e._v(" method.\nThe "),a("code",[e._v("@RestController")]),e._v(" annotation tells Spring to render the resulting string directly back to the caller.")]),e._v(" "),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The "),a("code",[e._v("@RestController")]),e._v(" and "),a("code",[e._v("@RequestMapping")]),e._v(" annotations are Spring MVC annotations (they are not specific to Spring Boot)."),a("br"),e._v("See the "),a("a",{attrs:{href:"https://docs.spring.io/spring-framework/docs/5.3.16/reference/html/web.html#mvc",target:"_blank",rel:"noopener noreferrer"}},[e._v("MVC section"),a("OutboundLink")],1),e._v(" in the Spring Reference Documentation for more details.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("h4",{attrs:{id:"_4-3-2-the-enableautoconfiguration-annotation"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_4-3-2-the-enableautoconfiguration-annotation"}},[e._v("#")]),e._v(" 4.3.2. The @EnableAutoConfiguration Annotation")]),e._v(" "),a("p",[e._v("The second class-level annotation is "),a("code",[e._v("@EnableAutoConfiguration")]),e._v(".\nThis annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added.\nSince "),a("code",[e._v("spring-boot-starter-web")]),e._v(" added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.")]),e._v(" "),a("p",[e._v("Starters and Auto-configuration")]),e._v(" "),a("p",[e._v("Auto-configuration is designed to work well with “Starters”, but the two concepts are not directly tied.\nYou are free to pick and choose jar dependencies outside of the starters.\nSpring Boot still does its best to auto-configure your application.")]),e._v(" "),a("h4",{attrs:{id:"_4-3-3-the-main-method"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_4-3-3-the-main-method"}},[e._v("#")]),e._v(" 4.3.3. The “main” Method")]),e._v(" "),a("p",[e._v("The final part of our application is the "),a("code",[e._v("main")]),e._v(" method.\nThis is a standard method that follows the Java convention for an application entry point.\nOur main method delegates to Spring Boot’s "),a("code",[e._v("SpringApplication")]),e._v(" class by calling "),a("code",[e._v("run")]),e._v("."),a("code",[e._v("SpringApplication")]),e._v(" bootstraps our application, starting Spring, which, in turn, starts the auto-configured Tomcat web server.\nWe need to pass "),a("code",[e._v("MyApplication.class")]),e._v(" as an argument to the "),a("code",[e._v("run")]),e._v(" method to tell "),a("code",[e._v("SpringApplication")]),e._v(" which is the primary Spring component.\nThe "),a("code",[e._v("args")]),e._v(" array is also passed through to expose any command-line arguments.")]),e._v(" "),a("h3",{attrs:{id:"_4-4-running-the-example"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_4-4-running-the-example"}},[e._v("#")]),e._v(" 4.4. Running the Example")]),e._v(" "),a("p",[e._v("At this point, your application should work.\nSince you used the "),a("code",[e._v("spring-boot-starter-parent")]),e._v(" POM, you have a useful "),a("code",[e._v("run")]),e._v(" goal that you can use to start the application.\nType "),a("code",[e._v("mvn spring-boot:run")]),e._v(" from the root project directory to start the application.\nYou should see output similar to the following:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ mvn spring-boot:run\n\n . ____ _ __ _ _\n /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\\n( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\\n \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )\n ' |____| .__|_| |_|_| |_\\__, | / / / /\n =========|_|==============|___/=/_/_/_/\n :: Spring Boot :: (v2.6.4)\n....... . . .\n....... . . . (log output here)\n....... . . .\n........ Started MyApplication in 2.222 seconds (JVM running for 6.514)\n")])])]),a("p",[e._v("If you open a web browser to "),a("code",[e._v("[localhost:8080](http://localhost:8080)")]),e._v(", you should see the following output:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("Hello World!\n")])])]),a("p",[e._v("To gracefully exit the application, press "),a("code",[e._v("ctrl-c")]),e._v(".")]),e._v(" "),a("h3",{attrs:{id:"_4-5-creating-an-executable-jar"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_4-5-creating-an-executable-jar"}},[e._v("#")]),e._v(" 4.5. Creating an Executable Jar")]),e._v(" "),a("p",[e._v("We finish our example by creating a completely self-contained executable jar file that we could run in production.\nExecutable jars (sometimes called “fat jars”) are archives containing your compiled classes along with all of the jar dependencies that your code needs to run.")]),e._v(" "),a("p",[e._v("Executable jars and Java")]),e._v(" "),a("p",[e._v("Java does not provide a standard way to load nested jar files (jar files that are themselves contained within a jar).\nThis can be problematic if you are looking to distribute a self-contained application.")]),e._v(" "),a("p",[e._v("To solve this problem, many developers use “uber” jars.\nAn uber jar packages all the classes from all the application’s dependencies into a single archive.\nThe problem with this approach is that it becomes hard to see which libraries are in your application.\nIt can also be problematic if the same filename is used (but with different content) in multiple jars.")]),e._v(" "),a("p",[e._v("Spring Boot takes a "),a("RouterLink",{attrs:{to:"/en/spring-boot/executable-jar.html#appendix.executable-jar"}},[e._v("different approach")]),e._v(" and lets you actually nest jars directly.")],1),e._v(" "),a("p",[e._v("To create an executable jar, we need to add the "),a("code",[e._v("spring-boot-maven-plugin")]),e._v(" to our "),a("code",[e._v("pom.xml")]),e._v(".\nTo do so, insert the following lines just below the "),a("code",[e._v("dependencies")]),e._v(" section:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("\n \n \n org.springframework.boot\n spring-boot-maven-plugin\n \n \n\n")])])]),a("table",[a("thead",[a("tr",[a("th"),e._v(" "),a("th",[e._v("The "),a("code",[e._v("spring-boot-starter-parent")]),e._v(" POM includes "),a("code",[e._v("")]),e._v(" configuration to bind the "),a("code",[e._v("repackage")]),e._v(" goal."),a("br"),e._v("If you do not use the parent POM, you need to declare this configuration yourself."),a("br"),e._v("See the "),a("a",{attrs:{href:"https://docs.spring.io/spring-boot/docs/2.6.4/maven-plugin/reference/htmlsingle/#getting-started",target:"_blank",rel:"noopener noreferrer"}},[e._v("plugin documentation"),a("OutboundLink")],1),e._v(" for details.")])])]),e._v(" "),a("tbody")]),e._v(" "),a("p",[e._v("Save your "),a("code",[e._v("pom.xml")]),e._v(" and run "),a("code",[e._v("mvn package")]),e._v(" from the command line, as follows:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ mvn package\n\n[INFO] Scanning for projects...\n[INFO]\n[INFO] ------------------------------------------------------------------------\n[INFO] Building myproject 0.0.1-SNAPSHOT\n[INFO] ------------------------------------------------------------------------\n[INFO] .... ..\n[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---\n[INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar\n[INFO]\n[INFO] --- spring-boot-maven-plugin:2.6.4:repackage (default) @ myproject ---\n[INFO] ------------------------------------------------------------------------\n[INFO] BUILD SUCCESS\n[INFO] ------------------------------------------------------------------------\n")])])]),a("p",[e._v("If you look in the "),a("code",[e._v("target")]),e._v(" directory, you should see "),a("code",[e._v("myproject-0.0.1-SNAPSHOT.jar")]),e._v(".\nThe file should be around 10 MB in size.\nIf you want to peek inside, you can use "),a("code",[e._v("jar tvf")]),e._v(", as follows:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ jar tvf target/myproject-0.0.1-SNAPSHOT.jar\n")])])]),a("p",[e._v("You should also see a much smaller file named "),a("code",[e._v("myproject-0.0.1-SNAPSHOT.jar.original")]),e._v(" in the "),a("code",[e._v("target")]),e._v(" directory.\nThis is the original jar file that Maven created before it was repackaged by Spring Boot.")]),e._v(" "),a("p",[e._v("To run that application, use the "),a("code",[e._v("java -jar")]),e._v(" command, as follows:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("$ java -jar target/myproject-0.0.1-SNAPSHOT.jar\n\n . ____ _ __ _ _\n /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\\n( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\\n \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )\n ' |____| .__|_| |_|_| |_\\__, | / / / /\n =========|_|==============|___/=/_/_/_/\n :: Spring Boot :: (v2.6.4)\n....... . . .\n....... . . . (log output here)\n....... . . .\n........ Started MyApplication in 2.536 seconds (JVM running for 2.864)\n")])])]),a("p",[e._v("As before, to exit the application, press "),a("code",[e._v("ctrl-c")]),e._v(".")]),e._v(" "),a("h2",{attrs:{id:"_5-what-to-read-next"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#_5-what-to-read-next"}},[e._v("#")]),e._v(" 5. What to Read Next")]),e._v(" "),a("p",[e._v("Hopefully, this section provided some of the Spring Boot basics and got you on your way to writing your own applications.\nIf you are a task-oriented type of developer, you might want to jump over to "),a("a",{attrs:{href:"https://spring.io",target:"_blank",rel:"noopener noreferrer"}},[e._v("spring.io"),a("OutboundLink")],1),e._v(" and follow some of the "),a("a",{attrs:{href:"https://spring.io/guides/",target:"_blank",rel:"noopener noreferrer"}},[e._v("getting started"),a("OutboundLink")],1),e._v(" guides that solve specific “How do I do that with Spring?” problems.\nWe also have Spring Boot-specific “"),a("RouterLink",{attrs:{to:"/en/spring-boot/howto.html#howto"}},[e._v("How-to")]),e._v("” reference documentation.")],1),e._v(" "),a("p",[e._v("Otherwise, the next logical step is to read "),a("em",[a("RouterLink",{attrs:{to:"/en/spring-boot/using.html#using"}},[e._v("using.html")])],1),e._v(".\nIf you are really impatient, you could also jump ahead and read about "),a("em",[a("RouterLink",{attrs:{to:"/en/spring-boot/features.html#features"}},[e._v("Spring Boot features")])],1),e._v(".")])])}),[],!1,null,null,null);t.default=o.exports}}]);