SpringBoot 入门例子

来源:互联网 发布:淘宝店怎么做链接地址 编辑:程序博客网 时间:2024/06/05 09:41

官方地址:http://projects.spring.io/spring-boot/

1、创建Maven项目MySpringBoot,结构如下:

Eclipse、Tomcat8、JDK8......


2、pom.xml 配置如下,有些配置不是必须:

<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">  <modelVersion>4.0.0</modelVersion>  <groupId>MySpringBoot</groupId>  <artifactId>MySpringBoot</artifactId>  <version>0.0.1-SNAPSHOT</version>     <!-- Inherit defaults from Spring Boot -->      <parent>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-parent</artifactId>          <version>1.4.0.BUILD-SNAPSHOT</version>      </parent>        <!-- Add typical dependencies for a web application -->      <dependencies>          <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-web</artifactId>          </dependency>          <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-tomcat</artifactId>  <version>2.0.0.M3</version></dependency>            </dependencies>          <!-- Package as an executable jar -->      <build>       <pluginManagement>        <plugins>              <plugin>                  <groupId>org.springframework.boot</groupId>                  <artifactId>spring-boot-maven-plugin</artifactId>              </plugin>                         <plugin>  <!-- Maven JDK 1.8编译 --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin>        </plugins>         </pluginManagement>     </build>                 <!-- Add Spring repositories -->      <!-- (you don't need this if you are using a .RELEASE version) -->      <repositories>          <repository>              <id>spring-snapshots</id>              <url>http://repo.spring.io/snapshot</url>              <snapshots><enabled>true</enabled></snapshots>          </repository>          <repository>              <id>spring-milestones</id>              <url>http://repo.spring.io/milestone</url>          </repository>      </repositories>      <pluginRepositories>          <pluginRepository>              <id>spring-snapshots</id>              <url>http://repo.spring.io/snapshot</url>          </pluginRepository>          <pluginRepository>              <id>spring-milestones</id>              <url>http://repo.spring.io/milestone</url>          </pluginRepository>      </pluginRepositories>     </project>


3、Application类,作为入口:

package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {   public static void main(String[] args) {SpringApplication.run(Application.class, args); }}

4、ExampleController 类:

package com.example;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@EnableAutoConfigurationpublic class ExampleController {@RequestMappingpublic String home(){return "SpringBoot is starting......";}@RequestMapping("/go/{name}")public String index(@PathVariable String name){return "Go To "+name+"!!!!";}}

5、运行入口类Application,Run As---Java Application,Console正常如下:




6、出现问题展现及解决:

ERROR:  Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

Solve : 在Spring Boot的入口类Application加上@SpringBootApplication.





原创粉丝点击