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