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表单提交



Insert title here


	
First name: Last name:


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

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

	@POST
	@Path("test")
	@Produces("application/json; charset=utf-8")
	public Map getName(String data)
			throws JsonGenerationException, JsonMappingException, IOException {
		String name = null;
		String password = null;
		//解析传入json数据
		ObjectMapper objectMapper = new ObjectMapper();
		Map 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();
		}
		//输出map
		Map map = new HashMap();
		map.put("name", name);
		map.put("password", password);
		return map;
	}
}


前台页面:







postDate


	

post_data测试

name:
password:






你可能感兴趣的:(resteasy,ajax,resteasy)