SpringBoot HelloWorld

SpringBoot 是 基于Spring 开发的J2EE 一站式解决方案,近几年非常火热,今天刚入门,把自己的 SpringBoot 版的 HelloWorld 记录一下,以便后续整理,也希望各位大佬多多指教,感谢。

1、创建 Maven 工程
2、导入 Spring Boot 相关依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

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

 3、编写主程序:启动 Spring Boot 应用 

/**
 * @SpringBootApplication 标注主程序:说明此类是 SpringBoot 应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        //springboot 应用启动
        SpringApplication.run(HelloWorldMainApplication.class, args);
    }
}

 4、编写业务逻辑 

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }
}

5、启动主程序,测试

http://localhost:8080/hello

页面响应:Hello World

6、简化部署

pom 文件添加如下内容

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

在 IDEA 右侧边栏,maven --> lifecycle--运行 package 命令进行打包,然后启动打包后的 jar 文件,访问 http://localhost:8080/hello 即可;

原文地址:https://www.cnblogs.com/wdh01/p/12677092.html