Spring Boot's "hello world" program

Environmental Science

  • Operating system: Ubuntu 20.04
  • Development tool: IntelliJ IDEA 2020.1.2 (Community Edition)
  • JDK:
➜  ~ java -version
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+7-Ubuntu-1ubuntu1)
OpenJDK 64-Bit Server VM (build 14.0.1+7-Ubuntu-1ubuntu1, mixed mode, sharing)
  • Maven:
➜  ~ mvn -v
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 14.0.1, vendor: Private Build, runtime: /usr/lib/jvm/java-14-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-37-generic", arch: "amd64", family: "unix"

to configure

Configure environment variables

Modify ~ /. bashrc file and add configuration JAVA_HOME and M2_ The home variable is as follows:

export JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64
export M2_HOME=/usr/share/maven

Save exit, run. ~/.bashrc to make it effective.

If other shells are configured as the default shell, you need to modify the corresponding configuration file. For example, if I configured zsh as the default shell, I need to modify the ~ /. zshrc file and run. ~ /. zshrc to make it effective.

Note: you can use echo $SHELL to view the current shell:

➜  ~ echo $SHELL
/usr/bin/zsh

Configuring Maven images

Open settings.xml (in this case, the path is / usr/share/maven/conf/settings.xml )Add the nexus server image of aliyun between < mirrors >... < mirrors >

......
    <mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
......

Configure IntelliJ IDEA

Configure Maven path

File -> Settings… , as shown in the following figure:

Installing the Spring Assistant plug-in

File -> Settings… ->Plugins, click "Marketplace", search for the keyword "Spring", find "Spring Assistant" and install it, as shown below:

Write code

Now that all the above preparations have been completed, you can start to write code.

Create a new Project and select "Spring Assistant", as shown in the following figure:

Next, fill in the Project information, as shown in the following figure:

Next, search "Web" and select Spring Web, as shown below:
The resulting Project structure is shown in the following figure:

At this time, there may be some errors in Project because of the lack of required dependency packages. From the Maven window on the right, select "compile" to download the required dependency package.

Now you can Run the program. Open demo0621 Application.java , click the green small arrow, and select Run, as shown in the following figure:

After the program is started, as shown in the following figure:

It can be seen that Tomcat has started on port 8080. Test the connectivity of port 8080 as follows:

➜  ~ nc -zv localhost 8080
Connection to localhost 8080 port [tcp/http-alt] succeeded!

However, there is no Controller at present, so accessing the server will report a 404 error:

➜  ~ curl --silent --write-out '\n%{http_code}\n' -XGET "http://localhost:8080/" | jq .
{
  "timestamp": "2020-06-21T02:58:23.082+00:00",
  "status": 404,
  "error": "Not Found",
  "message": "",
  "path": "/"
}
404

Now let's add a Controller called "hello".

Create a new Java class "Test0621", as follows:

package com.example.demo0621;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Test0621 {
    @RequestMapping("/hello")
    public String hello() {
        return "hello world!";
    }
}

As shown in the figure below:

Restart the program, and now you can access / hello.

➜  ~ curl --silent --write-out '\n%{http_code}\n' -XGET "http://localhost:8080/hello"       
hello world!
200

The console output of the program is as follows:

