1. 程式人生 > >springboot 的 繼承 和 匯入

springboot 的 繼承 和 匯入

1.繼承 springboot

pom:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <!-- 父專案的pom.xml檔案的相對路徑; 預設值是../pom.xml。-->
        <relativePath
/>
</parent> <!--會被子模組繼承該依賴--> <dependencies> <!-- spring-boot的web啟動的jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </
dependency
>
</dependencies>

2.引入 springboot

pom:

    <!--宣告依賴,子專案若沒有顯示宣告,不會被引入-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>
org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- spring-boot的web啟動的jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>

3.啟動類

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);
    }
}

@SpringBootApplication:等同於@Configuration、@EnableAutoConfiguration、@ComponentScan三個註解。

4.測試類

@Controller
@RequestMapping(value = "/demo")
public class DemoController {

    @RequestMapping(value = "/test1", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody //返回結果不會被解析為跳轉路徑,而是直接寫入HTTP 響應正文中(ResponseBody)中
    public Map test1(){
        Map respMap = new HashMap();
        respMap.put("code","007");
        respMap.put("message","測試demo1");

        return  respMap;
    }
}