在Jackson / Jersey JAVA上发布带有多个参数JSON和String的请求

编程入门 行业动态 更新时间:2024-10-26 16:23:57
本文介绍了在Jackson / Jersey JAVA上发布带有多个参数JSON和String的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Jersey / Jackson创建了一个休息api,效果很好。我想调整我的POST方法以接收除了作为JSON接收的POJO之外的字符串标记。我已经调整了我的一个方法:

I've created a rest api using Jersey/Jackson and it works well. I want to adjust my POST methods to receive a string token in addition to the POJO they are receiving as JSON. I've adjusted one of my methods like so:

@POST @Path("/user") @Consumes(MediaType.APPLICATION_JSON) public Response createObject(User o, String token) { System.out.println("token: " + token); String password = Tools.encryptPassword(o.getPassword()); o.setPassword(password); String response = DAL.upsert(o); return Response.status(201).entity(response).build(); }

我想调用该方法,但无论出于何种原因令牌无论我尝试什么,打印到null。这是我写的发送邮件请求的客户端代码:

I want to call that method, but for whatever reason token prints to null no matter what I try. Here is the client code I've written to send the post request:

public String update() { try { com.sun.jersey.api.client.Client daclient = com.sun.jersey.api.client.Client .create(); WebResource webResource = daclient .resource("localhost:8080/PhizzleAPI/rest/post/user"); User c = new User(id, client, permission, reseller, type, username, password, name, email, active, createddate, lastmodifieddate, token, tokentimestamp); JSONObject j = new JSONObject(c); ObjectMapper mapper = new ObjectMapper(); String request = mapper.writeValueAsString(c) + "&{''token'':,''" + "dog" + "''}"; System.out.println("request:" + request); ClientResponse response = webResource.type("application/json") .post(ClientResponse.class, request); if (response.getStatus() != 201) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } System.out.println("Output from Server .... \n"); String output = response.getEntity(String.class); setId(UUID.fromString(output)); System.out.println("output:" + output); return "" + output; } catch (UniformInterfaceException e) { return "failue: " + e.getMessage(); } catch (ClientHandlerException e) { return "failue: " + e.getMessage(); } catch (Exception e) { return "failure: " + e.getMessage(); } }

任何帮助都将不胜感激。

Any help would be greatly appreciated.

推荐答案

这不是JAX-RS的工作方式。 POST请求的主体将被封送到带注释的资源方法的第一个参数(在这种情况下,进入 User 参数)。你有两个选择来解决这个问题:

This is not the way JAX-RS works. The body of your POST request will get marshaled to the first argument of your annotated resource method (in this case, into the User argument). You have a couple options to get around this:

  • 创建一个包含User对象和令牌的包装器对象。在客户端和服务器之间来回发送。
  • 在您的URL上将令牌指定为查询参数,并在服务器端以 @QueryParam 。
  • 将标记添加为标头参数,并在服务器端以 @HeaderParam 的形式访问它。
  • Create a wrapper object containing both a User object and token. Send that back and forth between your client and server.
  • Specify the token as a query parameter on your URL and access it on the server side as a @QueryParam.
  • Add the token as a header parameter and access it on the server side as a @HeaderParam.
  • 示例 - 选项1

    class UserTokenContainer implements Serializable { private User user; private String token; // Constructors, getters/setters }

    示例 - 选项2

    客户:

    WebResource webResource = client. resource("localhost:8080/PhizzleAPI/rest/post/user?token=mytoken");

    服务器:

    @POST Path("/user") @Consumes(MediaType.APPLICATION_JSON) public Response createObject(@QueryParam("token") String token, User o) { System.out.println("token: " + token); // ... }

    示例 - 选项3

    客户:

    ClientResponse response = webResource .type("application/json") .header("Token", token) .post(ClientResponse.class, request);

    服务器:

    @POST Path("/user") @Consumes(MediaType.APPLICATION_JSON) public Response createObject(@HeaderParam("token") String token, User o) { System.out.println("token: " + token); // ... }

    更多推荐

    在Jackson / Jersey JAVA上发布带有多个参数JSON和String的请求

    本文发布于:2023-11-26 17:34:31,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1634527.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:多个   参数   Jersey   Jackson   JAVA

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!