/home/ding/.jdks/openjdk-14.0.1/bin/java -javaagent:/snap/intellij-idea-community/232/lib/idea_rt.jar=43913:/snap/intellij-idea-community/232/bin -Dfile.encoding=UTF-8 -classpath /home/ding/IdeaProjects/demo0621/target/classes:/home/ding/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.3.1.RELEASE/spring-boot-starter-web-2.3.1.RELEASE.jar:/home/ding/.m2/repository/org/springframework/boot/spring-boot-starter/2.3.1.RELEASE/spring-boot-starter-2.3.1.RELEASE.jar:/home/ding/.m2/repository/org/springframework/boot/spring-boot/2.3.1.RELEASE/spring-boot-2.3.1.RELEASE.jar:/home/ding/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.3.1.RELEASE/spring-boot-autoconfigure-2.3.1.RELEASE.jar:/home/ding/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.3.1.RELEASE/spring-boot-starter-logging-2.3.1.RELEASE.jar:/home/ding/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/home/ding/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/home/ding/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar:/home/ding/.m2/repository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar:/home/ding/.m2/repository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar:/home/ding/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/home/ding/.m2/repository/org/yaml/snakeyaml/1.26/snakeyaml-1.26.jar:/home/ding/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.3.1.RELEASE/spring-boot-starter-json-2.3.1.RELEASE.jar:/home/ding/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.0/jackson-databind-2.11.0.jar:/home/ding/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.0/jackson-annotations-2.11.0.jar:/home/ding/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.0/jackson-core-2.11.0.jar:/home/ding/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.11.0/jackson-datatype-jdk8-2.11.0.jar:/home/ding/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.11.0/jackson-datatype-jsr310-2.11.0.jar:/home/ding/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.11.0/jackson-module-parameter-names-2.11.0.jar:/home/ding/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.3.1.RELEASE/spring-boot-starter-tomcat-2.3.1.RELEASE.jar:/home/ding/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.36/tomcat-embed-core-9.0.36.jar:/home/ding/.m2/repository/org/glassfish/jakarta.el/3.0.3/jakarta.el-3.0.3.jar:/home/ding/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.36/tomcat-embed-websocket-9.0.36.jar:/home/ding/.m2/repository/org/springframework/spring-web/5.2.7.RELEASE/spring-web-5.2.7.RELEASE.jar:/home/ding/.m2/repository/org/springframework/spring-beans/5.2.7.RELEASE/spring-beans-5.2.7.RELEASE.jar:/home/ding/.m2/repository/org/springframework/spring-webmvc/5.2.7.RELEASE/spring-webmvc-5.2.7.RELEASE.jar:/home/ding/.m2/repository/org/springframework/spring-aop/5.2.7.RELEASE/spring-aop-5.2.7.RELEASE.jar:/home/ding/.m2/repository/org/springframework/spring-context/5.2.7.RELEASE/spring-context-5.2.7.RELEASE.jar:/home/ding/.m2/repository/org/springframework/spring-expression/5.2.7.RELEASE/spring-expression-5.2.7.RELEASE.jar:/home/ding/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/home/ding/.m2/repository/org/springframework/spring-core/5.2.7.RELEASE/spring-core-5.2.7.RELEASE.jar:/home/ding/.m2/repository/org/springframework/spring-jcl/5.2.7.RELEASE/spring-jcl-5.2.7.RELEASE.jar com.example.demo0621.Demo0621Application

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-06-20 20:08:38.135  INFO 5499 --- [           main] c.example.demo0621.Demo0621Application   : Starting Demo0621Application on ubuntu with PID 5499 (/home/ding/IdeaProjects/demo0621/target/classes started by ding in /home/ding/IdeaProjects/demo0621)
2020-06-20 20:08:38.138  INFO 5499 --- [           main] c.example.demo0621.Demo0621Application   : No active profile set, falling back to default profiles: default
2020-06-20 20:08:39.707  INFO 5499 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-06-20 20:08:39.722  INFO 5499 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-06-20 20:08:39.723  INFO 5499 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-06-20 20:08:39.814  INFO 5499 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-06-20 20:08:39.814  INFO 5499 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1606 ms
2020-06-20 20:08:40.051  INFO 5499 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-20 20:08:40.273  INFO 5499 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-06-20 20:08:40.286  INFO 5499 --- [           main] c.example.demo0621.Demo0621Application   : Started Demo0621Application in 2.968 seconds (JVM running for 4.244)
2020-06-20 20:09:03.378  INFO 5499 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-20 20:09:03.379  INFO 5499 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-06-20 20:09:03.387  INFO 5499 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms

Tags: Spring Tomcat Maven Apache

Posted on Sat, 20 Jun 2020 23:58:02 -0400 by keegan