post request with multiple parameters JSON and String on Jackson/Jersey JAVA(在 Jackson/Jersey JAVA 上发布带有多个参数 JSON 和 String 的请求)
问题描述
我使用 Jersey/Jackson 创建了一个 rest api,它运行良好.除了他们作为 JSON 接收的 POJO 之外,我想调整我的 POST 方法以接收字符串令牌.我已经像这样调整了我的一种方法:
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();
}
我想调用该方法,但无论出于何种原因,无论我尝试什么,token 都会打印为 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("http://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 ....
");
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();
}
}
任何帮助将不胜感激.
推荐答案
这不是 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:
- 创建一个包含用户对象和令牌的包装对象.在客户端和服务器之间来回发送.
- 将令牌指定为 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("http://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 的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Jackson/Jersey JAVA 上发布带有多个参数 JSON 和 String 的请求
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01