使用SpringBoot請求方式和訪問靜態(tài)頁面
SpringBoot請求方式
1.get請求
@RequestMapping(value = "/findUser",method = RequestMethod.GET) public UserInfo findUser(){ return new UserInfo("1","張三","123","男","22"); }
2.restful風(fēng)格
@GetMapping(value = "/rest/{id}/{name}") public Map findObject(@PathVariable(value = "id") String id, @PathVariable(value = "name") String name){ map.clear(); map.put("id",id); map.put("name",name); return map; }
3.分頁列表
@GetMapping(value = "/page") public Map pages(@RequestParam(name = "page",defaultValue = "1")Integer page,Integer limit){ map.clear(); map.put("page",page); map.put("limit",limit); return map; }
4.添加數(shù)據(jù)
@PostMapping(value = "/add") public Map addUser(UserInfo ui){ map.clear(); map.put("ui",ui); return map; }
5.token形式
@GetMapping(value = "/findheader") public Map findHeader(@RequestHeader("token")String token,String id){ map.clear(); map.put("token",token); map.put("id",id); return map; }
6.查找信息
@GetMapping(value = "/find") public Map findHeader(HttpServletRequest request){ map.clear(); String id = request.getParameter("id"); map.put("id",id); return map; }
7.登錄
@PostMapping(value = "/login") public Map login(String name ,String pwd){ map.clear(); map.put("name",name); map.put("pwd",pwd); return map; }
8.修改信息
@PutMapping(value = "/update") public Map update(String name){ map.clear(); map.put("name",name); return map; }
9.刪除信息
@DeleteMapping(value = "/delete") public Map delete(String id){ map.clear(); map.put("id",id); return map; }
常用框架阿里fastjson, 谷歌gson
JavaBean序列化為Json,性能: Jackson > FastJson > Gson > Json-lib 同個結(jié)構(gòu)
Jackson、FastJson、 Gson類庫各有優(yōu)點(diǎn),各有自己的專長
空間換時間,時間換空間
Jackson處理相關(guān)自動
- 指定字段不返回:@JsonIgnore
- 指定日期格式:@JsonFormat (pattern="yyy-MM-dd hh:mm:ss" , locale=" zh" , timezone="GMT+8" )
- 空字段不返回:@JsonInclude(Include . NON_NULL)
- 指定別名:@JsonProperty
SpringBoot目錄文件結(jié)構(gòu)
1、目錄講解
- src/main/java:存放代碼
- src/main/resources
- static:存放靜態(tài)文件,比如css、 js、 image, (訪問方式 http://localhost:8080/js/xxx.js)
- templates:存放靜態(tài)頁面jsp, html,tpl
- config:存放配置文件, application. properties
resources:
2、引入依賴 Thymelesf
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
如果不引人這個依賴包,html文件應(yīng)該放在默認(rèn)加載文件夾里面,比如resources、static. public這個幾個文件夾,才可以訪問。
3、同個文件的加載順序,靜態(tài)資源文件
Spring Boot 默認(rèn)會挨個從META/resources > resources > static > public里面找是 否存在相應(yīng)的資
源,如果有則直接返回。
4、默認(rèn)配置
1)官網(wǎng)地址: https ://docs.spring.io/spring-boot/docs/current/reference/html/
boot-features -developing-web- applications . html#boot - features- spring-mvc-static-content
2)spring.resources. static-locations = classpath: /META- INF/resources/ , classpath: /resources/ ,classpath:/static/ , classpath:/public/
Spring Boot訪問靜態(tài)資源
在瀏覽器中直接輸入 localhost:8080/a.html 會去訪問 static 文件夾下的靜態(tài)資源
如果想要訪問到 templates 下的靜態(tài)資源有兩種方式,但是都要在pom.xml里面去引入Thymelesf依賴。
1)在application.properties里面添加以下配置定死
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html
2)在application.properties里面添加以下配置進(jìn)行靈活訪問
spring.resources.static-locations = classpath:classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/page/
最后在Controller控制層中進(jìn)行訪問
package org.zhx.bootdemo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class TestController { @GetMapping("/finds") public String find(){ return "a"; } @GetMapping("/findss") public String finds(){ return "ab.html"; } }
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MAVEN_HOME、M2_HOME,maven環(huán)境變量設(shè)置方式
這篇文章主要介紹了MAVEN_HOME、M2_HOME,maven環(huán)境變量設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Spring Boot2.0 @ConfigurationProperties使用詳解
這篇文章主要介紹了Spring Boot2.0 @ConfigurationProperties使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11Springboot整合mybatis開啟二級緩存的實(shí)現(xiàn)示例
在一級緩存中,是查詢兩次數(shù)據(jù)庫的,顯然這是一種浪費(fèi),既然SQL查詢相同,就沒有必要再次查庫了,直接利用緩存數(shù)據(jù)即可,這種思想就是MyBatis二級緩存的初衷,本文就詳細(xì)的介紹了Springboot整合mybatis開啟二級緩存,感興趣的可以了解一下2022-05-05詳解Java并發(fā)包中線程池ThreadPoolExecutor
ThreadPoolExecutor是Java語言對于線程池的實(shí)現(xiàn)。線程池技術(shù)使線程在使用完畢后不回收而是重復(fù)利用。如果線程能夠復(fù)用,那么我們就可以使用固定數(shù)量的線程來解決并發(fā)問題,這樣一來不僅節(jié)約了系統(tǒng)資源,而且也會減少線程上下文切換的開銷2021-06-06半小時實(shí)現(xiàn)Java手?jǐn)]網(wǎng)絡(luò)爬蟲框架(附完整源碼)
最近在做一個搜索相關(guān)的項(xiàng)目,需要爬取網(wǎng)絡(luò)上的一些鏈接存儲到索引庫中,自己寫了一個簡單的網(wǎng)絡(luò)爬蟲,感興趣的可以了解一下2021-06-06JavaWeb中請求轉(zhuǎn)發(fā)和請求重定向的區(qū)別以及使用
今天帶大家學(xué)習(xí)JavaWeb的相關(guān)知識,文章圍繞著JavaWeb中請求轉(zhuǎn)發(fā)和請求重定向的區(qū)別以及使用展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06Java中設(shè)置session超時(失效)的三種方法
這篇文章主要介紹了Java中設(shè)置session超時(失效)的三種方法,本文講解了在web容器中設(shè)置、在工程的web.xml中設(shè)置、通過java代碼設(shè)置3種方法,需要的朋友可以參考下2015-07-07