使用SpringBoot請求方式和訪問靜態(tài)頁面
SpringBoot請求方式
1.get請求
@RequestMapping(value = "/findUser",method = RequestMethod.GET)
public UserInfo findUser(){
return new UserInfo("1","張三","123","男","22");
}2.restful風格
@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.添加數據
@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 同個結構
Jackson、FastJson、 Gson類庫各有優(yōu)點,各有自己的專長
空間換時間,時間換空間
Jackson處理相關自動
- 指定字段不返回:@JsonIgnore
- 指定日期格式:@JsonFormat (pattern="yyy-MM-dd hh:mm:ss" , locale=" zh" , timezone="GMT+8" )
- 空字段不返回:@JsonInclude(Include . NON_NULL)
- 指定別名:@JsonProperty
SpringBoot目錄文件結構
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文件應該放在默認加載文件夾里面,比如resources、static. public這個幾個文件夾,才可以訪問。
3、同個文件的加載順序,靜態(tài)資源文件
Spring Boot 默認會挨個從META/resources > resources > static > public里面找是 否存在相應的資
源,如果有則直接返回。
4、默認配置
1)官網地址: 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里面添加以下配置進行靈活訪問
spring.resources.static-locations = classpath:classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/page/
最后在Controller控制層中進行訪問
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";
}
}總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java連接數據庫JDBC技術之prepareStatement的詳細介紹
這篇文章主要介紹了Java連接數據庫JDBC技術之prepareStatement的詳細介紹,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
SpringBoot favicon Chrome設置問題解決方案
在本篇文章里小編給大家分享的是關于SpringBoot favicon Chrome設置問題實例內容,小的朋友們可以參考學習下。2020-02-02

