您的位置:首页 > 编程语言 > Java开发

springboot构建微服务

2017-02-27 16:49 351 查看
Spring Boot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目。大多数SpringBoot项目只需要很少的配置文件。

下面开始创建一个最简单的springboot工程。

创建maven项目,引入依赖。

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>

<dependencies>
<!-- web支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Springboot 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>


创建springboot启动类Application

@SpringBootApplication //集成@Configuration,@EnableAutoConfiguration,@ComponentScan三个注解
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


创建服务控制类HelloController

@RestController //@RestController标注的类返回字符串到页面,@Controller返回指定的页面
public class HelloController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}


启动服务(运行main方法,springboot内置了自己的tomcat,所以不用部署到tomcat上),访问localhost:8080/hello可以看到页面返回了“Hello World”。至此就算构建成功了一个简单的springboot项目。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: