当我调用下一个代码时:
Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
response.getEntity();
Run Code Online (Sandbox Code Playgroud)
response.getEntity()始终为null.
但是当我调用时:
JsonObject json = target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), JsonObject.class);
Run Code Online (Sandbox Code Playgroud)
变量json不为null.
我需要使用第一个变体,因为我需要检查响应状态.
为什么第一个代码不起作用?我怎样才能获得状态代码呢?
这是我的Java代码:
@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
public String sumPost(@QueryParam(value = "x") int x,
@QueryParam(value = "y") int y) {
System.out.println("x = " + x);
System.out.println("y = " + y);
return (x + y) + "\n";
}
Run Code Online (Sandbox Code Playgroud)
我这样称呼它:
curl -XPOST "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -d 'x:5&y:3'
Run Code Online (Sandbox Code Playgroud)
问题是System.out.println呼叫一直张贴零零,看来我没有正确传递 x 和 y。
得到答复后,我将请求更改为:
curl -d '{"x" : 4, "y":3}' "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -H "Content-Type:application/json" -H "Accept:text/plain" --include
Run Code Online (Sandbox Code Playgroud)
服务是:
@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String sumPost(@QueryParam(value = "x") int x,
@QueryParam(value = "y") int y) {
System.out.println("sumPost");
System.out.println("x …Run Code Online (Sandbox Code Playgroud) 我似乎无法让这个为我工作,我已经在其他帖子中看到这一点,并希望有人可能能够发现我做错了什么.我正试图得到这个休息api的请求的身体但似乎无法拉回我需要的东西,然后在下面的字符串中得到null.
@POST
@Path("/SetFeeds")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String setFeed(@PathParam("name")String name2, @QueryParam("name") String name,@Context UriInfo uriInfo){
MultivaluedMap<String,String> queryParams = uriInfo.getQueryParameters();
String query = uriInfo.getRequestUri().getQuery();
String response = queryParams.getFirst("name");
return response;
}
Run Code Online (Sandbox Code Playgroud)