SpringBoot(目標半天入門,學習總結,HelloWorld)

1. 什麼是**SpringBoot**

Spring Boot 是由 Pivotal 團隊提供的全新框架,其設計目的是用來簡化新 Spring 應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot 致力於在蓬勃發展的快速應用開發領域(rapid

application development)成爲領導者。–摘自百度百科



加兩個依賴,無敵了,全部JAR包都弄好了

<?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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.tzy.app</groupId>
    <artifactId>springboot_helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--引入springBoot父類依賴-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    <dependencies>
        <!--springboot-wed組件 springmvc+spring+mybatis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

寫個接口吧

package com.tzy.springbootH;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * Created by 70416 on 2018/4/10.
 */
//表示開始注入spring容器,創建Tomcat,srping在加載
@EnableAutoConfiguration
//表示該接口全部返回的都是json格式(就是這個類裏面所有的方法都用了@ResponseBody)
@RestController
public class HelloWorldController {
   @RequestMapping("/indedx")
    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","成功");
        return result;
    }

    public static void main(String[] args) {
        //主函數的啓動入口,springboot是通過應用程序運行的
        SpringApplication.run(HelloWorldController.class,args);
    }
}

運行Main


啓動成功



完結

@EnableAutoConfiguration這個註解只掃面當前應用程序,如果有多個會暴端口號被佔用

那怎麼辦,接口不能寫多個咯?不可能

那我們拆一下。

添加一個類

package com.tzy.springbootH;

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

/**
 * Created by 70416 on 2018/4/10.
 */
@RestController
public class IndexController {
    @RequestMapping("/indexc")
    public  String index(){
        return "indexc";
    }
}

我們提供一個主應用入口

package com.tzy.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by 70416 on 2018/4/10.
 */
@ComponentScan("com.tzy.springbootH")
//表示開始注入spring容器,創建Tomcat,srping在加載
@EnableAutoConfiguration
public class APP {
    public static void main(String[] args) {
        //主函數的啓動入口,springboot是通過應用程序運行的
        SpringApplication.run(APP.class,args);
    }
}

啓動


發現都能查看到了哦.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章