Spring Boot搭建简单的hello world(一)

Spring boot简介

  spring boot是spring官方推出的一个全新框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程。

Spring boot特性

  1.创建独立的Spring应用程序

  2.嵌入的Tomcat,无需部署war文件

  3.简化maven配置

  4.自动配置spring

  5.提供声场就绪型功能,如指标,健康检查和外部配置

  6.开箱即用,没有代码生成,也无需xml配置。

spring boot并不是对spring功能上的增强,而是提供了一种快速使用spring的方式。

搭建简单的hello world程序

1.创建maven程序->next

2.编辑pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.david.springbootTest</groupId>
    <artifactId>springbootTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <!--sprin boot 父节点依赖,引入这个之后相关的引用就不需要添加version配置了,spring boot会选择最合适的版本进行添加 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--spring-boot-starter-web : spring相关的jar,内置tomcat服务器,jackson,MVC ,AOP 等 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

3.新建一个controller类

package com.david;

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

/**
 * RestController 等价于 @Controller 和 @ResponseBody
 */
@RestController //引入spring boot的web模块,就会自动配置web.xml等于web相关的内容
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

4.新建一个测试类-不需要部署tomcat服务器,内置的tomcat服务器直接通过main方法运行

package com.david;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 指定这是一个spring boot 应用程序
 */
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class,args);
    }
}

启动DemoApplication,默认端口8080 输入地址http://localhost:8080/hello 即可访问成功

猜你喜欢

转载自www.cnblogs.com/baidawei/p/9100977.html