webpack4.0各個擊破(3)—— Assets篇
1029
2025-03-31
http請求中的參數
本文以前端框架jquery.ajax和后端標準JAX-RS、SpringMVC為例,介紹如何在前后端通過http request消息傳遞參數。
http請求消息分為消息頭header和消息體body。放在body中的數據只有一種,其余都是放在header中的不同位置。
HTTP請求可以文本顯示,各參數以回車分割。
如:
一般根據參數在http中的位置的不同,置值和取值的方法也不一樣。下面將分別介紹。
header
數據在header中,并單獨占header的一個屬性。header中除了HTTP標準字段,也可以自己添加非標準字段。
前端:
$.ajax({
url: "/getHeader",
type: "GET",
header: {
token: "11111"
}
});
后端:
JAX-RS
@GET
@Path("/getHeader")
public String getHeader(@HeaderParam(HttpHeaders.token) String token) {}
SpringMVC
@GET
@Path("/getHeader")
public String getHeader(@RequestHeader("token") String token) {}
擴展
HTTP頭(Request)中的標準字段如下(摘自維基百科)
cookie
數據在header的cookie中,cookie是發送HTTP請求時客戶端會自動附上的數據。
前端:
$.ajax({
url: "/getCookie",
type: "GET"
})
cookie中的數據
sessionId=415A4AC178C59DACE0B2C9CA727CDD84
后端:
JAX-RS
@GET
@Path("/getCookie")
public String getCookie(@CookieParam("sessionId") String sessionId) {}
SpringMVC
@GET
@Path("/getCookie")
public String getCookie(@CookieValue("sessionId") String sessionId) {}
path
數據在url中。
前端:
$.ajax({
url: "/getUserById/100/Ann",
type: "GET"
})
后端:
JAX-RS
@GET
@Path("/getUserById/{id}/{name}")
public String getUserInfoId(@PathParam("id") int id,
@PathParam("name") String name) {}
SpringMVC
@GET
@Path("/getUserById/{id}/{name}")
public String getUserInfoId(@PathVariable("id") int id,
@PathVariable("name") String name) {}
query
數據在url中,以?和&區隔。
前端:
$.ajax({
url: "/getUserByName?name=Ann",
type: "GET"
})
后端:
JAX-RS
@GET
@Path("/getUserByName")
public String getUserInfo(@DefaultValue("fei")@QueryParam("name") String name) {}
SpringMVC
@GET
@Path("/getUserByName")
public String getUserInfo(@RequestParam(value="name", required=false) String name) {}
matrix
數據在url中,以;區隔。
矩陣參數適用于特定路徑元素,而查詢參數作為整體應用于請求.當對多級資源和子資源進行復雜的REST風格查詢時,這種情況就會發生:
http://example.com/res/categories;name=foo/objects;name=green/?page=1
前端:
$.ajax({
url: "/getMatrix;name=Ann;address=Shanghai",
type: "GET"
});
后端:
JAX-RS
@GET
@Path("/getMatrix")
public String getMatrix(@MatrixParam("name") String name,
@MatrixParam("address") String address) {}
SpringMVC
@GET
@Path("/getMatrix")
public String getMatrix(@MatrixVariable("name") String name,
@MatrixVariable("address") String address) {}
body
一個http請求只能有一個消息體(request body),因此該參數在一個請求中只有一個。
body中的數據支持多種格式,常用的有application/x-www-form-urlencoded、application/json 、application/xml等。
后端:
JAX-RS
若數據類型是form,用@FormParam 。
SpringMVC
若數據類型是form,用@RequestParam 。
若數據類型不是form,用@RequestBody 。
其他
獲取上下文中的HttpServletRequest、HttpServletResponse等對象。
后端:
JAX-RS
用@Context注解。
public String demo(@Context HttpServletRequest httpServletRequest)
The object instances that it can inject are the following:
SecurityContext – Security context instance for the current HTTP request
Request – Used for setting precondition request processing
Application, Configuration, and Providers -> Provide access to the JAX-RS application, configuration, and providers instances
ResourceContext – Resource context class instances
ServletConfig – The ServletConfig instance instance
ServletContext – The ServletContext instance
HttpServletRequest – The HttpServletRequest instance for the current request
HttpServletResponse – The HttpServletResponse instance for the current request
HttpHeaders – Maintains the HTTP header keys and values
UriInfo – Query parameters and path variables from the URI called
開發者
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。