1. 程式人生 > >Jersey中QueryParam,FormParam註解以及引數接收測試

Jersey中QueryParam,FormParam註解以及引數接收測試

一、QueryParam,FormParam的區別:

1.Query是用於獲取get請求中的查詢引數

@GET
@Path("/findUser")
@Produces("text/plain;charset=utf-8")
public String findUser(@QueryParam("userId") String userId,
		@QueryParam("password") String password) {
	...
}

當瀏覽器請求 http://host:port/findUser?userId=801&password=123456時,

後臺接收到引數userId為801,password為123456

2.FormParam是用於獲取post請求的表單引數

@POST
@Path("/updateUserInfo")
@Consumes("application/x-www-form-urlencoded") 
public String findUser(@FormParam("userId") String userId,
		@FormParam("password") String password) {
	...
}

二、測試引數接收

get方法比較簡單(使用超連結後面追加引數),下面說下post

1.使用工具postman:

這樣後臺能夠正常取到引數

2.程式碼測試:使用ajax請求進行測試

$.ajax({
	url : "http://localhost:8090/updateUserInfo",
	type : "post",
	data : {
		"userId" : "801",
		"password" : "123456"
	},
	error: function (jqXHR, textStatus, errorThrown) {
			console.log(jqXHR.responseText,jqXHR.status,jqXHR.readyState,jqXHR.statusText);
			console.log(textStatus);
			console.log(errorThrown);
	},
	success : function (responseText) {
		console.log(responseText);
	}
});

參考連結: