1.创建一个Maven项目导入相应的依赖
<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.xx</groupId>
<artifactId>springboot-sgg</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 导入SpringBoot的父依赖,由他来管理依赖版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.创建一个主程序类,这里需要注意我的这个主程序类是放在com.xx这个包下的,会自动扫描com.xx下的所有包
package com.xx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
* @SpringBootApplication用于标注一个主程序类,其底层@Import注解会将主配置类所在包下的所有子包里面的所有组件扫描到Spring容器中
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring启动入口
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
3.写一个Controller用来测试
package com.xx.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "HelloWorld!";
}
}
4.运行主程序类,输入http://localhost:8080/hello