学习 准备 尝试 谨慎小心

0%

IDEA创建一个SpringBoot项目

[toc]

看图说话,手把手教你在IDEA中搭建一个SpringBoot项目

点击 File -> New -> Project

image-20191128113041555

选择 Spring Initializr

image-20191128113641733

填写 Group、Artifact 等信息

image-20191128155036625

Tip:Artifact 名称不能包含大写

这和 java 包名的命名规范有相通之处,java 包名不能包含大写字母

选择 Web -> Spring Web

image-20191128155056548

选择项目目录

image-20191128155113536

创建完毕

image-20191128155555797

编写 HelloworldController

1
package com.liuchuanv.hellworld;
2
3
import org.springframework.stereotype.Controller;
4
import org.springframework.web.bind.annotation.GetMapping;
5
import org.springframework.web.bind.annotation.RequestMapping;
6
import org.springframework.web.bind.annotation.ResponseBody;
7
8
/**
9
 * description
10
 *
11
 * @author LiuChuanWei
12
 * @date 2019-11-28 16:00
13
 */
14
@Controller
15
@RequestMapping("/helloworld")
16
public class HelloworldController {
17
18
    @GetMapping("/sayHello")
19
    @ResponseBody
20
    public String sayHello() {
21
        return "Hello Baby!";
22
    }
23
}

Tip:方法 sayHello 的注解 @ResponseBody 作用是将方法返回值写入到response的body里,从而将数据直接返回给浏览器;如果没有加该注解,底层会将方法的返回值封装为ModelAndView对象然后返回。

运行 HelloworldApplication

image-20191128201718629

访问 http://localhost:8080/helloworld/sayHello

image-20191128160954091