Jersey REST 调用中的复杂查询
Complex query in Jersey REST call
我正在使用 com.sun.jersey.api.*
调用 REST 服务,我不能使用其他库,我需要进行更复杂的查询,例如
"customer" : {
"name": "Smith",
"documents" :
[{"id" : "100", "content" : "lorem"},
{"id" : "101", "content" : "ipsum"}]
}
这是我到目前为止尝试过的代码,仅查询 Customer::name
并且...它失败了。
Client client = Client.create();
WebResource resource = client.resource(URL);
String response = resource.queryParam("customer.name", "Smith")
.accept(MediaType.APPLICATION_FORM_URLENCODED)
.post(String.class);
我说的“失败”是指,我没有在服务器端收到 null
而不是“Smith”。
编辑
好吧,我犯了明显的错误,我需要 post 正文,而不是查询。还是...
String body = "{\"customer\": {\"name\" : \"Smith\"}}";
String s = resource
.accept(MediaType.APPLICATION_FORM_URLENCODED)
.post(String.class, body);
System.out.println(body);
打印
{"customer": {"name" : "Smith"}}
并且对服务器的传入请求是 null
。
尝试在 Postman 中使用与正文相同的 JSON - 它奏效了。
我有 Post 请求的示例代码,如果您提到的 JSON 是您希望在服务器端接收的内容,请在 Post body 而不是作为请求参数,如果它是请求参数,则检查您的服务器是否需要相同的关键参数,即 customer.name
Post 的示例代码,正文 JSON 数据
public static void main(String[] args) {
try {
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/RESTfulExample/rest/foo");
String input = "{
\"customer\": {
\"name\": \"Smith\",
\"documents\": [{
\"id\": \"100\",
\"content\": \"lorem\"
},
{
\"id\": \"101\",
\"content\": \"ipsum\"
}
]
}
}";
ClientResponse response = webResource.type("application/json")
.post(ClientResponse.class, input);
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);
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
这里是参考 link 以供您参考
https://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/
编辑后
设置 webResource.type("application/json")
我正在使用 com.sun.jersey.api.*
调用 REST 服务,我不能使用其他库,我需要进行更复杂的查询,例如
"customer" : {
"name": "Smith",
"documents" :
[{"id" : "100", "content" : "lorem"},
{"id" : "101", "content" : "ipsum"}]
}
这是我到目前为止尝试过的代码,仅查询 Customer::name
并且...它失败了。
Client client = Client.create();
WebResource resource = client.resource(URL);
String response = resource.queryParam("customer.name", "Smith")
.accept(MediaType.APPLICATION_FORM_URLENCODED)
.post(String.class);
我说的“失败”是指,我没有在服务器端收到 null
而不是“Smith”。
编辑
好吧,我犯了明显的错误,我需要 post 正文,而不是查询。还是...
String body = "{\"customer\": {\"name\" : \"Smith\"}}";
String s = resource
.accept(MediaType.APPLICATION_FORM_URLENCODED)
.post(String.class, body);
System.out.println(body);
打印
{"customer": {"name" : "Smith"}}
并且对服务器的传入请求是 null
。
尝试在 Postman 中使用与正文相同的 JSON - 它奏效了。
我有 Post 请求的示例代码,如果您提到的 JSON 是您希望在服务器端接收的内容,请在 Post body 而不是作为请求参数,如果它是请求参数,则检查您的服务器是否需要相同的关键参数,即 customer.name
Post 的示例代码,正文 JSON 数据
public static void main(String[] args) {
try {
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/RESTfulExample/rest/foo");
String input = "{
\"customer\": {
\"name\": \"Smith\",
\"documents\": [{
\"id\": \"100\",
\"content\": \"lorem\"
},
{
\"id\": \"101\",
\"content\": \"ipsum\"
}
]
}
}";
ClientResponse response = webResource.type("application/json")
.post(ClientResponse.class, input);
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);
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
这里是参考 link 以供您参考 https://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/
编辑后 设置 webResource.type("application/json")