配置完成以后部署 tomcat 调试,如今很是流行微服务,若是我这个项目仅仅只是须要发送一个邮件,或者个人项目仅仅是生产一个积分,我都须要这样折腾一遍,想一想就很累!mysql
spingboot 建议的目录结果以下:web
采用默认配置能够省去不少配置,固然也能够根据本身的喜欢来进行更改最后,启动 main 方法,至此一个项目搭建好了!redis
官方的构建工具很是舒服,下面我选择本身建立一个maven项目,本身作配置,个人项目结构以下:spring
1.pom.xml 中添加支持 web 的模块:sql
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springboot</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
2.编写 controller 内容:mongodb
package com.bdqn.zmj.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HellController { @RequestMapping("/hello") public String index() { return "Hello World"; } }
@RestController 的意思就是 controller 里面的方法都以 json 格式输出,是controller和responbody的结合体数据库
3.启动类apache
package com.bdqn.zmj.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan("com.bdqn.zmj") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
四、启动 main 方法,打开浏览器访问 http://localhost:8080/hello 就能够看到效果了!json
你们不要慌张,你可能会碰见如下问题!!!你输入好你的配置路径后怎么也访问不到hello word气不气,你说浏览器
你们在建立项目的时候必定要注意两个点!!按照下面的方式问题获得解决
/**
* 当测试启动类和Controller不位于同一个包下面时候须要
* 在application启动类里面配置@ComponentScan(basePackages = {"com.bdqn.Controller"})去扫描controller的路径
*/
按照建立maven项目同样,选择图下面画框的部分
接下你能够选择一些你须要导入的组件,springboot会自动把你把依赖导入进来,一直点击下一步直完成
这个时候你点击启动类,application的话,会报错提示以下
Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. //没法配置数据库,没有指定url属性,而且没法配置embedded datasource Reason: Failed to determine a suitable driver class //缘由:没法明确指定正确的驱动类(driver.class) Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active). //建议: //若是若是须要加载嵌入式的数据库,请将他放入路径中 //若是有数据库设置须要从指定配置文件中加载,须要调用该配置文件(目前没有活动的配置文件)
发现只由于有pom文件的修改致使项目中增长的mysql、redis、es、mongodb的依赖包的导入,须要添加新的database配置文件,这个问题要解决也很简单,只须要在@SpringBootApplication或者@EnableAutoConfiguration注解后面加上(exclude= {DataSourceAutoConfiguration.class})就OK了,意思就是数据库的相关信息我没配,你也别去找了.....
<!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-web</artifactId>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.mybatis.spring.boot</groupId>--> <!--<artifactId>mybatis-spring-boot-starter</artifactId>--> <!--<version>1.3.2</version>--> <!--</dependency>-->
程序入口处: @SpringBootApplication public class DemoApplication { 修改成: @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //该注解的做用是,排除自动注入数据源的配置(取消数据库配置),通常使用在客户端(消费者)服务中 public class DemoApplication {