一、搭建环境
1.下载安装IDEA
2.安装maven,将maven集成到IDEA
二、开始
先贴个项目目录
1.使用maven创建一个空的project
File——new——project——Maven——按照提示填写,一路next下去......
2.打开pom.xml,添加一个<parent>,再添加一个web依赖。pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.springboot</groupId>
<artifactId>springtest</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3.新建一个application.properties,这是一个用来配置的文件
#项目命名空间
server.context-path=/demo
#端口号
server.port=8090
4.新建一个springboot入口类,Application.java,代码如下
package config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "controller")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
说明:@SpringBootApplication:这个注解表示这是springboot的入口
@ComponentScan(basePackages = "controller"):这个它会去controller包下找与浏览器中你输入的路径相匹配的方法。(不写的话会找不到页面)
5.新建一个controller类,HelloController.java,代码如下
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller
@ResponseBody
public class HelloController {
@RequestMapping(value = {"/hello"})
public String hello()
{
return "hi boy";
}
}
6.运行application.java。打开浏览器输入“http://localhost:8090/demo/hello”即可访问。