SpringBoot+IDEA+Maven快速入门
2016-11-09 20:14
573 查看
一、首先创建一个Maven项目,比较简单
二、在pom文件中添加依赖
如下:<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Springboot</artifactId> <packaging>war</packaging> <name>Springboot Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
三、开始写Java代码
1、首先创建一个Application类,里面添加一个main()方法,@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
需要注意的是:这个类的上面需要添加SpringBoot的注解@SpringBootApplication,然后在主函数中开始启动。
2、然后创建一个HelloWord类,用来测试SpringBoot
如下:
@RestController @EnableAutoConfiguration public class HelloWord { @RequestMapping("/hello") public String hello() { return "hello,Spring boot!"; } //带参数 @RequestMapping("/word/{name}") public String word(@PathVariable String name) { return "word--spring boot:" + name; } }
需要注意的是这上面的几个注解:
@RestController:表示这是个控制器,和Controller类似
@EnableAutoConfiguration:springboot没有xml配置文件因为这个注解帮助我们干了这些事情,有了这个注解springboot启动的时候回自动猜测你的配置文件从而部署你的web服务器
@RequestMapping(“/hello”):这个和SpringMvc中的类似了。
@PathVariable:参数
Ok,就这样,一个简单的SpringBoot小demo就写出来了,我们直接就可以运行了,然后输入:http://localhost:8080/hello 和 http://localhost:8080/word/canshu 浏览器上即可得到输出结果
源码下载
相关文章推荐
- SpringBoot快速入门(非maven)
- IDEA +Maven+Springboot入门
- idea快速创建maven集成spring-boot项目
- Spring Boot 快速入门
- Spring Boot快速入门之Hello World
- SpringBoot初识和快速入门(一)
- Spring Boot 快速入门
- Spring Boot 快速入门
- Spring Boot 快速入门
- SpringBoot入门(maven为例)
- 转-Spring Boot 快速入门
- springBoot maven快速构建项目
- Spring Boot快速入门
- 使用spring boot快速构建spring框架的maven项目
- Maven快速使用教程(二) spring boot 项目构建
- 微框架 - spring boot快速入门
- Spring Boot 快速入门
- Spring Boot 快速入门
- Spring Boot 快速入门
- Spring快速入门教程 - 1 - 使用 Maven 创建工程 - 修改0次