本系列博客都是基于maven项目的
环境约束
–jdk1.8:Spring Boot 推荐jdk1.7及以上;java version "1.8.0_112"
–maven3.x:maven 3.3以上版本;Apache Maven 3.3.9
–IntelliJIDEA2017:IntelliJ IDEA 2017.2.2 x64、STS
–SpringBoot 1.5.9.RELEASE:1.5.9;
1.首先要去网上现在maven,具体可以百度。然后打开给给maven 的settings.xml配置文件的profiles标签添加如下配置
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
-
给自己的idea配置maven (File->Setting-> Build, Execution, Deployment->BulidTools ->maven)
dd.png -
在pom.xml中导入spring boot相关的依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 编写一个主程序;启动Spring Boot应用
package com.flyz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
- 编写相关的Controller、Service
package com.flyz.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;
//@ResponseBody// 该注解标识这个类的所有方法返回的数据直接写给浏览器,(如果是对象,则会转成json数据)
//@Controller // RestController 等价于 @ResponseBody @Controller 同时写上
@RestController
public class HelloController {
@RequestMapping("/index")
public String hello(){
return "welcome to SpringBoot";
}
}
运行项目(App类)
JDFAN7_Z~U}8J}SE~~K9LIK.png值得注意的是:
RestController:代表是返回JSON字符串给前台
Controller:注解是返回页面(比如结合thymeleaf)
网友评论