REST Web 服务中 POST HTTP 请求的处理参数 (JERSEY)
我正在 GAE 上开发一个休息网络服务。我正在使用 Jersey 框架来实现服务。这是一个 POST 服务,我还必须传递参数。我尝试使用两种类型的注释,但无法获取参数:
@Context
@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);
.......
}
@QueryParam
@POST
@Path("add")
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Notification addUser(@QueryParam("nickname") String nickname, @QueryParam("name") String name, @QueryParam("surname") String surname, @QueryParam("telephone") String telephone, @QueryParam("email") String 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":"[email protected]","telephone":"123"}
Sent Headers
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept-Language: en
有人知道我是否遗漏了什么吗?
预先感谢您的帮助
达尼洛
I'm developing a rest web service on GAE. I'm using Jersey framework to implement the services. It is a POST service where I have to pass also parameters. I tried to use 2 types of annotation but I can't get the parameters:
@Context
@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);
.......
}
@QueryParam
@POST
@Path("add")
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Notification addUser(@QueryParam("nickname") String nickname, @QueryParam("name") String name, @QueryParam("surname") String surname, @QueryParam("telephone") String telephone, @QueryParam("email") String email) {
User =createUser(nickname, name, surname, telephone, email);
......
}
But in both cases I can not get the parameters all of them are null values.
this is an example of my http request:
Request URL: http://windyser.appspot.com/rest/users/add
Request Method: POST
Params: {"nickname":"prova","name":"danilo","surname":"delizia","email":"[email protected]","telephone":"123"}
Sent Headers
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept-Language: en
Anybody know if I'm missing something?
Thanks in advance for your help
Danilo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Jetty 8.x 支持 Java EE 6,而 Jetty 7.x 仅支持 Java EE 5。
我从您的第一个代码片段中看到您使用了 Java EE 6 中的“@Produces”注释。这一定是问题所在。
修复方法:
[编辑]
让服务知道您需要使用字符串参数。尝试使用@Consumes运行时注释:
下一步如果您需要访问该服务:
希望这会有所帮助。
Jetty 8.x supports Java EE 6 and Jetty 7.x supports only Java EE 5.
I see from your first code snippet that you use "@Produces" annotation from Java EE 6. It must be the problem.
To fix:
[EDIT]
Let the service know that you need to consume String parameters. Try to use @Consumes runtime annotation:
next if you need to access the service:
Hope this helps.