开发你的第一个Spring Boot应用, Hello World!


使用Java开发一个简单的"Hello World!" web应用,将使用Maven构建该项


pom.xml

<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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.made</groupId>
 <artifactId>springboot_demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 
 <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.0.BUILD-SNAPSHOT</version>
   </parent>

   <!-- Additional lines to be added here... -->

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

   <!-- (you don't need this if you are using a .RELEASE version) -->
   <repositories>
       <repository>
           <id>spring-snapshots</id>
           <url>http://repo.spring.io/snapshot</url>
           <snapshots><enabled>true</enabled></snapshots>
       </repository>
       <repository>
           <id>spring-milestones</id>
           <url>http://repo.spring.io/milestone</url>
       </repository>
   </repositories>
   <pluginRepositories>
       <pluginRepository>
           <id>spring-snapshots</id>
           <url>http://repo.spring.io/snapshot</url>
       </pluginRepository>
       <pluginRepository>
           <id>spring-milestones</id>
           <url>http://repo.spring.io/milestone</url>
       </pluginRepository>
   </pluginRepositories>
</project>

代码

package com.made.demo;

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

@RestController
@EnableAutoConfiguration
public class Example {
    
    @RequestMapping("/home")
    public String home() {
        
        return "Hello world !";
    }

    public static void main(String[] args) {
        
        SpringApplication.run(Example.class, args);
    }
}

启动

  1. 进行 Run As ---> Java Application启动 会看到控制台打印如下

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.0.0.BUILD-SNAPSHOT)
....... . . .
....... . . . (log output here)
....... . . .
com.made.demo.Example: Started Example in 3.44 seconds (JVM running for 3.929)

  1. 浏览器访问
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,014评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,678评论 25 708
  • 原文链接:http://www.dropwizard.io/1.2.0/docs/getting-started....
    Lance_Xu阅读 955评论 0 0
  • 两次义工旅行总计一个月我都在238青旅 度过从开始的小白一个月 不多不少我也学会的不少东西 001壁画 我是心理学...
    陈凯字东洲阅读 291评论 0 0