關於springboot這裏就不多說了,現在大部分企業都是用的它,想必聽過了,這裏是我自學時的筆記,來做一下分享
- 這裏說一下使用springboot的優勢:
- 敏捷開發、開箱即用,提供各種默認配置來簡化項目配置
- tomcat內嵌式容器簡化Web項目
- 使用了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.application、com.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
最後去MytestApplication中Run as 啓動項目
訪問 http://localhost:8090/userTest 就Ok了
下面是一張結構圖 :
記得點一個贊哦,你的贊是小編成功的第一步