SpringBoot入门小案例

时间:2023-03-08 23:16:06
SpringBoot入门小案例

1、创建一个简单的maven project项目

SpringBoot入门小案例

SpringBoot入门小案例

2、下面来看一下项目结构:

SpringBoot入门小案例

3、pom.xml 配置jar包

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.12.RELEASE</version>
<relativePath />
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

4、创建一个SpringBoot项目运行的启动类

package com.cppdy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

5、创建一个controller(@Controller只返回页面;@RestController返回json格式的数据)

package com.cppdy.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloWordController { @RequestMapping("hello")
public String hello() { return "HelloWord";
} }

6、到这一步项目就配置好了打开浏览器访问 http://localhost:8080/hello,请注意访问不需要加项目名,如果要加需要在application.properties文件中另行配置

SpringBoot入门小案例