WIP
This commit is contained in:
192
README.adoc
192
README.adoc
@@ -243,7 +243,9 @@ Stub Runner` properties, as shown in the following example:
|
||||
+
|
||||
[source,yaml,indent=0]
|
||||
----
|
||||
Unresolved directive in _verifier_how_it_works.adoc - include::https://raw.githubusercontent.com/spring-cloud/spring-cloud-contract/master/samples/standalone/dsl/http-client/src/test/resources/application-test-repo.yaml[]
|
||||
stubrunner:
|
||||
ids: 'com.example:http-server-dsl:+:stubs:8080'
|
||||
repositoryRoot: https://repo.spring.io/libs-snapshot
|
||||
----
|
||||
|
||||
Now you can annotate your test class with `@AutoConfigureStubRunner`. In the annotation,
|
||||
@@ -550,7 +552,9 @@ Runner` properties, as shown in the following example:
|
||||
+
|
||||
[source,yaml,indent=0]
|
||||
----
|
||||
Unresolved directive in _verifier_how_it_works.adoc - include::https://raw.githubusercontent.com/spring-cloud/spring-cloud-contract/master/samples/standalone/dsl/http-client/src/test/resources/application-test-repo.yaml[]
|
||||
stubrunner:
|
||||
ids: 'com.example:http-server-dsl:+:stubs:8080'
|
||||
repositoryRoot: https://repo.spring.io/libs-snapshot
|
||||
----
|
||||
|
||||
Now you can annotate your test class with `@AutoConfigureStubRunner`. In the annotation,
|
||||
@@ -1079,7 +1083,9 @@ achieving the same thing by changing the properties.
|
||||
|
||||
[source,yaml,indent=0]
|
||||
----
|
||||
Unresolved directive in _verifier_how_it_works.adoc - include::https://raw.githubusercontent.com/spring-cloud/spring-cloud-contract/master/samples/standalone/dsl/http-client/src/test/resources/application-test-repo.yaml[]
|
||||
stubrunner:
|
||||
ids: 'com.example:http-server-dsl:+:stubs:8080'
|
||||
repositoryRoot: https://repo.spring.io/libs-snapshot
|
||||
----
|
||||
|
||||
That's it!
|
||||
@@ -1559,7 +1565,185 @@ You can read more about Spring Cloud Contract Verifier by reading the
|
||||
|
||||
== Contributing
|
||||
|
||||
Unresolved directive in README.adoc - include::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/docs/src/main/asciidoc/contributing.adoc[]
|
||||
:spring-cloud-build-branch: master
|
||||
|
||||
Spring Cloud is released under the non-restrictive Apache 2.0 license,
|
||||
and follows a very standard Github development process, using Github
|
||||
tracker for issues and merging pull requests into master. If you want
|
||||
to contribute even something trivial please do not hesitate, but
|
||||
follow the guidelines below.
|
||||
|
||||
=== Sign the Contributor License Agreement
|
||||
Before we accept a non-trivial patch or pull request we will need you to sign the
|
||||
https://cla.pivotal.io/sign/spring[Contributor License Agreement].
|
||||
Signing the contributor's agreement does not grant anyone commit rights to the main
|
||||
repository, but it does mean that we can accept your contributions, and you will get an
|
||||
author credit if we do. Active contributors might be asked to join the core team, and
|
||||
given the ability to merge pull requests.
|
||||
|
||||
=== Code of Conduct
|
||||
This project adheres to the Contributor Covenant https://github.com/spring-cloud/spring-cloud-build/blob/master/docs/src/main/asciidoc/code-of-conduct.adoc[code of
|
||||
conduct]. By participating, you are expected to uphold this code. Please report
|
||||
unacceptable behavior to spring-code-of-conduct@pivotal.io.
|
||||
|
||||
=== Code Conventions and Housekeeping
|
||||
None of these is essential for a pull request, but they will all help. They can also be
|
||||
added after the original pull request but before a merge.
|
||||
|
||||
* Use the Spring Framework code format conventions. If you use Eclipse
|
||||
you can import formatter settings using the
|
||||
`eclipse-code-formatter.xml` file from the
|
||||
https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-dependencies-parent/eclipse-code-formatter.xml[Spring
|
||||
Cloud Build] project. If using IntelliJ, you can use the
|
||||
https://plugins.jetbrains.com/plugin/6546[Eclipse Code Formatter
|
||||
Plugin] to import the same file.
|
||||
* Make sure all new `.java` files to have a simple Javadoc class comment with at least an
|
||||
`@author` tag identifying you, and preferably at least a paragraph on what the class is
|
||||
for.
|
||||
* Add the ASF license header comment to all new `.java` files (copy from existing files
|
||||
in the project)
|
||||
* Add yourself as an `@author` to the .java files that you modify substantially (more
|
||||
than cosmetic changes).
|
||||
* Add some Javadocs and, if you change the namespace, some XSD doc elements.
|
||||
* A few unit tests would help a lot as well -- someone has to do it.
|
||||
* If no-one else is using your branch, please rebase it against the current master (or
|
||||
other target branch in the main project).
|
||||
* When writing a commit message please follow https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html[these conventions],
|
||||
if you are fixing an existing issue please add `Fixes gh-XXXX` at the end of the commit
|
||||
message (where XXXX is the issue number).
|
||||
|
||||
=== Checkstyle
|
||||
|
||||
Spring Cloud Build comes with a set of checkstyle rules. You can find them in the `spring-cloud-build-tools` module. The most notable files under the module are:
|
||||
|
||||
.spring-cloud-build-tools/
|
||||
----
|
||||
└── src
|
||||
├── checkstyle
|
||||
│ └── checkstyle-suppressions.xml <3>
|
||||
└── main
|
||||
└── resources
|
||||
├── checkstyle-header.txt <2>
|
||||
└── checkstyle.xml <1>
|
||||
----
|
||||
<1> Default Checkstyle rules
|
||||
<2> File header setup
|
||||
<3> Default suppression rules
|
||||
|
||||
==== Checkstyle configuration
|
||||
|
||||
Checkstyle rules are *disabled by default*. To add checkstyle to your project just define the following properties and plugins.
|
||||
|
||||
.pom.xml
|
||||
----
|
||||
<properties>
|
||||
<maven-checkstyle-plugin.failsOnError>true</maven-checkstyle-plugin.failsOnError> <1>
|
||||
<maven-checkstyle-plugin.failsOnViolation>true
|
||||
</maven-checkstyle-plugin.failsOnViolation> <2>
|
||||
<maven-checkstyle-plugin.includeTestSourceDirectory>true
|
||||
</maven-checkstyle-plugin.includeTestSourceDirectory> <3>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin> <4>
|
||||
<groupId>io.spring.javaformat</groupId>
|
||||
<artifactId>spring-javaformat-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin> <5>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin> <5>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
</build>
|
||||
----
|
||||
<1> Fails the build upon Checkstyle errors
|
||||
<2> Fails the build upon Checkstyle violations
|
||||
<3> Checkstyle analyzes also the test sources
|
||||
<4> Add the Spring Java Format plugin that will reformat your code to pass most of the Checkstyle formatting rules
|
||||
<5> Add checkstyle plugin to your build and reporting phases
|
||||
|
||||
If you need to suppress some rules (e.g. line length needs to be longer), then it's enough for you to define a file under `${project.root}/src/checkstyle/checkstyle-suppressions.xml` with your suppressions. Example:
|
||||
|
||||
.projectRoot/src/checkstyle/checkstyle-suppresions.xml
|
||||
----
|
||||
<?xml version="1.0"?>
|
||||
<!DOCTYPE suppressions PUBLIC
|
||||
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
|
||||
"https://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
|
||||
<suppressions>
|
||||
<suppress files=".*ConfigServerApplication\.java" checks="HideUtilityClassConstructor"/>
|
||||
<suppress files=".*ConfigClientWatch\.java" checks="LineLengthCheck"/>
|
||||
</suppressions>
|
||||
----
|
||||
|
||||
It's advisable to copy the `${spring-cloud-build.rootFolder}/.editorconfig` and `${spring-cloud-build.rootFolder}/.springformat` to your project. That way, some default formatting rules will be applied. You can do so by running this script:
|
||||
|
||||
```bash
|
||||
$ curl https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/.editorconfig -o .editorconfig
|
||||
$ touch .springformat
|
||||
```
|
||||
|
||||
=== IDE setup
|
||||
|
||||
==== Intellij IDEA
|
||||
|
||||
In order to setup Intellij you should import our coding conventions, inspection profiles and set up the checkstyle plugin.
|
||||
The following files can be found in the https://github.com/spring-cloud/spring-cloud-build/tree/master/spring-cloud-build-tools[Spring Cloud Build] project.
|
||||
|
||||
.spring-cloud-build-tools/
|
||||
----
|
||||
└── src
|
||||
├── checkstyle
|
||||
│ └── checkstyle-suppressions.xml <3>
|
||||
└── main
|
||||
└── resources
|
||||
├── checkstyle-header.txt <2>
|
||||
├── checkstyle.xml <1>
|
||||
└── intellij
|
||||
├── Intellij_Project_Defaults.xml <4>
|
||||
└── Intellij_Spring_Boot_Java_Conventions.xml <5>
|
||||
----
|
||||
<1> Default Checkstyle rules
|
||||
<2> File header setup
|
||||
<3> Default suppression rules
|
||||
<4> Project defaults for Intellij that apply most of Checkstyle rules
|
||||
<5> Project style conventions for Intellij that apply most of Checkstyle rules
|
||||
|
||||
.Code style
|
||||
|
||||
image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/{spring-cloud-build-branch}/docs/src/main/asciidoc/images/intellij-code-style.png[Code style]
|
||||
|
||||
Go to `File` -> `Settings` -> `Editor` -> `Code style`. There click on the icon next to the `Scheme` section. There, click on the `Import Scheme` value and pick the `Intellij IDEA code style XML` option. Import the `spring-cloud-build-tools/src/main/resources/intellij/Intellij_Spring_Boot_Java_Conventions.xml` file.
|
||||
|
||||
.Inspection profiles
|
||||
|
||||
image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/{spring-cloud-build-branch}/docs/src/main/asciidoc/images/intellij-inspections.png[Code style]
|
||||
|
||||
Go to `File` -> `Settings` -> `Editor` -> `Inspections`. There click on the icon next to the `Profile` section. There, click on the `Import Profile` and import the `spring-cloud-build-tools/src/main/resources/intellij/Intellij_Project_Defaults.xml` file.
|
||||
|
||||
.Checkstyle
|
||||
|
||||
To have Intellij work with Checkstyle, you have to install the `Checkstyle` plugin. It's advisable to also install the `Assertions2Assertj` to automatically convert the JUnit assertions
|
||||
|
||||
image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/{spring-cloud-build-branch}/docs/src/main/asciidoc/images/intellij-checkstyle.png[Checkstyle]
|
||||
|
||||
Go to `File` -> `Settings` -> `Other settings` -> `Checkstyle`. There click on the `+` icon in the `Configuration file` section. There, you'll have to define where the checkstyle rules should be picked from. In the image above, we've picked the rules from the cloned Spring Cloud Build repository. However, you can point to the Spring Cloud Build's GitHub repository (e.g. for the `checkstyle.xml` : `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle.xml`). We need to provide the following variables:
|
||||
|
||||
- `checkstyle.header.file` - please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/main/resources/checkstyle/checkstyle-header.txt` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle-header.txt` URL.
|
||||
- `checkstyle.suppressions.file` - default suppressions. Please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` URL.
|
||||
- `checkstyle.additional.suppressions.file` - this variable corresponds to suppressions in your local project. E.g. you're working on `spring-cloud-contract`. Then point to the `project-root/src/checkstyle/checkstyle-suppressions.xml` folder. Example for `spring-cloud-contract` would be: `/home/username/spring-cloud-contract/src/checkstyle/checkstyle-suppressions.xml`.
|
||||
|
||||
IMPORTANT: Remember to set the `Scan Scope` to `All sources` since we apply checkstyle rules for production and test sources.
|
||||
|
||||
== How to build it
|
||||
|
||||
|
||||
25
docs/src/main/asciidoc/attributes.adoc
Normal file
25
docs/src/main/asciidoc/attributes.adoc
Normal file
@@ -0,0 +1,25 @@
|
||||
:doctype: book
|
||||
:idprefix:
|
||||
:idseparator: -
|
||||
:toc: left
|
||||
:toclevels: 4
|
||||
:tabsize: 4
|
||||
:numbered:
|
||||
:sectanchors:
|
||||
:sectnums:
|
||||
:icons: font
|
||||
:hide-uri-scheme:
|
||||
:docinfo: shared,private
|
||||
|
||||
:spring-cloud-contract-repo: snapshot
|
||||
:github-tag: master
|
||||
:spring-cloud-contract-docs-version: current
|
||||
:spring-cloud-contract-docs: https://docs.spring.io/spring-cloud-contract/docs/{spring-cloud-contract-docs-version}/reference
|
||||
:spring-cloud-contract-docs-current: https://docs.spring.io/spring-cloud-contract/docs/current/reference
|
||||
:github-repo: spring-cloud/spring-cloud-contract
|
||||
:github-raw: https://raw.github.com/{github-repo}/{github-tag}
|
||||
:github-code: https://github.com/{github-repo}/tree/{github-tag}
|
||||
:github-issues: https://github.com/{github-repo}/issues/
|
||||
:github-wiki: https://github.com/{github-repo}/wiki
|
||||
:github-master-code: https://github.com/{github-repo}/tree/master
|
||||
:sc-ext: java
|
||||
149
docs/src/main/asciidoc/documentation-overview.adoc
Normal file
149
docs/src/main/asciidoc/documentation-overview.adoc
Normal file
@@ -0,0 +1,149 @@
|
||||
[[contract-documentation]]
|
||||
= Spring Cloud Contract Documentation
|
||||
include::attributes.adoc[]
|
||||
|
||||
This section provides a brief overview of Spring Cloud Contract reference documentation. It serves
|
||||
as a map for the rest of the document.
|
||||
|
||||
|
||||
|
||||
[[contract-documentation-about]]
|
||||
== About the Documentation
|
||||
The Spring Cloud Contract reference guide is available as
|
||||
|
||||
* {spring-cloud-contract-docs}/html[Multi-page HTML]
|
||||
* {spring-cloud-contract-docs}/htmlsingle[Single page HTML]
|
||||
* {spring-cloud-contract-docs}/pdf/spring-cloud-contract-reference.pdf[PDF]
|
||||
|
||||
The latest copy is available at {spring-cloud-contract-docs-current}.
|
||||
|
||||
Copies of this document may be made for your own use and for distribution to others,
|
||||
provided that you do not charge any fee for such copies and further provided that each
|
||||
copy contains this Copyright Notice, whether distributed in print or electronically.
|
||||
|
||||
|
||||
|
||||
[[contract-documentation-getting-help]]
|
||||
== Getting Help
|
||||
If you have trouble with Spring Cloud Contract, we would like to help.
|
||||
|
||||
* Try the <<howto.adoc#howto, How-to documents>>. They provide solutions to the most
|
||||
common questions.
|
||||
* Learn the Spring Cloud Contract basics. If you are
|
||||
starting out with Spring Cloud Contract, try one of the https://spring.io/guides[guides].
|
||||
* Ask a question. We monitor https://stackoverflow.com[stackoverflow.com] for questions
|
||||
tagged with https://stackoverflow.com/tags/spring-cloud-contract[`spring-cloud-contract`].
|
||||
* Report bugs with Spring Cloud Contract at https://github.com/spring-cloud/spring-cloud-contract/issues.
|
||||
|
||||
NOTE: All of Spring Cloud Contract is open source, including the documentation. If you find
|
||||
problems with the docs or if you want to improve them, please {github-code}[get
|
||||
involved].
|
||||
|
||||
|
||||
|
||||
[[contract-documentation-first-steps]]
|
||||
== First Steps
|
||||
If you are getting started with Spring Cloud Contract or 'Spring' in general, start with
|
||||
<<getting-started.adoc#getting-started, the following topics>>:
|
||||
|
||||
* *From scratch:*
|
||||
<<getting-started.adoc#getting-started-introducing-spring-cloud-contract, Overview>> |
|
||||
<<getting-started.adoc#getting-started-system-requirements, Requirements>> |
|
||||
<<getting-started.adoc#getting-started-installing-spring-cloud-contract, Installation>>
|
||||
* *Tutorial:*
|
||||
<<getting-started.adoc#getting-started-first-application, Part 1>> |
|
||||
<<getting-started.adoc#getting-started-first-application-code, Part 2>>
|
||||
* *Running your example:*
|
||||
<<getting-started.adoc#getting-started-first-application-run, Part 1>> |
|
||||
<<getting-started.adoc#getting-started-first-application-executable-jar, Part 2>>
|
||||
|
||||
|
||||
|
||||
== Working with Spring Cloud Contract
|
||||
Ready to actually start using Spring Cloud Contract? <<using-spring-cloud-contract.adoc#using-boot, We have
|
||||
you covered>>:
|
||||
|
||||
* *Build systems:*
|
||||
<<using-spring-cloud-contract.adoc#using-contract-maven, Maven>> |
|
||||
<<using-spring-cloud-contract.adoc#using-contract-gradle, Gradle>> |
|
||||
<<using-spring-cloud-contract.adoc#using-contract-ant, Ant>> |
|
||||
<<using-spring-cloud-contract.adoc#using-contract-starter, Starters>>
|
||||
* *Best practices:*
|
||||
<<using-spring-cloud-contract.adoc#using-contract-structuring-your-code, Code Structure>> |
|
||||
<<using-spring-cloud-contract.adoc#using-contract-configuration-classes, @Configuration>> |
|
||||
<<using-spring-cloud-contract.adoc#using-contract-auto-configuration, @EnableAutoConfiguration>> |
|
||||
<<using-spring-cloud-contract.adoc#using-contract-spring-beans-and-dependency-injection, Beans and
|
||||
Dependency Injection>>
|
||||
* *Running your code:*
|
||||
<<using-spring-cloud-contract.adoc#using-contract-running-from-an-ide, IDE>> |
|
||||
<<using-spring-cloud-contract.adoc#using-contract-running-as-a-packaged-application, Packaged>> |
|
||||
<<using-spring-cloud-contract.adoc#using-contract-running-with-the-maven-plugin, Maven>> |
|
||||
<<using-spring-cloud-contract.adoc#using-contract-running-with-the-gradle-plugin, Gradle>>
|
||||
* *Packaging your app:*
|
||||
<<using-spring-cloud-contract.adoc#using-contract-packaging-for-production, Production jars>>
|
||||
* *Spring Cloud Contract CLI:*
|
||||
<<spring-cloud-contract-cli.adoc#cli, Using the CLI>>
|
||||
|
||||
|
||||
|
||||
== Learning about Spring Cloud Contract Features
|
||||
Need more details about Spring Cloud Contract's core features?
|
||||
<<spring-cloud-contract-features.adoc#contract-features, The following content is for you>>:
|
||||
|
||||
* *Core Features:*
|
||||
<<spring-cloud-contract-features.adoc#contract-features-spring-application, SpringApplication>> |
|
||||
<<spring-cloud-contract-features.adoc#contract-features-external-config, External Configuration>> |
|
||||
<<spring-cloud-contract-features.adoc#contract-features-profiles, Profiles>> |
|
||||
<<spring-cloud-contract-features.adoc#contract-features-logging, Logging>>
|
||||
* *Web Applications:*
|
||||
<<spring-cloud-contract-features.adoc#contract-features-spring-mvc, MVC>> |
|
||||
<<spring-cloud-contract-features.adoc#contract-features-embedded-container, Embedded Containers>>
|
||||
* *Working with data:*
|
||||
<<spring-cloud-contract-features.adoc#contract-features-sql, SQL>> |
|
||||
<<spring-cloud-contract-features.adoc#contract-features-nosql, NO-SQL>>
|
||||
* *Messaging:*
|
||||
<<spring-cloud-contract-features.adoc#contract-features-messaging, Overview>> |
|
||||
<<spring-cloud-contract-features.adoc#contract-features-jms, JMS>>
|
||||
* *Testing:*
|
||||
<<spring-cloud-contract-features.adoc#contract-features-testing, Overview>> |
|
||||
<<spring-cloud-contract-features.adoc#contract-features-testing-spring-cloud-contract-applications, Boot
|
||||
Applications>> |
|
||||
<<spring-cloud-contract-features.adoc#contract-features-test-utilities, Utils>>
|
||||
* *Extending:*
|
||||
<<spring-cloud-contract-features.adoc#contract-features-developing-auto-configuration, Auto-configuration>> |
|
||||
<<spring-cloud-contract-features.adoc#contract-features-condition-annotations, @Conditions>>
|
||||
|
||||
|
||||
|
||||
== Moving to Production
|
||||
When you are ready to push your Spring Cloud Contract application to production, we have
|
||||
<<production-ready-features.adoc#production-ready, some tricks>> that you might like:
|
||||
|
||||
* *Management endpoints:*
|
||||
<<production-ready-features.adoc#production-ready-endpoints, Overview>> |
|
||||
<<production-ready-features.adoc#production-ready-customizing-endpoints, Customization>>
|
||||
* *Connection options:*
|
||||
<<production-ready-features.adoc#production-ready-monitoring, HTTP>> |
|
||||
<<production-ready-features.adoc#production-ready-jmx, JMX>>
|
||||
* *Monitoring:*
|
||||
<<production-ready-features.adoc#production-ready-metrics, Metrics>> |
|
||||
<<production-ready-features.adoc#production-ready-auditing, Auditing>> |
|
||||
<<production-ready-features.adoc#production-ready-tracing, Tracing>> |
|
||||
<<production-ready-features.adoc#production-ready-process-monitoring, Process>>
|
||||
|
||||
|
||||
|
||||
== Advanced Topics
|
||||
Finally, we have a few topics for more advanced users:
|
||||
|
||||
* *Spring Cloud Contract Applications Deployment:*
|
||||
<<deployment.adoc#cloud-deployment, Cloud Deployment>> |
|
||||
<<deployment.adoc#deployment-service, OS Service>>
|
||||
* *Build tool plugins:*
|
||||
<<build-tool-plugins.adoc#build-tool-plugins-maven-plugin, Maven>> |
|
||||
<<build-tool-plugins.adoc#build-tool-plugins-gradle-plugin, Gradle>>
|
||||
* *Appendix:*
|
||||
<<appendix.adoc#common-application-properties, Application Properties>> |
|
||||
<<appendix.adoc#auto-configuration-classes, Auto-configuration classes>> |
|
||||
<<appendix.adoc#executable-jar, Executable Jars>>
|
||||
|
||||
821
docs/src/main/asciidoc/getting-started.adoc
Normal file
821
docs/src/main/asciidoc/getting-started.adoc
Normal file
@@ -0,0 +1,821 @@
|
||||
[[getting-started]]
|
||||
= Getting Started
|
||||
include::attributes.adoc[]
|
||||
|
||||
If you are getting started with Spring Boot, or "`Spring`" in general, start by reading
|
||||
this section. It answers the basic "`what?`", "`how?`" and "`why?`" questions. It
|
||||
includes an introduction to Spring Boot, along with installation instructions. We then
|
||||
walk you through building your first Spring Boot application, discussing some core
|
||||
principles as we go.
|
||||
|
||||
|
||||
|
||||
[[getting-started-introducing-spring-boot]]
|
||||
== Introducing Spring Boot
|
||||
Spring Boot makes it easy to create stand-alone, production-grade Spring-based
|
||||
Applications that you can run. We take an opinionated view of the Spring platform and
|
||||
third-party libraries, so that you can get started with minimum fuss. Most Spring Boot
|
||||
applications need very little Spring configuration.
|
||||
|
||||
You can use Spring Boot to create Java applications that can be started by using
|
||||
`java -jar` or more traditional war deployments. We also provide a command line tool that
|
||||
runs "`spring scripts`".
|
||||
|
||||
Our primary goals are:
|
||||
|
||||
* Provide a radically faster and widely accessible getting-started experience for all
|
||||
Spring development.
|
||||
* Be opinionated out of the box but get out of the way quickly as requirements start to
|
||||
diverge from the defaults.
|
||||
* 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).
|
||||
* Absolutely no code generation and no requirement for XML configuration.
|
||||
|
||||
|
||||
|
||||
[[getting-started-system-requirements]]
|
||||
== System Requirements
|
||||
Spring Boot {spring-boot-version} requires https://www.java.com[Java 8] and is compatible
|
||||
up to Java 12 (included). {spring-reference}[Spring Framework {spring-framework-version}]
|
||||
or above is also required.
|
||||
|
||||
Explicit build support is provided for the following build tools:
|
||||
|
||||
|===
|
||||
|Build Tool |Version
|
||||
|
||||
|Maven
|
||||
|3.3+
|
||||
|
||||
|Gradle
|
||||
|5.x (4.10 is also supported but in a deprecated form)
|
||||
|===
|
||||
|
||||
|
||||
|
||||
[[getting-started-system-requirements-servlet-containers]]
|
||||
=== Servlet Containers
|
||||
Spring Boot supports the following embedded servlet containers:
|
||||
|
||||
|===
|
||||
|Name |Servlet Version
|
||||
|
||||
|Tomcat 9.0
|
||||
|4.0
|
||||
|
||||
|Jetty 9.4
|
||||
|3.1
|
||||
|
||||
|Undertow 2.0
|
||||
|4.0
|
||||
|===
|
||||
|
||||
You can also deploy Spring Boot applications to any Servlet 3.1+ compatible container.
|
||||
|
||||
|
||||
|
||||
[[getting-started-installing-spring-boot]]
|
||||
== Installing Spring Boot
|
||||
Spring Boot can be used with "`classic`" Java development tools or installed as a command
|
||||
line tool. Either way, you need https://www.java.com[Java SDK v1.8] or higher. Before you
|
||||
begin, you should check your current Java installation by using the following command:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
$ java -version
|
||||
----
|
||||
|
||||
If you are new to Java development or if you want to experiment with Spring Boot, you
|
||||
might want to try the <<getting-started-installing-the-cli, Spring Boot CLI>> (Command
|
||||
Line Interface) first. Otherwise, read on for "`classic`" installation instructions.
|
||||
|
||||
|
||||
|
||||
[[getting-started-installation-instructions-for-java]]
|
||||
=== Installation Instructions for the Java Developer
|
||||
You can use Spring Boot in the same way as any standard Java library. To do so, include
|
||||
the appropriate `+spring-boot-*.jar+` files on your classpath. Spring Boot does not
|
||||
require any special tools integration, so you can use any IDE or text editor. Also, 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.
|
||||
|
||||
Although you _could_ copy Spring Boot jars, we generally recommend that you use a build
|
||||
tool that supports dependency management (such as Maven or Gradle).
|
||||
|
||||
|
||||
|
||||
[[getting-started-maven-installation]]
|
||||
==== Maven Installation
|
||||
Spring Boot is compatible with Apache Maven 3.3 or above. If you do not already have
|
||||
Maven installed, you can follow the instructions at https://maven.apache.org.
|
||||
|
||||
TIP: On many operating systems, Maven can be installed with a package manager. If you use
|
||||
OSX Homebrew, try `brew install maven`. Ubuntu users can run
|
||||
`sudo apt-get install maven`. Windows users with https://chocolatey.org/[Chocolatey] can
|
||||
run `choco install maven` from an elevated (administrator) prompt.
|
||||
|
||||
Spring Boot dependencies use the `org.springframework.boot` `groupId`. Typically, your
|
||||
Maven POM file inherits from the `spring-boot-starter-parent` project and declares
|
||||
dependencies to one or more <<using-spring-boot.adoc#using-boot-starter,"`Starters`">>.
|
||||
Spring Boot also provides an optional
|
||||
<<build-tool-plugins.adoc#build-tool-plugins-maven-plugin, Maven plugin>> to create
|
||||
executable jars.
|
||||
|
||||
The following listing shows a typical `pom.xml` file:
|
||||
|
||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
<?xml version="1.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>myproject</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<!-- Inherit defaults from Spring Boot -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>{spring-boot-version}</version>
|
||||
</parent>
|
||||
|
||||
<!-- Add typical dependencies for a web application -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<!-- Package as an executable jar -->
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
ifeval::["{spring-boot-repo}" != "release"]
|
||||
<!-- Add Spring repositories -->
|
||||
<!-- (you don't need this if you are using a .RELEASE version) -->
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots><enabled>true</enabled></snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
endif::[]
|
||||
</project>
|
||||
----
|
||||
|
||||
TIP: The `spring-boot-starter-parent` is a great way to use Spring Boot, but it might not
|
||||
be suitable all of the time. Sometimes you may need to inherit from a different parent
|
||||
POM, or you might not like our default settings. In those cases, see
|
||||
<<using-boot-maven-without-a-parent>> for an alternative solution that uses an `import`
|
||||
scope.
|
||||
|
||||
|
||||
|
||||
[[getting-started-gradle-installation]]
|
||||
==== Gradle Installation
|
||||
Spring Boot is compatible with 5.x. 4.10 is also supported but this support is deprecated
|
||||
and will be removed in a future release. If you do not already have Gradle installed, you
|
||||
can follow the instructions at https://gradle.org.
|
||||
|
||||
Spring Boot dependencies can be declared by using the `org.springframework.boot` `group`.
|
||||
Typically, your project declares dependencies to one or more
|
||||
<<using-spring-boot.adoc#using-boot-starter, "`Starters`">>. Spring Boot
|
||||
provides a useful <<build-tool-plugins.adoc#build-tool-plugins-gradle-plugin, Gradle
|
||||
plugin>> that can be used to simplify dependency declarations and to create executable
|
||||
jars.
|
||||
|
||||
.Gradle Wrapper
|
||||
****
|
||||
The Gradle Wrapper provides a nice way of "`obtaining`" Gradle when you need to build a
|
||||
project. It is a small script and library that you commit alongside your code to
|
||||
bootstrap the build process. See {gradle-user-guide}/gradle_wrapper.html for details.
|
||||
****
|
||||
|
||||
More details on getting started with Spring Boot and Gradle can be found in the
|
||||
{spring-boot-gradle-plugin-reference}/#getting-started[Getting Started section] of the
|
||||
Gradle plugin's reference guide.
|
||||
|
||||
|
||||
|
||||
[[getting-started-installing-the-cli]]
|
||||
=== Installing the Spring Boot CLI
|
||||
The Spring Boot CLI (Command Line Interface) is a command line tool that you can use to
|
||||
quickly prototype with Spring. It lets you run http://groovy-lang.org/[Groovy] scripts,
|
||||
which means that you have a familiar Java-like syntax without so much boilerplate code.
|
||||
|
||||
You do not need to use the CLI to work with Spring Boot, but it is definitely the
|
||||
quickest way to get a Spring application off the ground.
|
||||
|
||||
|
||||
|
||||
[[getting-started-manual-cli-installation]]
|
||||
==== Manual Installation
|
||||
You can download the Spring CLI distribution from the Spring software repository:
|
||||
|
||||
* https://repo.spring.io/{spring-boot-repo}/org/springframework/boot/spring-boot-cli/{spring-boot-version}/spring-boot-cli-{spring-boot-version}-bin.zip[spring-boot-cli-{spring-boot-version}-bin.zip]
|
||||
* https://repo.spring.io/{spring-boot-repo}/org/springframework/boot/spring-boot-cli/{spring-boot-version}/spring-boot-cli-{spring-boot-version}-bin.tar.gz[spring-boot-cli-{spring-boot-version}-bin.tar.gz]
|
||||
|
||||
Cutting edge
|
||||
https://repo.spring.io/snapshot/org/springframework/boot/spring-boot-cli/[snapshot
|
||||
distributions] are also available.
|
||||
|
||||
Once downloaded, follow the
|
||||
{github-raw}/spring-boot-project/spring-boot-cli/src/main/content/INSTALL.txt[INSTALL.txt]
|
||||
instructions from the unpacked archive. In summary, there is a `spring` script
|
||||
(`spring.bat` for Windows) in a `bin/` directory in the `.zip` file. Alternatively, you
|
||||
can use `java -jar` with the `.jar` file (the script helps you to be sure that the
|
||||
classpath is set correctly).
|
||||
|
||||
|
||||
|
||||
[[getting-started-sdkman-cli-installation]]
|
||||
==== Installation with SDKMAN!
|
||||
SDKMAN! (The Software Development Kit Manager) can be used for managing multiple versions
|
||||
of various binary SDKs, including Groovy and the Spring Boot CLI.
|
||||
Get SDKMAN! from https://sdkman.io and install Spring Boot by using the following
|
||||
commands:
|
||||
|
||||
[indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
$ sdk install springboot
|
||||
$ spring --version
|
||||
Spring Boot v{spring-boot-version}
|
||||
----
|
||||
|
||||
If you develop features for the CLI and want easy access to the version you built,
|
||||
use the following commands:
|
||||
|
||||
[indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
$ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-{spring-boot-version}-bin/spring-{spring-boot-version}/
|
||||
$ sdk default springboot dev
|
||||
$ spring --version
|
||||
Spring CLI v{spring-boot-version}
|
||||
----
|
||||
|
||||
The preceding instructions install a local instance of `spring` called the `dev`
|
||||
instance. It points at your target build location, so every time you rebuild Spring Boot,
|
||||
`spring` is up-to-date.
|
||||
|
||||
You can see it by running the following command:
|
||||
|
||||
[indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
$ sdk ls springboot
|
||||
|
||||
================================================================================
|
||||
Available Springboot Versions
|
||||
================================================================================
|
||||
> + dev
|
||||
* {spring-boot-version}
|
||||
|
||||
================================================================================
|
||||
+ - local version
|
||||
* - installed
|
||||
> - currently in use
|
||||
================================================================================
|
||||
----
|
||||
|
||||
|
||||
|
||||
[[getting-started-homebrew-cli-installation]]
|
||||
==== OSX Homebrew Installation
|
||||
If you are on a Mac and use https://brew.sh/[Homebrew], you can install the Spring Boot
|
||||
CLI by using the following commands:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
$ brew tap pivotal/tap
|
||||
$ brew install springboot
|
||||
----
|
||||
|
||||
Homebrew installs `spring` to `/usr/local/bin`.
|
||||
|
||||
NOTE: If you do not see the formula, your installation of brew might be out-of-date. In
|
||||
that case, run `brew update` and try again.
|
||||
|
||||
|
||||
|
||||
[[getting-started-macports-cli-installation]]
|
||||
==== MacPorts Installation
|
||||
If you are on a Mac and use https://www.macports.org/[MacPorts], you can install the
|
||||
Spring Boot CLI by using the following command:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
$ sudo port install spring-boot-cli
|
||||
----
|
||||
|
||||
|
||||
|
||||
[[getting-started-cli-command-line-completion]]
|
||||
==== Command-line Completion
|
||||
The Spring Boot CLI includes scripts that provide command completion for the
|
||||
https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29[BASH] and
|
||||
https://en.wikipedia.org/wiki/Z_shell[zsh] shells. You can `source` the script (also named
|
||||
`spring`) in any shell or put it in your personal or system-wide bash completion
|
||||
initialization. On a Debian system, the system-wide scripts are in
|
||||
`/shell-completion/bash` and all scripts in that directory are executed when a new shell
|
||||
starts. For example, to run the script manually if you have installed by using SDKMAN!,
|
||||
use the following commands:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring
|
||||
$ spring <HIT TAB HERE>
|
||||
grab help jar run test version
|
||||
----
|
||||
|
||||
NOTE: If you install the Spring Boot CLI by using Homebrew or MacPorts, the command-line
|
||||
completion scripts are automatically registered with your shell.
|
||||
|
||||
|
||||
|
||||
[[getting-started-scoop-cli-installation]]
|
||||
==== Windows Scoop Installation
|
||||
If you are on a Windows and use https://scoop.sh/[Scoop], you can install the Spring Boot
|
||||
CLI by using the following commands:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
> scoop bucket add extras
|
||||
> scoop install springboot
|
||||
----
|
||||
|
||||
Scoop installs `spring` to `~/scoop/apps/springboot/current/bin`.
|
||||
|
||||
NOTE: If you do not see the app manifest, your installation of scoop might be out-of-date.
|
||||
In that case, run `scoop update` and try again.
|
||||
|
||||
|
||||
|
||||
[[getting-started-cli-example]]
|
||||
==== Quick-start Spring CLI Example
|
||||
You can use the following web application to test your installation. To start, create a
|
||||
file called `app.groovy`, as follows:
|
||||
|
||||
[source,groovy,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
@RestController
|
||||
class ThisWillActuallyRun {
|
||||
|
||||
@RequestMapping("/")
|
||||
String home() {
|
||||
"Hello World!"
|
||||
}
|
||||
|
||||
}
|
||||
----
|
||||
|
||||
Then run it from a shell, as follows:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
$ spring run app.groovy
|
||||
----
|
||||
|
||||
NOTE: The first run of your application is slow, as dependencies are downloaded.
|
||||
Subsequent runs are much quicker.
|
||||
|
||||
Open `http://localhost:8080` in your favorite web browser. You should see the following
|
||||
output:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
Hello World!
|
||||
----
|
||||
|
||||
|
||||
|
||||
[[getting-started-upgrading-from-an-earlier-version]]
|
||||
=== Upgrading from an Earlier Version of Spring Boot
|
||||
If you are upgrading from an earlier release of Spring Boot, check the
|
||||
{github-wiki}/Spring-Boot-2.0-Migration-Guide["`migration guide`" on the project wiki]
|
||||
that provides detailed upgrade instructions. Check also the
|
||||
{github-wiki}["`release notes`"] for a list of "`new and noteworthy`" features for each
|
||||
release.
|
||||
|
||||
When upgrading to a new feature release, some properties may have been renamed or removed.
|
||||
Spring Boot provides a way to analyze your application's environment and print diagnostics
|
||||
at startup, but also temporarily migrate properties at runtime for you. To enable that
|
||||
feature, add the following dependency to your project:
|
||||
|
||||
[source,xml,indent=0]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-properties-migrator</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
WARNING: Properties that are added late to the environment, such as when using
|
||||
`@PropertySource`, will not be taken into account.
|
||||
|
||||
NOTE: Once you're done with the migration, please make sure to remove this module from
|
||||
your project's dependencies.
|
||||
|
||||
To upgrade an existing CLI installation, use the appropriate package manager command (for
|
||||
example, `brew upgrade`) or, if you manually installed the CLI, follow the
|
||||
<<getting-started-manual-cli-installation, standard instructions>>, remembering to update
|
||||
your `PATH` environment variable to remove any older references.
|
||||
|
||||
|
||||
|
||||
[[getting-started-first-application]]
|
||||
== Developing Your First Spring Boot Application
|
||||
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.
|
||||
|
||||
[TIP]
|
||||
====
|
||||
The https://spring.io[spring.io] web site contains many "`Getting Started`"
|
||||
https://spring.io/guides[guides] that use Spring Boot. If you need to solve a specific
|
||||
problem, check there first.
|
||||
|
||||
You can shortcut the steps below by going to https://start.spring.io and choosing the
|
||||
"Web" starter from the dependencies searcher. Doing so generates a new project structure
|
||||
so that you can <<getting-started-first-application-code,start coding right away>>. Check
|
||||
the {spring-initializr-reference}/#user-guide[Spring Initializr documentation] for more
|
||||
details.
|
||||
====
|
||||
|
||||
Before we begin, open a terminal and run the following commands to ensure that you have
|
||||
valid versions of Java and Maven installed:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
$ 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)
|
||||
----
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
$ 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
|
||||
----
|
||||
|
||||
NOTE: This sample needs to be created in its own folder. Subsequent instructions assume
|
||||
that you have created a suitable folder and that it is your current directory.
|
||||
|
||||
|
||||
|
||||
[[getting-started-first-application-pom]]
|
||||
=== Creating the POM
|
||||
We need to start by creating a Maven `pom.xml` file. The `pom.xml` is the recipe that is
|
||||
used to build your project. Open your favorite text editor and add the following:
|
||||
|
||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
<?xml version="1.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>myproject</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>{spring-boot-version}</version>
|
||||
</parent>
|
||||
|
||||
<!-- Additional lines to be added here... -->
|
||||
|
||||
ifeval::["{spring-boot-repo}" != "release"]
|
||||
<!-- (you don't need this if you are using a .RELEASE version) -->
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots><enabled>true</enabled></snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
endif::[]
|
||||
</project>
|
||||
----
|
||||
|
||||
The preceding listing should give you a working build. You can test it by running `mvn
|
||||
package` (for now, you can ignore the "`jar will be empty - no content was marked for
|
||||
inclusion!`" warning).
|
||||
|
||||
NOTE: At this point, you could import the project into an IDE (most modern Java IDEs
|
||||
include built-in support for Maven). For simplicity, we continue to use a plain text
|
||||
editor for this example.
|
||||
|
||||
|
||||
|
||||
[[getting-started-first-application-dependencies]]
|
||||
=== Adding Classpath Dependencies
|
||||
Spring Boot provides a number of "`Starters`" that let you add jars to your classpath.
|
||||
Our applications for smoke tests use the `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
|
||||
<<using-spring-boot.adoc#using-boot-dependency-management,`dependency-management`>>
|
||||
section 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:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
$ 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 `parent` section:
|
||||
|
||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
----
|
||||
|
||||
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.
|
||||
|
||||
|
||||
|
||||
[[getting-started-first-application-code]]
|
||||
=== Writing the Code
|
||||
To finish our application, we need to create a single Java file. By default, Maven
|
||||
compiles sources from `src/main/java`, so you need to create that folder structure and
|
||||
then add a file named `src/main/java/Example.java` to contain the following code:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
import org.springframework.boot.*;
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@EnableAutoConfiguration
|
||||
public class Example {
|
||||
|
||||
@RequestMapping("/")
|
||||
String home() {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Example.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
----
|
||||
|
||||
Although there is not much code here, quite a lot is going on. We step through the
|
||||
important parts in the next few sections.
|
||||
|
||||
|
||||
|
||||
[[getting-started-first-application-annotations]]
|
||||
==== The `@RestController` and `@RequestMapping` Annotations
|
||||
The first annotation on our `Example` 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.
|
||||
|
||||
TIP: The `@RestController` and `@RequestMapping` annotations are Spring MVC annotations.
|
||||
(They are not specific to Spring Boot.) See the {spring-reference}web.html#mvc[MVC
|
||||
section] in the Spring Reference Documentation for more details.
|
||||
|
||||
|
||||
|
||||
[[getting-started-first-application-auto-configuration]]
|
||||
==== The @EnableAutoConfiguration Annotation
|
||||
The second class-level annotation is `@EnableAutoConfiguration`. This annotation tells
|
||||
Spring Boot to "`guess`" how you want to configure Spring, based on the jar dependencies
|
||||
that you have added. Since `spring-boot-starter-web` added Tomcat and Spring MVC, the
|
||||
auto-configuration assumes that you are developing a web application and sets up Spring
|
||||
accordingly.
|
||||
|
||||
.Starters and Auto-configuration
|
||||
****
|
||||
Auto-configuration is designed to work well with "`Starters`", but the two concepts are
|
||||
not directly tied. You are free to pick and choose jar dependencies outside of the
|
||||
starters. Spring Boot still does its best to auto-configure your application.
|
||||
****
|
||||
|
||||
|
||||
|
||||
[[getting-started-first-application-main-method]]
|
||||
==== The "`main`" Method
|
||||
The final part of our application is the `main` method. This is just a standard method
|
||||
that follows the Java convention for an application entry point. Our main method
|
||||
delegates to Spring Boot's `SpringApplication` class by calling `run`.
|
||||
`SpringApplication` bootstraps our application, starting Spring, which, in turn, starts
|
||||
the auto-configured Tomcat web server. We need to pass `Example.class` as an argument to
|
||||
the `run` method to tell `SpringApplication` which is the primary Spring component. The
|
||||
`args` array is also passed through to expose any command-line arguments.
|
||||
|
||||
|
||||
|
||||
[[getting-started-first-application-run]]
|
||||
=== Running the Example
|
||||
At this point, your application should work. Since you used the
|
||||
`spring-boot-starter-parent` POM, you have a useful `run` goal that you can use to start
|
||||
the application. Type `mvn spring-boot:run` from the root project directory to start the
|
||||
application. You should see output similar to the following:
|
||||
|
||||
[indent=0,subs="attributes"]
|
||||
----
|
||||
$ mvn spring-boot:run
|
||||
|
||||
. ____ _ __ _ _
|
||||
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
|
||||
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
|
||||
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
|
||||
' |____| .__|_| |_|_| |_\__, | / / / /
|
||||
=========|_|==============|___/=/_/_/_/
|
||||
:: Spring Boot :: (v{spring-boot-version})
|
||||
....... . . .
|
||||
....... . . . (log output here)
|
||||
....... . . .
|
||||
........ Started Example in 2.222 seconds (JVM running for 6.514)
|
||||
----
|
||||
|
||||
If you open a web browser to `http://localhost:8080`, you should see the following output:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
Hello World!
|
||||
----
|
||||
|
||||
To gracefully exit the application, press `ctrl-c`.
|
||||
|
||||
|
||||
|
||||
[[getting-started-first-application-executable-jar]]
|
||||
=== Creating an Executable Jar
|
||||
We finish our example by creating a completely self-contained executable jar file that
|
||||
we could run in production. Executable jars (sometimes called "`fat jars`") are archives
|
||||
containing your compiled classes along with all of the jar dependencies that your code
|
||||
needs to run.
|
||||
|
||||
.Executable jars and Java
|
||||
****
|
||||
Java does not provide a standard way to load nested jar files (jar files that are
|
||||
themselves contained within a jar). This can be problematic if you are looking to
|
||||
distribute a self-contained application.
|
||||
|
||||
To solve this problem, many developers use "`uber`" jars. An uber jar packages all the
|
||||
classes from all the application's dependencies into a single archive. The problem with
|
||||
this approach is that it becomes hard to see which libraries are in your application. It
|
||||
can also be problematic if the same filename is used (but with different content) in
|
||||
multiple jars.
|
||||
|
||||
Spring Boot takes a <<appendix.adoc#executable-jar, different approach>> and lets you
|
||||
actually nest jars directly.
|
||||
****
|
||||
|
||||
To create an executable jar, we need to add the `spring-boot-maven-plugin` to our
|
||||
`pom.xml`. To do so, insert the following lines just below the `dependencies` section:
|
||||
|
||||
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
----
|
||||
|
||||
NOTE: The `spring-boot-starter-parent` POM includes `<executions>` configuration to bind
|
||||
the `repackage` goal. If you do not use the parent POM, you need to declare this
|
||||
configuration yourself. See the {spring-boot-maven-plugin-site}/usage.html[plugin
|
||||
documentation] for details.
|
||||
|
||||
Save your `pom.xml` and run `mvn package` from the command line, as follows:
|
||||
|
||||
[indent=0,subs="attributes"]
|
||||
----
|
||||
$ mvn package
|
||||
|
||||
[INFO] Scanning for projects...
|
||||
[INFO]
|
||||
[INFO] ------------------------------------------------------------------------
|
||||
[INFO] Building myproject 0.0.1-SNAPSHOT
|
||||
[INFO] ------------------------------------------------------------------------
|
||||
[INFO] .... ..
|
||||
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
|
||||
[INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar
|
||||
[INFO]
|
||||
[INFO] --- spring-boot-maven-plugin:{spring-boot-version}:repackage (default) @ myproject ---
|
||||
[INFO] ------------------------------------------------------------------------
|
||||
[INFO] BUILD SUCCESS
|
||||
[INFO] ------------------------------------------------------------------------
|
||||
----
|
||||
|
||||
If you look in the `target` directory, you should see `myproject-0.0.1-SNAPSHOT.jar`. The
|
||||
file should be around 10 MB in size. If you want to peek inside, you can use `jar tvf`,
|
||||
as follows:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
$ jar tvf target/myproject-0.0.1-SNAPSHOT.jar
|
||||
----
|
||||
|
||||
You should also see 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 Spring Boot.
|
||||
|
||||
To run that application, use the `java -jar` command, as follows:
|
||||
|
||||
[indent=0,subs="attributes"]
|
||||
----
|
||||
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar
|
||||
|
||||
. ____ _ __ _ _
|
||||
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
|
||||
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
|
||||
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
|
||||
' |____| .__|_| |_|_| |_\__, | / / / /
|
||||
=========|_|==============|___/=/_/_/_/
|
||||
:: Spring Boot :: (v{spring-boot-version})
|
||||
....... . . .
|
||||
....... . . . (log output here)
|
||||
....... . . .
|
||||
........ Started Example in 2.536 seconds (JVM running for 2.864)
|
||||
----
|
||||
|
||||
As before, to exit the application, press `ctrl-c`.
|
||||
|
||||
|
||||
|
||||
[[getting-started-whats-next]]
|
||||
== What to Read Next
|
||||
Hopefully, this section provided some of the Spring Boot basics and got you on your way
|
||||
to writing your own applications. If you are a task-oriented type of developer, you might
|
||||
want to jump over to https://spring.io and check out some of the
|
||||
https://spring.io/guides/[getting started] guides that solve specific "`How do I do that
|
||||
with Spring?`" problems. We also have Spring Boot-specific
|
||||
"`<<howto.adoc#howto, How-to>>`" reference documentation.
|
||||
|
||||
Otherwise, the next logical step is to read _<<using-spring-boot.adoc#using-boot>>_. If
|
||||
you are really impatient, you could also jump ahead and read about
|
||||
_<<spring-boot-features.adoc#boot-features, Spring Boot features>>_.
|
||||
17
docs/src/main/asciidoc/index.htmladoc
Normal file
17
docs/src/main/asciidoc/index.htmladoc
Normal file
@@ -0,0 +1,17 @@
|
||||
[[spring-cloud-contract-reference-documentation]]
|
||||
= Spring Cloud Contract Reference Documentation
|
||||
Adam Dudczak, Mathias Düsterhöft, Marcin Grzejszczak, Dennis Kieselhorst, Jakub Kubryński, Karol Lassak, Olga Maciaszek-Sharma, Mariusz Smykuła, Dave Syer, Jay Bryant
|
||||
:docinfo: shared
|
||||
|
||||
The reference documentation consists of the following sections:
|
||||
|
||||
[horizontal]
|
||||
<<legal.adoc#legal-information,Legal>> :: Legal information.
|
||||
<<documentation-overview.adoc#contract-documentation,Documentation Overview>> :: About the Documentation, Getting Help, First Steps, and more.
|
||||
<<getting-started.adoc#getting-started,Getting Started>> :: Introducing Spring Cloud Contract, System Requirements, Developing Your First Spring Cloud Contract based Application
|
||||
<<using-spring-cloud-contract.adoc#using-contract,Using Spring Cloud Contract>> :: Build Systems, Structuring Your Code, Configuration, Spring Beans and Dependency Injection, and more.
|
||||
<<spring-cloud-contract-features.adoc#contract-features,Spring Cloud Contract Features>> :: Profiles, Logging, Security, Caching, Spring Integration, Testing, and more.
|
||||
<<production-ready-features.adoc#production-ready,Spring Boot Actuator>> :: Monitoring, Metrics, Auditing, and more.
|
||||
<<build-tool-plugins.adoc#build-tool-plugins,Build Tool Plugins>> :: Maven Plugin, Gradle Plugin.
|
||||
<<howto.adoc#howto,"`How-to`" Guides>> :: Application Development, Configuration, Embedded Servers, Data Access, and many more.
|
||||
<<appendix.adoc#appendix,Appendices>> :: Properties, Metadata, Configuration, Dependencies, and more.
|
||||
14
docs/src/main/asciidoc/index.htmlpdfadoc
Normal file
14
docs/src/main/asciidoc/index.htmlpdfadoc
Normal file
@@ -0,0 +1,14 @@
|
||||
[[spring-cloud-contract-reference-documentation]]
|
||||
= Spring Cloud Contract Reference Documentation
|
||||
Adam Dudczak, Mathias Düsterhöft, Marcin Grzejszczak, Dennis Kieselhorst, Jakub Kubryński, Karol Lassak, Olga Maciaszek-Sharma, Mariusz Smykuła, Dave Syer, Jay Bryant
|
||||
:docinfo: shared
|
||||
include::attributes.adoc[]
|
||||
|
||||
include::legal.adoc[leveloffset=+1]
|
||||
include::documentation-overview.adoc[leveloffset=+1]
|
||||
include::getting-started.adoc[leveloffset=+1]
|
||||
include::using-spring-cloud-contract.adoc[leveloffset=+1]
|
||||
include::spring-cloud-contract-features.adoc[leveloffset=+1]
|
||||
include::build-tool-plugins.adoc[leveloffset=+1]
|
||||
include::howto.adoc[leveloffset=+1]
|
||||
include::appendix.adoc[leveloffset=+1]
|
||||
14
docs/src/main/asciidoc/index.htmlsingleadoc
Normal file
14
docs/src/main/asciidoc/index.htmlsingleadoc
Normal file
@@ -0,0 +1,14 @@
|
||||
[[spring-cloud-contract-reference-documentation]]
|
||||
= Spring Cloud Contract Reference Documentation
|
||||
Adam Dudczak, Mathias Düsterhöft, Marcin Grzejszczak, Dennis Kieselhorst, Jakub Kubryński, Karol Lassak, Olga Maciaszek-Sharma, Mariusz Smykuła, Dave Syer, Jay Bryant
|
||||
:docinfo: shared
|
||||
include::attributes.adoc[]
|
||||
|
||||
include::legal.adoc[leveloffset=+1]
|
||||
include::documentation-overview.adoc[leveloffset=+1]
|
||||
include::getting-started.adoc[leveloffset=+1]
|
||||
include::using-spring-cloud-contract.adoc[leveloffset=+1]
|
||||
include::spring-cloud-contract-features.adoc[leveloffset=+1]
|
||||
include::build-tool-plugins.adoc[leveloffset=+1]
|
||||
include::howto.adoc[leveloffset=+1]
|
||||
include::appendix.adoc[leveloffset=+1]
|
||||
11
docs/src/main/asciidoc/legal.adoc
Normal file
11
docs/src/main/asciidoc/legal.adoc
Normal file
@@ -0,0 +1,11 @@
|
||||
[legal]
|
||||
= Legal
|
||||
|
||||
{spring-cloud-contract-version}
|
||||
|
||||
Copyright © 2012-2019
|
||||
|
||||
Copies of this document may be made for your own use and for distribution to
|
||||
others, provided that you do not charge any fee for such copies and further
|
||||
provided that each copy contains this Copyright Notice, whether distributed in
|
||||
print or electronically.
|
||||
@@ -543,7 +543,7 @@ Surefire plugin setup, like in the following example:
|
||||
|
||||
[source,xml,indent=0]
|
||||
----
|
||||
include::{samples_url}/producer_with_spock/pom.xml[tags=pock-surefire-setup,indent=0]
|
||||
include::{samples_url}/producer_with_spock/pom.xml[tags=spock-surefire-setup,indent=0]
|
||||
----
|
||||
|
||||
= Stubs and Transitive Dependencies
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
:toc: left
|
||||
:toclevels: 3
|
||||
:nofooter:
|
||||
:source-highlighter: prettify
|
||||
:numbered:
|
||||
:icons: font
|
||||
:sectlinks: true
|
||||
:branch: master
|
||||
|
||||
= Spring Cloud Contract
|
||||
|
||||
_Documentation Authors: Adam Dudczak, Mathias Düsterhöft, Marcin Grzejszczak, Dennis Kieselhorst, Jakub Kubryński, Karol Lassak,
|
||||
Olga Maciaszek-Sharma, Mariusz Smykuła, Dave Syer, Jay Bryant_
|
||||
|
||||
{spring-cloud-version}
|
||||
|
||||
== Spring Cloud Contract
|
||||
|
||||
You need confidence when pushing new features to a new application or service in a
|
||||
distributed system. This project provides contract tests support for both HTTP and message-based interactions,
|
||||
covering a range of options for writing tests, publishing them as assets, and asserting
|
||||
that a contract is kept by producers and consumers.
|
||||
|
||||
include::_spring-cloud-contract-verifier.adoc[]
|
||||
|
||||
include::_spring-cloud-wiremock.adoc[]
|
||||
|
||||
include::_links.adoc[]
|
||||
1
docs/src/main/asciidoc/spring-cloud-contract.adoc
Symbolic link
1
docs/src/main/asciidoc/spring-cloud-contract.adoc
Symbolic link
@@ -0,0 +1 @@
|
||||
index.htmladoc
|
||||
Reference in New Issue
Block a user