Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Latest commit

 

History

History
153 lines (122 loc) · 4.99 KB

lab01.adoc

File metadata and controls

153 lines (122 loc) · 4.99 KB

Building a Spring Boot Application

Create a Spring Boot Project

  1. Browse to https://start.spring.io

  2. Generate a Maven Project with Spring Boot 1.5.2.

  3. Fill out the Project metadata fields as follows:

    Group

    io.pivotal

    Artifact

    cloud-native-spring

  4. In the dependencies section, add the following manually:

    Web Rest Repositories JPA H2 Actuator

  5. Click the Generate Project button. Your browser will download a zip file.

  6. Copy then unpack the downloaded zip file to CN-Workshop-TM/labs/lab01/cloud-native-spring

    Your directory structure should now look like:

    CN-Workshop-TM:
    ├── labs
    │   ├── lab01
    │   │   ├── cloud-native-spring
  7. Import the project’s pom.xml into your editor/IDE of choice.

    STS Import Help:

    Select File > Import… Then select Maven > Existing Maven Projects. On the Import Maven Projects page, browse to the /cloud-native-spring directory (e.g. CN-Workshop-TM/labs/lab01/cloud-native-spring)

Add an Endpoint

  1. Add an @RestController annotation to the class io.pivotal.CloudNativeSpringApplication (/cloud-native-spring/src/main/java/io/pivotal/CloudNativeSpringApplication.java).

    package io.pivotal;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class CloudNativeSpringApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(CloudNativeSpringApplication.class, args);
        }
    }
  2. Add the following request handler to the class _io.pivotal.CloudNativeSpringApplication (/cloud-native-spring/src/main/java/io/pivotal/CloudNativeSpringApplication.java).

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

    Completed:

    package io.pivotal;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class CloudNativeSpringApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(CloudNativeSpringApplication.class, args);
        }
    
        @RequestMapping("/")
        public String hello() {
            return "Hello World!";
        }
    }

Run the cloud-native-spring Application

  1. In a terminal, change working directory to CN-Workshop-TM/labs/lab01/cloud-native-spring

    $ cd /CN-Workshop-TM/labs/lab01/cloud-native-spring

  2. Run the application

    $ mvn clean spring-boot:run

  3. You should see the application start up an embedded Apache Tomcat server on port 8080 (review terminal output):

    2015-10-02 13:26:59.264  INFO 44749 --- [lication.main()] s.b.c.e.t.TomcatEmbeddedServletContainer: Tomcat started on port(s): 8080 (http)
    2015-10-02 13:26:59.267  INFO 44749 --- [lication.main()] io.pivotal.hello.CloudNativeSpringApplication: Started CloudNativeSpringApplication in 2.541 seconds (JVM running for 9.141)
  4. Browse to http://localhost:8080

  5. Stop the cloud-native-spring application. In the terminal window: Ctrl + C

Deploy cloud-native-spring to Pivotal Cloud Foundry

  1. Build the application

    $ mvn clean package
  2. Create an application manifest in the root folder /cloud-native-spring

    $ touch manifest.yml

  3. Add application metadata, using a text editor (of choice)

    ---
    applications:
    - name: cloud-native-spring
      host: cloud-native-spring-${random-word}
      memory: 1G
      instances: 1
      path: ./target/cloud-native-spring-0.0.1-SNAPSHOT.jar
      buildpack: java_buildpack_offline
      env:
        JAVA_OPTS: -Djava.security.egd=file:///dev/urandom
  4. Push application into Cloud Foundry

    $ cf push -f manifest.yml

  5. Find the URL created for your app in the health status report. Browse to your app.

Congratulations! You’ve just completed your first Spring Boot application.