Spring Boot - Hello World

2017-11-06  本文已影响86人  番薯IT

Spring Boot 介绍

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。-- 摘自百度百科

目前SpringBoot的最新版是1.5.8

Spring Boot 项目搭建

准备

保证你的IDEA已经配置好了jdk和maven

创建项目

打开IDEA -> new Project -> Spring Initializr -> 填写Group,Article -> 选择Web,勾选Web -> Next -> Finish

Group和Article用户自己定义,我的如下:

生成如下结构

--spring-boot-helloworld
    --src
        --main
            --java
                --com.roachfu.tutorial
                    --Application.java
            --resources
                --static
                --templates
                --application.properties
        --test
            --java
                --com.roachfu.tutorial
                    --ApplicationTests.java
    --pom.xml

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.roachfu.tutorial</groupId>
    <artifactId>spring-boot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-helloworld</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </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>

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

Hello World

package com.roachfu.tutorial.controller;

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

@RestController
@RequestMapping("/hello")
public class HelloController {
    
    @GetMapping
    public String index(){
        System.out.println("Hello World Spring Boot");
        return "Hello World Spring Boot !!!";
    }
}

项目源码

上一篇 下一篇

猜你喜欢

热点阅读