REST Web 服务中 POST HTTP 请求的处理参数 (JERSEY)

发布于 2024-11-17 04:25:38 字数 1693 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2024-11-24 04:25:38

Jetty 8.x 支持 Java EE 6,而 Jetty 7.x 仅支持 Java EE 5。
我从您的第一个代码片段中看到您使用了 Java EE 6 中的“@Produces”注释。这一定是问题所在。

修复方法:

  1. 确保您使用与 Jetty 8.x 捆绑在一起的 GAE SDK 以支持注释 - 或 -
  2. 仅使用 Java EE 5 工具。

[编辑]

让服务知道您需要使用字符串参数。尝试使用@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()

希望这会有所帮助。

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:

  1. Make sure you are using GAE SDK bundled with Jetty 8.x to support the annotation -or-
  2. Use Java EE 5 facilities only.

[EDIT]

Let the service know that you need to consume String parameters. Try to use @Consumes runtime annotation:

@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) {        
    ......
}

next if you need to access the service:

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()

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文