1. 程式人生 > >spring boot(一):詳細入門篇

spring boot(一):詳細入門篇

關於springboot這裡就不多說了,現在大部分企業都是用的它,想必聽過了,這裡是我自學時的筆記,來做一下分享

  • 這裡說一下使用springboot的優勢:
  1. 敏捷開發、開箱即用,提供各種預設配置來簡化專案配置
  2. tomcat內嵌式容器簡化Web專案
  3. 使用了maven的依賴管理,減少了冗餘程式碼生成和XML配置的要求
  • 今天這裡主要目標就是完成Spring Boot基礎專案的構建,實現一個簡單的HTTP請求

//首相介紹一下開發環境

Java1.8及以上

Spring Framework 4.1.5及以上

本文采用Java 1.8.0_73

Spring Boot 2.0.0除錯通過

  • 開啟eclipse使用maven建立com.test.springboot工程 ,這裡注意Packaging使用jar

 

建立成功以後,找到pom,xml檔案,加入以下依賴

然後點選專案右鍵maven>updateProject更新一下,避免報錯

    <!--繼承父級依賴 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    
    <dependencies>
      <!--SpringBoot web 元件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

 在專案src/main/java中建立com.test.applicationcom.test.controller

 在application中建立springboot啟動器MyTestApplication

 在controller中建立請求介面UserController類  

程式碼如下:

MyTestApplication.java

package com.test.application;

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

/**
 * 
  * @ClassName: MyTestApplication 
  * @Description: TODO
  * @author chen 
  * @date 2018年9月26日 上午9:26:56 
  * @EnableAutoConfiguration 只掃面當前類
  * @RestController 以json格式返回資料
 */
@EnableAutoConfiguration
@RestController
@ComponentScan("com.test.controller")
public class MyTestApplication {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(MyTestApplication.class, args);
	}

}

UserController.java

package com.test.controller;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

	@RequestMapping("/userTest")
	public Map<String, Object> userTest(){
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("userName", "admin");
		map.put("passWord", "123456");
		return map; 
	}
}

src/main/resouces中建立File檔案applapplication.properties在裡面自定義tomcat埠號

#\u524d\u7aef\u7aef\u53e3
##自定義埠號
server.port=8090
#\u6700\u5927\u8fde\u63a5\u6570
server.tomcat.max-connections=200
#\u6700\u7ebf\u7a0b\u6570
server.tomcat.max-threads=300
#\u7f16\u7801\u65b9\u5f0f
server.tomcat.uri-encoding=UTF-8
#post\u63d0\u4ea4\u6570\u636e\u6700\u5927\u5927\u5c0f\uff0c\u8bbe\u7f6e\u4e3a0\u4e0d\u9650\u5236
server.tomcat.max-http-post-size=0

 最後去MytestApplicationRun as 啟動專案

訪問   http://localhost:8090/userTest  就Ok了

下面是一張結構圖 :

記得點一個贊哦,你的贊是小編成功的第一步

下一篇:springboot教程第二篇(整合靜態資源訪問)

原始碼下載:https://github.com/APassionMy/github.springboot.actual