第一步创建maven工程:
第二步,在pom文件添加parent标签,加入web依赖
parent标签:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
web依赖(自动整合spring和springmvc):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
第三部,创建启动类:
package cn.sjxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//告诉服务器,这个类是启动类
public class MySpringBootApplication {
public static void main(String[]args){
//调用springApplication的run方法,启动springboot
SpringApplication.run(MySpringBootApplication.class);
}
}
点击
运行
结果
看到tomcat启动成功,前端控制器加载完毕
第四步,创建控制层的类,在页面上显示效果
package cn.sjxy.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody//将return的结果转换为json数据
public class TestController {
@RequestMapping("/dog")
public String he(){
return "helloworld!";
}
}
运行结果
完毕