SpringBoot + Maven搭建Hello world

基本介绍

我们使用Maven进行代码编译及构建,使用SpringBoot 2.2.5.RELEASE版本,同时当我们通过浏览器访问链接http://localhost:8080/hello时,会在浏览器上输出hello world

Pom文件

<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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mary.demo</groupId>
  <artifactId>SpringBootDemo</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SpringBootDemo</name>

  <!-- Inherit defaults from Spring Boot -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

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

    </dependencies>

    <!-- Package as an executable jar -->
    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </build>
</project>

项目结构

项目结构如下:
在这里插入图片描述

项目代码

  1. 入口类APP.java
package com.mary.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.annotation.Inherited;

/**
 *
 */
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.mary.demo"})
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

``
2. HTTP API访问入口类HelloWorldController

```java
package com.mary.demo;

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

@RestController
public class HelloWorldController {

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

    @RequestMapping("/test")
    String test(){
        return "test";
    }
}

启动服务

当我们代码写完之后,我们可以开始对我们项目启动了,由于SpringBoot中默认嵌入了Tomcat,所以直接启动主类APP.java文件即会启动嵌入的tomcat
启动成功之后,你会看到如下图所示的屏幕,同时会看到Started App in 1.668 seconds (JVM running for 3.484)这样的字眼,那就说明启动成功了

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

2020-03-12 08:20:30.683  INFO 6752 --- [           main] com.mary.demo.App                        : Starting App on DESKTOP-FV5K1S8 with PID 6752 (E:\soft\code\SpringBootDemo\target\classes started by yangchao in E:\soft\code\SpringBootDemo)
2020-03-12 08:20:30.687  INFO 6752 --- [           main] com.mary.demo.App                        : No active profile set, falling back to default profiles: default
2020-03-12 08:20:31.609  INFO 6752 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-03-12 08:20:31.617  INFO 6752 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-12 08:20:31.617  INFO 6752 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-12 08:20:31.721  INFO 6752 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-12 08:20:31.721  INFO 6752 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 991 ms
2020-03-12 08:20:31.877  INFO 6752 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-12 08:20:32.013  INFO 6752 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
**2020-03-12 08:20:32.017  INFO 6752 --- [           main] com.mary.demo.App                        : Started App in 1.668 seconds (JVM running for 3.484)**

通过浏览器访问服务

打开浏览器,输入http://localhost:8080/hello,则会看到如下图所示内容,说明我们的Hello World搭建成功了
在这里插入图片描述

写在最后

到此你的Hello World就搭建成功了

发布了14 篇原创文章 · 获赞 1 · 访问量 428

猜你喜欢

转载自blog.csdn.net/qinwuxian19891211/article/details/104811146