Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 66 additions & 33 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ application development. As you read more Spring Getting Started guides, you wil
use cases for Spring Boot. This guide is meant to give you a quick taste of Spring Boot.
If you want to create your own Spring Boot-based project, visit
https://start.spring.io/[Spring Initializr], fill in your project details, pick your
options, and download a bundled up project as a zip file.
options, and download a bundled-up project as a zip file.

== What You Will build

Expand Down Expand Up @@ -54,34 +54,39 @@ them to your application context.
[[scratch]]
== Starting with Spring Initializr

You can use this https://start.spring.io/#!type=maven-project&language=java&packaging=jar&jvmVersion=17&groupId=com.example&artifactId=spring-boot&name=spring-boot&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.spring-boot&dependencies=web[pre-initialized project] and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.
You can use this https://start.spring.io/#!type=maven-project&&packaging=jar&jvmVersion=17&groupId=com.example&artifactId=spring-boot&name=spring-boot&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.springboot&dependencies=web[pre-initialized project] and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.

To manually initialize the project:

. Navigate to https://start.spring.io.
This service pulls in all the dependencies you need for an application and does most of the setup for you.
. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.
. Choose either Gradle or Maven and the language you want to use: Kotlin or Java.
. Click *Dependencies* and select *Spring Web*.
. Click *Generate*.
. Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.

NOTE: If your IDE has the Spring Initializr integration, you can complete this process from your IDE.

NOTE: You can also fork the project from Github and open it in your IDE or other editor.
NOTE: You can also fork the project from GitHub and open it in your IDE or other editor.

NOTE: For Spring 3.0 you need Java 17 or later, regardless of whether you use Spring Initializr.

[[initial]]
== Create a Simple Web Application

Now you can create a web controller for a simple web application, as the following listing
(from `src/main/java/com/example/springboot/HelloController.java`) shows:
Now you can create a web controller for a simple web application, as the following listing shows:

====
[source,java]
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
include::initial/src/main/java/com/example/springboot/HelloController.java[]
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
include::initial-kotlin/src/main/kotlin/com/example/springboot/HelloController.kt[]
----
====

The class is flagged as a `@RestController`, meaning it is ready for use by Spring MVC to
Expand All @@ -93,26 +98,30 @@ results in web requests returning data rather than a view.
== Create an Application class

The Spring Initializr creates a simple application class for you. However, in this case,
it is too simple. You need to modify the application class to match the following listing
(from `src/main/java/com/example/springboot/Application.java`):
it is too simple. You need to modify the application class to match the following listing:

====
[source,java]
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
include::complete/src/main/java/com/example/springboot/Application.java[]
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
include::complete-kotlin/src/main/kotlin/com/example/springboot/Application.kt[]
----
====

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/spring-boot-application-new-path.adoc[]

There is also a `CommandLineRunner` method marked as a `@Bean`, and this runs on start up.
There is also a `CommandLineRunner` method marked as a `@Bean`, and this runs on startup.
It retrieves all the beans that were created by your application or that were
automatically added by Spring Boot. It sorts them and prints them out.

== Run the Application

To run the application, run the following command in a terminal window (in the `complete`)
directory:
To run the application, run the following command in a terminal window directory:

====
[source,text]
Expand All @@ -121,8 +130,7 @@ directory:
----
====

If you use Maven, run the following command in a terminal window (in the `complete`)
directory:
If you use Maven, run the following command in a terminal window directory:

====
[source,text]
Expand Down Expand Up @@ -176,7 +184,7 @@ viewControllerHandlerMapping

You can clearly see `org.springframework.boot.autoconfigure` beans. There is also a `tomcatEmbeddedServletContainerFactory`.

Now run the service with curl (in a separate terminal window), by running the following
Now run the service with curl (in a separate terminal window) by running the following
command (shown with its output):

====
Expand All @@ -192,12 +200,20 @@ Greetings from Spring Boot!
You will want to add a test for the endpoint you added, and Spring Test provides some
machinery for that.

If you use Gradle, add the following dependency to your `build.gradle` file:
If you use Gradle, add the following dependency to your `build.gradle(.kts)` file:

[source,groovy,indent=0]
====
[source,groovy,indent=0,subs="verbatim,quotes",role="primary"]
.Groovy
----
include::complete/build.gradle[tag=tests]
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
include::complete-kotlin/build.gradle.kts[tag=tests]
----
====

