关于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了
下面是一张结构图 :
记得点一个赞哦,你的赞是小编成功的第一步