resteasy几种传值方式

1.@pathparam

	//删除商品
	@GET
	@Path("delProByPNumber/{param}")
	@Produces("application/json; charset=utf-8")
	public Response delPro(@PathParam("param") String  pNumber){
		session.delete("production.delete", pNumber);
		session.commit();
		List list=queryAllPro();
		return Response.status(201).entity(list).build();
	}
访问路径:http://localhost:8888/Invoice/optPro/delProByPNumber/7
其中7为传入参数


2.@queryParam

//添加账户
@GET
	@Path("addAcc/parameters")
	@Produces("application/json; charset=utf-8")
	public Response addCus(
			@QueryParam("accname") String accname,
			@QueryParam("balance") long balance,
			@QueryParam("department") String   department,
			@QueryParam("bankname") String bankname,
			@QueryParam("accnumber") long accnumber,
			@QueryParam("username") String username
			) {
		Account account=new Account();
		account.setAccname(accname);
		account.setBalance(balance);
		account.setDepartment(department);
		account.setBankname(bankname);
		account.setAccnumber(accnumber);
		account.setUsername(username);
		session.insert("account.addAcc",account);
		session.commit();

		return Response.status(201).entity(queryOne(accname)).build();
	}
访问路径:http://localhost:8888/Invoice/optAcc/addAcc/parameters?accname=rr&balance=99999&department=heda&bankname=邮政&accnumber=6666&username=yu
parameters后面为传入参数


3.@FormParam

//form表单提交方式
@Path("/services/hello")
public class HelloWorldRestService {
	@POST
	@Path("getName")
	public String  getName(@FormParam("fname") String fname,@FormParam("lname") String lname) {
		String result = "RESTEasy Hello World : " + fname+lname;
		System.out.println("fname"+fname);
		return result;
	}
}
前台form表单提交
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="http://localhost:8888/Invoice/services/hello/getName" method="POST">
		First name: <input type="text" name="fname" />
		Last name: <input type="text" name="lname" />
		<input type="submit" value="Submit" />
	</form>
</body>
</html>


访问路径:
http://localhost:8888/Invoice/services/hello/getName

4.接收json参数,不需要标注

	@POST
	@Path("test")
	@Produces("application/json; charset=utf-8")
	public Map<String, Object> getName(String data)
			throws JsonGenerationException, JsonMappingException, IOException {
		String name = null;
		String password = null;</span>
		//解析传入json数据
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, Object> maps;
		try {
			maps = objectMapper.readValue(data, Map.class);
			name = (String) maps.get("name");
			password = (String) maps.get("password");
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}</span>
		//输出map
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("password", password);
		return map;
	}
}


前台页面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/jquery-2.0.0.js"></script>
<script type="text/javascript" src="js/jquery-2.0.0.min.js"></script>
<script type="text/javascript">
function showInfo() {
 	var name = encodeURI($('#name').val(),"UTF-8");
	var password = encodeURI($('#password').val(),"UTF-8");

	$.ajax({
				type : 'post',
				url : 'http://localhost:8888/Invoice/services/getPost/test',
				data: JSON.stringify({ "name": name, "password": password  }),
				dataType: "json",
				contentType: "application/json; charset=UTF-8",
				success : function(result) {
					var name2=result.name;
					var password2=result.password;
					alert("name2:"+name2+"======password2:"+password2);
				},
				error : function erryFunction() {
					alert("error");
				}
			});
}
</script>

<title>postDate</title>
</head>
<body>
	<h2>post_data测试</h2>
	name:<input type="text" id="name" /><br>
	password:<input type="text" id="password" /><br>
<button type="submit" οnclick="showInfo()">提交</button>

</body>

</html>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值