If you use Maven, add the following to your `pom.xml` file:

Expand All @@ -207,14 +223,19 @@ include::complete/pom.xml[tag=tests]
----

Now write a simple unit test that mocks the servlet request and response through your
endpoint, as the following listing (from
`src/test/java/com/example/springboot/HelloControllerTest.java`) shows:
endpoint, as the following listing shows:

====
[source,java]
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
include::complete/src/test/java/com/example/springboot/HelloControllerTest.java[]
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
include::complete-kotlin/src/test/kotlin/com/example/springboot/HelloControllerTest.kt[]
----
====

`MockMvc` comes from Spring Test and lets you, through a set of convenient builder
Expand All @@ -228,14 +249,19 @@ narrow it down if you want to build something different.

As well as mocking the HTTP request cycle, you can also use Spring Boot to write a simple
full-stack integration test. For example, instead of (or as well as) the mock test shown
earlier, we could create the following test (from
`src/test/java/com/example/springboot/HelloControllerITest.java`):
earlier, we could create the following test:

====
[source,java]
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
include::complete/src/test/java/com/example/springboot/HelloControllerITest.java[]
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
include::complete-kotlin/src/test/kotlin/com/example/springboot/HelloControllerITest.kt[]
----
====

The embedded server starts on a random port because of
Expand All @@ -244,17 +270,25 @@ configured automatically in the base URL for the `TestRestTemplate`.

== Add Production-grade Services

If you are building a web site for your business, you probably need to add some management
If you are building a website for your business, you probably need to add some management
services. Spring Boot provides several such services (such as health, audits, beans, and
more) with its
http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#actuator[actuator module].

If you use Gradle, add the following dependency to your `build.gradle` file:
If you use Gradle, add the following dependency to your `build.gradle(.kts)` file:

[source,groovy,indent=0]
====
[source,groovy,indent=0,subs="verbatim,quotes",role="primary"]
.Groovy
----
include::complete/build.gradle[tag=actuator]
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
include::complete-kotlin/build.gradle.kts[tag=actuator]
----
====

If you use Maven, add the following dependency to your `pom.xml` file:

Expand All @@ -264,7 +298,7 @@ include::complete/pom.xml[tag=actuator]
----

Then restart the application. If you use Gradle, run the following command in a terminal
window (in the `complete` directory):
window:

====
[subs="attributes"]
Expand All @@ -273,8 +307,7 @@ window (in the `complete` directory):
----
====

If you use Maven, run the following command in a terminal window (in the `complete`
directory):
If you use Maven, run the following command in a terminal window:

====
[subs="attributes"]
Expand All @@ -283,8 +316,8 @@ directory):
----
====

You should see that a new set of RESTful end points have been added to the application.
These are management services provided by Spring Boot. The following listing shows typical
You should see that a new set of RESTful end points has been added to the application.
These are management services provided by Spring Boot. The following listing shows a typical
output:

====
Expand Down Expand Up @@ -342,7 +375,7 @@ Because we did not enable it, the requested endpoint is not available (because t
exist).

For more details about each of these REST endpoints and how you can tune their settings
with an `application.properties` file (in `src/main/resources`), see the
with an `application.properties` file (in `src/main/resources`), see
the http://docs.spring.io/spring-boot/docs/{spring_boot_version}/reference/htmlsingle/#production-ready-endpoints[documentation about the endpoints].

== View Spring Boot's Starters
Expand Down
35 changes: 35 additions & 0 deletions complete-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
plugins {
kotlin("jvm") version "1.9.25"
kotlin("plugin.spring") version "1.9.25"
id("org.springframework.boot") version "3.5.6"
id("io.spring.dependency-management") version "1.1.7"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
mavenCentral()
}
dependencies {
// tag::actuator[]
implementation("org.springframework.boot:spring-boot-starter-actuator")
// end::actuator[]
implementation("org.springframework.boot:spring-boot-starter-web")
// tag::tests[]
testImplementation("org.springframework.boot:spring-boot-starter-test")
// end::tests[]
}

kotlin {
jvmToolchain(17)
compilerOptions {
freeCompilerArgs.addAll("-Xjsr305=strict")
}
}


tasks.withType<Test> {
useJUnitPlatform()
}
Binary file added complete-kotlin/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions complete-kotlin/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading