REST web服务(JERSEY)中POST HTTP请求的进程参数
我正在GAE上开发一个RESTWeb服务。我正在使用Jersey框架来实现这些服务。这是一项邮政服务,我还必须传递参数。我尝试使用两种类型的注释,但无法获取参数: @上下文REST web服务(JERSEY)中POST HTTP请求的进程参数,http,google-app-engine,jersey,httprequest,Http,Google App Engine,Jersey,Httprequest,我正在GAE上开发一个RESTWeb服务。我正在使用Jersey框架来实现这些服务。这是一项邮政服务,我还必须传递参数。我尝试使用两种类型的注释,但无法获取参数: @上下文 @POST @Path("add") @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public Notification addUser(@Context UriInfo uriInfo){ MultivaluedMap&
@POST
@Path("add")
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Notification addUser(@Context UriInfo uriInfo){
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String nickname = queryParams.getFirst("nickname");
String name = queryParams.getFirst("name");
String surname = queryParams.getFirst("surname");
String telephone = queryParams.getFirst("telephone");
String email = queryParams.getFirst("email");
User =createUser(nickname, name, surname, telephone, email);
.......
}
但是在这两种情况下,我都不能得到参数,它们都是空值
这是我的http请求的一个示例:
Request URL: http://windyser.appspot.com/rest/users/add
Request Method: POST
Params: {"nickname":"prova","name":"danilo","surname":"delizia","email":"prova@yahoo.it","telephone":"123"}
Sent Headers
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept-Language: en
有人知道我是否遗漏了什么吗
提前谢谢你的帮助
DaniloJetty 8.x支持JavaEE6,Jetty 7.x仅支持JavaEE5。 我从您的第一个代码片段中看到,您使用了来自JavaEE6的“@products”注释。这一定是问题所在 要修复:
[编辑] 让服务知道您需要使用字符串参数。尝试使用@Consumes运行时批注:
@POST
@Path("/add")
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes("application/x-www-form-urlencoded") // to consume String parameters
public Notification addUser(@FormParam("nickname") String nickname) {
......
}
下一步,如果您需要访问该服务:
URL url = new URL("http://[HOST]:[PORT]/[CONTEXT]/?nickname=prova"); // Your parameter
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.connect();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line); // Your service's response
}
rd.close();
conn.disconnect()
希望这能有所帮助。Hi Surlac,正如u所说,我尝试了@Consumes(“text/plain”)和@Consumes(“application/x-www-form-urlencoded”)注释,但仍然不起作用。在第一种情况下,我从GAE获得“不支持的媒体类型”。第二种情况是我得到空参数。有没有关于我如何做到这一点的线索???试着在你的POST方法签名中使用@FormParam,正如我在回答中所描述的那样。
URL url = new URL("http://[HOST]:[PORT]/[CONTEXT]/?nickname=prova"); // Your parameter
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.connect();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line); // Your service's response
}
rd.close();
conn.disconnect()