Spring-boot 入门之第一个demo
环境约束
–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 file-new file-spring initializr
选择jdk版本 1.8,然后下一步 输入项目信息
下一步,选择最简单的web
下一步,输入项目名,选择项目在本地保存的位置
最后finish 完成项目构建。
代码部分:
POM.XML 引入spring-boot 需要的依赖和maven打包的插件,代码如下:
<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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
新建一个class作为spring-boot的启动类,代码如下:
@SpringBootApplication public class HelloWorldMainApplicaion { public static void main(String[] args) { SpringApplication.run(HelloWorldMainApplicaion.class,args); } }
新建一个controller,代码如下:
@Controller public class HelloController { @ResponseBody @RequestMapping("/hello") public String hello(){ return "Hello World"; } }
启动项目:
浏览器访问刚刚的项目: