介绍 Springboot
Spring Boot_百度百科
新建项目
- 打开IDEA选择 File->New->Project;
- 选择 Spring Initializr
- Spring initializr 是Spring 官方提供的一个用来初始化一个Spring boot 项目的工具。
- 组名项目名称可自定义
- 点击 next
- 选择 Dependencies
Web下面选择Spring Web;
点击 create
项目加载完成后,如下:
配置 Maven
在设置里面配置 Maven
测试
- 修改配置文件application.properties为application.yml,配置一下启动端口,默认8080。
- 编写一个控制器–>新建一个test包新建一个index类.添加@Controller注解。
- 编写index方法
注意:@RestController = @Controller + @ResponseBody 返回值的数据自动封装为json的数据格式
@RequestMapping :处理请求地址映射的注解
package com.example.demo.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class index {
@RequestMapping("/index")
@ResponseBody
public String index(){
return "hello world!";
}
}
“代码解释”: {
“@Controller”: “这是一个Spring MVC的注解,它表示该类是一个控制器。Spring MVC会扫描到带有这个注解的类,并把它作为一个控制器组件进行管理。”,“public class index”: “这是定义一个名为’index’的公开类。”,“@RequestMapping(”/index")": “这是Spring MVC中的一个注解,它映射(即将一个特定的HTTP请求路由到)'/index’路径到下面的’index’方法。”,“@ResponseBody”: “这是Spring MVC的一个注解,它表示下面的方法返回的结果直接写入HTTP response body中,通常是用来做REST API的。”,“public String index()”: “这是一个公开的方法,名为’index’,返回类型为String。”,“return “hello world!””: “这是方法的返回语句,返回一个字符串’hello world!'。”},“代码运行结果”: “当你访问’/index’这个URL时,你会在浏览器中看到’hello world!'这个字符串。”**
}
运行项目
运行 Demo1Application
运行成功:
打开浏览器,打开网址:localhost:8080/index