springboot初体验

来源:互联网 发布:nba2k16樱木捏脸数据 编辑:程序博客网 时间:2024/06/05 08:53

首先贴springboot的pom文件


<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>com.test</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>


  <name>springboot</name>
  <url>http://maven.apache.org</url>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


  
  
      <!-- spring boot 基本环境 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>


     <!--web应用基本环境配置 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


    <!-- 打包spring boot应用 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  
</project>



然后是启动类






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





控制器

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/index/{name}")
    @ResponseBody
    public String index(@PathVariable String name){
        
        if(null==name){
            name="boy";
        }
        
        return "hello world" +name;
    }
}




遇到的问题  

Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServlet


原因  未在启动类上加注解@SpringBootApplication  



最后启动成功后,访问

http://localhost:8080/index/aaa



修改默认的根路径和端口


  1. 在src/main/resources/目录下新建application.properties文件(如果已经有了则忽略这一步)
  2. 在application.properties添加下面配置 
    server.port=8088 
    server.context-path=/springboot