springboot入门

添加依赖

创建 maven 工程,在 pom.xml 文件中添加如下依赖:

<!-- 定义公共资源版本 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath /> 
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <!-- 上边引入 parent,因此 下边无需指定版本 -->
    <!-- 包含 mvc,aop 等jar资源 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

 

 

package com.hust.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
该注解指定项目为springboot,由此类当作程序入口
自动装配 web 依赖的环境
**/
@SpringBootApplication
public class App {
    
    public static void main(String[] args) {
        //主函数运行springboot项目
        SpringApplication.run(App.class, args);
    }
}

 

package com.hust.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@EnableAutoConfiguration
// 表示所有接口返回json格式
@RestController
public class HelloWorldController {

    @RequestMapping("/index")
    public String index() {
        return "success";
    }

    @RequestMapping("/getMap")
    public Map<String, Object> getMap() {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("errorCode", "200");
        result.put("errorMsg", "ok");
        return result;
    }
}

 

run as Java application

优势:不需要各种繁琐的xml配置、内置了tomcat。非常方便

热部署

当我们修改文件和创建文件时,都需要重新启动项目。这样频繁的操作很浪费时间,配置热部署可以让项目自动加载变化的文件,省去的手动操作。

<!-- 热部署 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    <scope>true</scope>
</dependency>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- 没有该配置,devtools 不生效 -->
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

 

 

报错:Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:repackage (default) on project fb: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:repackage failed: Unable to find main class -> 

解决办法:把spring boot 插件去掉就好了

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

原因不明

 

posted @ 2018-05-08 09:16  牧 天  阅读(116)  评论(0)    收藏  举报