首页 > 学院 > 开发设计 > 正文

springboot构建微服务

2019-11-06 09:14:50
字体:
来源:转载
供稿:网友

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项目。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表