#
1、SpringBoot2.xHTTP請求配置講解
簡介:SpringBoot2.xHTTP請求註解講解和簡化註解配置技巧
1、@RestController and @RequestMapping是springMVC的註解,不是springboot特有的
2、@RestController = @Controller+@ResponseBody
3、@SpringBootApplication =@Configuration+@EnableAutoConfiguration+@ComponentScan
localhost:8080
2、開發接口必備工具之PostMan接口調試工具
簡介:模擬Http接口測試工具PostMan安裝和講解
1、接口調試工具安裝和基本使用
2、下載地址:https://www.getpostman.com/
3、SpringBoot基礎HTTP接口GET請求
簡介:springboot接口,http的get請求,各個註解使用
1、GET請求
1、單一參數@RequestMapping(path = “/{id}”, method = RequestMethod.GET)
1) public String getUser(@PathVariable String id ) {}
2)@RequestMapping(path = “/{depid}/{userid}”, method = RequestMethod.GET) 可以同時指定多個提交方法, getUser(@PathVariable(“depid”) String departmentID,@PathVariable(“userid”) String userid)
3)一個頂倆
- @GetMapping = @RequestMapping(method = RequestMethod.GET)
- @PostMapping = @RequestMapping(method = RequestMethod.POST)
- @PutMapping = @RequestMapping(method = RequestMethod.PUT)
- @DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
4)@RequestParam(value = “name”, required = true)
- 可以設置默認值,比如分頁
5)@RequestBody 請求體映射實體類
- 需要指定http頭爲 content-type爲application/json charset=utf-8
6)@RequestHeader 請求頭,比如鑑權
- @RequestHeader(“access_token”) String accessToken
7)HttpServletRequest request自動注入獲取參數
4、SpringBoot基礎HTTP接口POST,PUT,DELETE請求
簡介:http請求post,put, delete提交方式
5、常用json框架介紹和Jackson返回結果處理
簡介:常用json框架和註解的使用,自定義返回json結構和格式
1、常用框架 阿里 fastjson,谷歌gson等
JavaBean序列化爲Json,性能:Jackson > FastJson > Gson > Json-lib 同個結構
Jackson、FastJson、Gson類庫各有優點,各有自己的專長
空間換時間,時間換空間
2、jackson處理相關自動
指定字段不返回:@JsonIgnore
指定日期格式:@JsonFormat(pattern=”yyyy-MM-dd hh:mm:ss”,locale=”zh”,timezone=”GMT+8”)
空字段不返回:@JsonInclude(Include.NON_NUll)
指定別名:@JsonProperty(安全性考慮)
6、SpringBoot2.x目錄文件結構
簡介:SpringBoot目錄文件結構和官方推薦的目錄規範
1、目錄
src/main/java:存放代碼
static: 存放靜態文件,比如 css、js、image, (訪問方式 http://localhost:8080/js/main.js)
templates:存放靜態頁面jsp,html,tpl
config:存放配置文件,application.properties
resources:
src/main/resources
2、引入依賴 Thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
注意:如果不引人這個依賴包,html文件應該放在默認加載文件夾裏面,比如resources、static、public這個幾個文件夾,纔可以訪問
3、同個文件的加載順序,靜態資源文件
Spring Boot 默認會挨個從META/resources > resources > static > public 裏面找是否存在相應的資源,如果有則直接返回。
4、默認配置
2)spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
5、大型靜態資源文件最好存儲在CDN
7、SpringBoot2.x文件上傳
簡介:HTML頁面文件上傳和後端處理,springboot文件上傳 MultipartFile file,源自SpringMVC
1)靜態頁面直接訪問:localhost:8080/index.html
- 注意點:如果想要直接訪問html頁面,則需要把html放在springboot默認加載的文件夾下面
2)MultipartFile 對象的transferTo方法,用於文件保存(效率和操作比原先用FileOutStream方便和高效)
訪問路徑 http://localhost:8080/images/39020dbb-9253-41b9-8ff9-403309ff3f19.jpeg
8、jar包方式運行web項目的文件上傳和訪問處理
簡介:SpingBoot2.x使用 java -jar運行方式的圖片上傳和訪問處理
1、文件大小配置,啓動類裏面配置
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//單個文件最大
factory.setMaxFileSize("10240KB"); //KB,MB
/// 設置總上傳數據總大小
factory.setMaxRequestSize("1024000KB");
return factory.createMultipartConfig();
}
2、打包成jar包,需要增加maven依賴
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果沒加相關依賴,執行maven打包,運行後會報錯:no main manifest attribute, in XXX.jar
GUI:反編譯工具,作用就是用於把class文件轉換成java文件
3、文件上傳和訪問需要指定磁盤路徑
application.properties中增加下面配置
1) web.images-path=/Users/jack/Desktop
2) spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path}
4、文件服務器:fastdfs,阿里雲oss,nginx搭建一個簡單的文件服務器