Skip to content

Latest commit

 

History

History
223 lines (179 loc) · 5.39 KB

lab_01.adoc

File metadata and controls

223 lines (179 loc) · 5.39 KB

Lab 1 - Getting Started with Spring Boot

Bootstrap the Project

  1. In your browser, visit http://start.spring.io.

  2. Fill out the Project metadata fields as follows:

    Group

    io.pivotal.spring

    Artifact

    hello-spring-boot

  3. In the Dependencies section, search for web, and choose Web from the autocomplete menu.

  4. Click the Generate Project button. Your browser will download a zip file. Unpack that zip file at $COURSE_HOME/labs/initial.

  5. Import the project’s pom.xml into your editor/IDE of choice.

  6. Add a @RestController annotation to the class io.pivotal.spring.HelloSpringBootApplication.

  7. Add the following request handler to the class io.pivotal.spring.HelloSpringBootApplication:

    @RequestMapping("/")
    public String hello() {
        return "Hello World!";
    }

Build and Run with Embedded Apache Tomcat

Spring Boot will embed Apache Tomcat by default.

  1. Build the application:

    $ ./mvnw package
  2. Run the application:

    $ java -jar target/hello-spring-boot-0.0.1-SNAPSHOT.jar

    You should see the application start up an embedded Apache Tomcat server on port 8080:

    2015-12-06 17:17:30.507  INFO 60277 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2015-12-06 17:17:30.511  INFO 60277 --- [           main] io.pivotal.spring.HelloSpringBootApplication    : Started HelloSpringBootApplication in 3.201 seconds (JVM running for 3.846)
  3. Visit the application in the browser (http://localhost:8080), and you should see the following:

    Hello World!

Build and Run with Embedded Eclipse Jetty

Spring Boot also supports embedding a Jetty server.

  1. Open pom.xml and replace the following:

    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    with:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
  2. Build the application:

    $ ./mvnw package
  3. Run the application:

    $ java -jar target/hello-spring-boot-0.0.1-SNAPSHOT.jar

    You should see the application start up an embedded Jetty server on port 8080:

    2015-12-06 17:19:11.649  INFO 60588 --- [           main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1)
    2015-12-06 17:19:11.654  INFO 60588 --- [           main] io.pivotal.spring.HelloSpringBootApplication    : Started HelloSpringBootApplication in 3.781 seconds (JVM running for 4.217)
  4. Visit the application in the browser (http://localhost:8080), and you should see the following:

    Hello World!

Refactor to Externalize the Config

  1. Rename src/main/resources/application.properties to src/main/resources/application.yml. Into that file, paste the following:

    greeting: Hello
  2. To the class io.pivotal.spring.HelloSpringBootApplication, add a greeting field and inject its value:

    @Value("${greeting}")
    String greeting;
  3. Also io.pivotal.spring.HelloSpringBootApplication, change the return statement of hello() to the following:

    return String.format("%s World!", greeting);
  4. Build the application:

    $ ./mvnw package
  5. Run the application:

    $ java -jar target/hello-spring-boot-0.0.1-SNAPSHOT.jar
  6. Visit the application in the browser (http://localhost:8080), and verify that the output is still the following:

    Hello World!
  7. Stop the application.

Using Environment Variables for Config

  1. Run the application again, this time setting the GREETING environment variable:

    $ GREETING=Ohai java -jar target/hello-spring-boot-0.0.1-SNAPSHOT.jar
  2. Visit the application in the browser (http://localhost:8080), and verify that the output has changed to the following:

    Ohai World!
  3. Stop the application.

Using Spring Profiles for Config

  1. Add a spanish profile to application.yml. Your finished configuration should reflect the following:

    greeting: Hello
    
    ---
    
    spring:
      profiles: spanish
    
    greeting: Hola
  2. Build the application:

    $ ./mvnw package
  3. Run the application, this time setting the SPRING_PROFILES_ACTIVE environment variable:

    $ SPRING_PROFILES_ACTIVE=spanish java -jar target/hello-spring-boot-0.0.1-SNAPSHOT.jar
  4. Visit the application in the browser (http://localhost:8080), and verify that the output has changed to the following:

    Hola World!
  5. Stop the application.

Resolving Conflicts

  1. Run the application, this time setting both the SPRING_PROFILES_ACTIVE and GREETING environment variables:

    $ SPRING_PROFILES_ACTIVE=spanish GREETING=Ohai java -jar target/hello-spring-boot-0.0.1-SNAPSHOT.jar
  2. Visit the application in the browser (http://localhost:8080), and verify that the output has changed to the following:

    Ohai World!
  3. Visit http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html to learn more about this outcome and the entire priority scheme for conflict resolution.