jersey学习之Post请求



上一篇中介绍了jersey的get请求,本篇在上篇的基础上介绍jerseyPost请求。

在HelloWorldRs中写post服务:

@POST
@Path("/sayHi")
@Produces(MediaType.TEXT_HTML)
public String sayHi(@QueryParam("from") String fromValue,
@Context HttpServletRequest request) {

return "hi " + map.get(fromValue);

}


在client中添加请求post的方法:
个人感觉和get几乎一样的。

public void sayHiTest(){


Client client = Client.create();
WebResource webResource = client.resource(url+"/helloWorldRs/sayHi");
MultivaluedMap<String, String> param= new MultivaluedMapImpl();

param.add("from", "2");
String result = webResource.queryParams(param).post(String.class);

System.out.println(result);
}


启动服务端,在client的main方法中执行hiTest方法,得到答案:

hi 222

浏览器中:  http://localhost:8080/jerseydemo/rest/helloWorldRs/sayHi?from=2  测试得到405 错误。 这个已经是post请求,再用get请求访问,当然报错。





猜你喜欢

转载自annybz.iteye.com/blog/1980801