Spring Boot学习---1.快速构建mvc模式项目

来源:互联网 发布:网络调试助手cm精装版 编辑:程序博客网 时间:2024/05/16 09:26

 Spring越来越强大了,因工作中都开始使用spring boot快速搭建项目,且去除xml配置,发觉使用springBoot后项目开发非常迅速,工作之余想自己从头了解整个springBoot,因此记下学习记录

一、引入springBoot

对应在pom文件中加入如下代码:

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

      <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>     </dependency>

只需要加上以上代码,在parent中引入parent的version,在依赖中不需要指定对应web的version,springBoot会自动匹配对应需要的依赖包和版本,完成以上引入后就可以写代码了~


二、写controller类

如下:

import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@RestController@EnableAutoConfigurationpublic class Example {    @RequestMapping("/test")    String home() {        return "Hello World!";    }    public static void main(String[] args) throws Exception {        SpringApplication.run(Example.class, args);    }}
写完代码后,就可以直接运行man方法也就是eclipse中的 run as -- java application,此时spring-boot会使用内置依赖的tomcat启动程序,默认端口是8080,启动成功然后在浏览器中访问 http://localhost:8080/test  就能看到 “hello world” 了

三、注意

在启动中第一次启动会有以下报错信息:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactoryat org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE]at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]at test.api.controller.Application.main(Application.java:22) [classes/:na]

出现这个错误的原因是缺少tomcat-juli.jar包,需要在pom文件中引入,解决办法如下:

                <dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-juli</artifactId></dependency>


再次启动,成功


0 0
原创粉丝点击