Spring Boot2 (002): first create a simple application SpringBoot2 manual - "HelloWorld" web project

 

Note: The following reference springboot official document https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/pdf/spring-boot-reference.pdf  of 11. Developing Your First Spring Boot the Application .

First, the development environment configuration instructions:

  First out about some of their own development environment information:
win10 + the JDK 1.8.0_111 the Apache Maven 3.3.9 + + + idea2019.1 Ali cloud maven mirror (https://maven.aliyun.com/repository/public)
  Note that if you use maven at the command line and does not specify the configuration file, then use the default image maven, address: https://repo.maven.apache.org/maven2 .
  For JDK and maven, let's make sure no problem, command were java -version and mvn -v :

 

Second, create a simple project springboot2

1, create the relevant directories and files pom

  The first is to create the required directory project and pom.xml files, for example, I've created a springboot2-example-01 to be used as construction projects, interior structure is as follows:

  Of course, there is a pom.xml file as follows:

<?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.wpbxin</groupId>
    <artifactId>springboot2-first-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>
    <!-- Additional lines to be added here... -->
</project>

  Then use the command mvn Package Penalty for  (Note: In springboot2-example-01 catalog ) run to ensure normal packaging (there is no configuration file is specified, the default maven mirror used: https://repo.maven.apache.org/maven2 , Download speeds should be relatively slow, slow slow down a little rest, normal on the line, one not do it again ...):

  Note: If you encountered an error while packaging: " sun.security.validator.ValidatorException: PKIX path failed The Building: sun.security.provider.certpath.SunCertPathBuilderException: Unable to the Find! Valid Certification path to requested target ", can refer to another author Part Description: Maven: sun.security.validator.ValidatorException: PKIX path failed The Building: sun.security.provider.certpath.SunCertPathBuilderException: Unable to the Find! Valid Certification path to requested target , it is also possible  Maven: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the Parameter at The trustAnchors the MUST bE non-empty , can solve the problem.

 

2, add dependencies

  springboot offers a lot of " Starters " to add the relevant jar package dependencies, cases of springboot-starter-parent is a rather special parent POM , it offers many useful default configuration, but does not rely on any of the jar. Here you can run mvn dependency: tree view dependent on the current project (estimated to be downloaded first run many related jar package, the lower half of the screenshot sub second run this command, the first time the upper half), can be found in spring-boot-starter-parent does not provide any dependencies.

  Here we want to create a web project, the need for web-related dependencies, so add the following configuration:

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

  Do it again mvn dependency: Tree (screenshot is already downloaded exhausted), the instrument allows to see spring family bucket almost out, but also embedded Tomcat :

 

3, add the "Hello World" Code

  The HelloWorld the Java class:

package com.wpbxin;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class HelloWorldExample {
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldExample.class, args);
    }
}

 

4. Run Case

  Command mvn spring-boot: run to run (the author has encountered during PKIX mistake again several times to no problem, most likely network problems):

  See playing out Spring logo to normal, then visit http: // localhost: 8080 / Normal:

  Then press ctrl -c end:

 

5. Create a runnable jar

  Increase the allocation:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

  Executed again mvn Package Penalty for :

  This time the target directory will file springboot2-first-example-0.0.1- SNAPSHOT.jar.original and related jar package (about 10MB or so):

  By jar -tvf target \ springboot2-first- example-0.0.1-SNAPSHOT.jar view all internal documents and references:

  By this jar -jar to run the jar package, it is normal:

  Access HTTP: // localhost: 8080 / , the OK

  The same is by CTRL-c to end the run:

  At this point, " the HelloWorld " project OK.

 

Guess you like

Origin www.cnblogs.com/wpbxin/p/11756338.html