1、SpringBoot之HelloWord

本项目主要是讲创建一个SpringBoot的HelloWorld项目

项目地址:https://github.com/Chakid/SpringBoot-example

该项目直接以maven的方式创建

pom文件中导入相关包:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

    <!--将应用打包成可执行的jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

创建SpringBoot启动类:

@SpringBootApplication
public class MainApplication {

    /**
     *  Spring应用启动起来
     */
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

创建一个控制类

@Controller
public class HelloController {


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

创建好相关类,具体代码可见项目中的实现,运行启动类;浏览器访问 http://127.0.0.1:8080/hello
即可见到相关效果:

image

推荐阅读更多精彩内容