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);
	}
});

参考链接:

https://blog.csdn.net/azhegps/article/details/72782704

猜你喜欢

转载自blog.csdn.net/u010999809/article/details/84104648