美文网首页
Spring Boot - Hello World

Spring Boot - Hello World

作者: 番薯IT | 来源:发表于2017-11-06 21:04 被阅读86次

Spring Boot 介绍

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

目前SpringBoot的最新版是1.5.8

Spring Boot 项目搭建

准备

  • JDK1.8
  • IDEA
  • Maven3

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

创建项目

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

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

  • Group : com.roachfu.tutorial
  • Article : spring-boot-helloworld

生成如下结构

--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

  • tutorial包下面创建controller

  • controller包下面创建HelloController

  • HelloController类的代码如下:

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 !!!";
    }
}
  • 执行Application类中的main方法

  • 待项目启动后,打开浏览器,输入:http://localhost:8080/hello。查看控制台和浏览器的输出。

项目源码

相关文章

网友评论

      本文标题:Spring Boot - Hello World

      本文链接:https://www.haomeiwen.com/subject/fmjnpxtx.html