构建微服务:快速搭建Spring Boot项目
Spring Boot简介:
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者(官网介绍)。
Spring Boot特点:
1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置 4. 自动配置Spring 5. 提供生产就绪型功能,如指标,健康检查和外部配置 6. 绝对没有代码生成并且对XML也没有配置要求 快速入门: 1、访问http://start.spring.io/ 2、填写相关的项目信息、jdk版本等(可参考下图)3、点击Generate Project,就会生成一个maven项目的压缩包,下载项目压缩包
4、解压后,使用eclipse,Import -> Existing Maven Projects -> Next ->选择解压后的文件夹-> Finsh
项目结构介绍:
如下图所示,Spring Boot的基础结构共三个文件:
src/main/java --程序开发以及主程序入口
src/main/resources --配置文件
src/test/java --测试程序
Spring Boot推荐的项目结构:
根目录:com.example.myproject
1)domain:实体类(com.example.domain)
2)Dao:数据访问层(com.example.repository)
3)Service:数据服务接口层(com.example.service)
ServiceImpl:数据服务实现层(com.example.service.impl)
4)Controller:前端控制器(com.example.controller)
5)utils:工具类(com.example.utils)
6)constant:常量接口类(com.example.constant)
7)config:配置信息类(com.example.config)
8)dto:数据传输对象(Data Transfer Object,用于封装多个实体类(domain)之间的关系,不破坏原有的实体类结构)(com.example.dto)
9)vo:视图包装对象(View Object,用于封装客户端请求的数据,防止部分数据泄露,保证数据安全,不破坏原有的实体类结构)(com.example.vo)
引入Web模块:
在pom.xml添加支持Web的模块
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency>
运行项目:
1、创建controller
1 package com.example.annewebsite_server.controller; 2 3 import org.springframework.web.bind.annotation.GetMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 6 @RestController 7 public class HelloController { 8 @GetMapping("/hello") 9 public String say(){ 10 return "Hello Spring Boot!"; 11 } 12 }
2、启动项目入口
3、项目启动成功
4、在浏览器中进行访问(http://localhost:8080/hello)
以上是一个Spring Boot项目的搭建过程,希望能够给正在学习Spring Boot的同仁带来一些些帮助,不足之处,欢迎指正。
- springboot 构建 spring cloud 微服务项目 搭建ARTHUR框架分享
- JAVAEE——SpringBoot入门:简介、微服务、环境准备、helloworld与探究、快速构建项目
- Spring Boot(二):快速搭建web项目或微服务
- SpringBoot快速构建微服务架构体系
- Spring Boot+JPA+Mysql+ThymeLeaf快速构建CURD系统(二)搭建SpringBoot工程
- Spring Boot+JPA+Mysql+ThymeLeaf快速构建CURD系统(二)搭建SpringBoot工程
- springBoot maven快速构建项目
- spring-boot(2) - RESTfull API简单项目的快速搭建
- Spring Boot+JPA+Mysql+ThymeLeaf快速构建CURD系统(二)搭建SpringBoot工程
- Spring Boot+JPA+Mysql+ThymeLeaf快速构建CURD系统(二)搭建SpringBoot工程
- SpringBoot1-spirng项目快速搭建+maven+spring的基本配置
- 快速构建可运行的Spring-boot项目(Hello World)
- Spring Boot 学习(一) 快速搭建SpringBoot 项目
- springboot+maven快速构建项目的示例代码
- SpringBoot构建微服务实战 之 服务注册的快速打包
- 快速搭建Spring Boot项目
- Spring Boot+JPA+Mysql+ThymeLeaf快速构建CURD系统(二)搭建SpringBoot工程
- Spring Boot+JPA+Mysql+ThymeLeaf快速构建CURD系统(二)搭建SpringBoot工程
- 快速构建可运行的Spring-boot项目(Hello World)