Java 如何发出HTTP删除/更新请求?

Java 如何发出HTTP删除/更新请求?,java,http,restful-url,http-delete,Java,Http,Restful Url,Http Delete,现在我有一个名为Users.java的Restful Web服务,该服务将用户添加到假数据库中 @Path("/users") public class UsersService { @POST public Response handlePost(String requestBody) { addUserToDatabase(requestBody); Map<String, Object> jsonMap = new HashM

现在我有一个名为Users.java的Restful Web服务,该服务将用户添加到假数据库中

@Path("/users")
public class UsersService {

    @POST
    public Response handlePost(String requestBody) {
        addUserToDatabase(requestBody);

        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("status", "success");
        jsonMap.put("resource-uri", "/users/12"); // assume 12 is the ID of the user we pretended to create
        Gson gson = new Gson();

        return Response.status(200).entity(gson.toJson(jsonMap)).build();
    }

    private boolean addUserToDatabase(String requestBody) {
        Gson gson = new Gson();
        Map<String, String> user = gson.fromJson(requestBody, new TypeToken<Map<String, String>>() {
        }.getType());
        for (String key : user.keySet()) {
            System.out.println(key + ": " + user.get(key));
        }
        return true; // lie and say we added the user to the database
    }

}
@Path(“/users”)
公共类用户服务{
@职位
公共响应handlePost(字符串请求主体){
addUserToDatabase(请求主体);
Map jsonMap=newhashmap();
jsonMap.put(“状态”、“成功”);
put(“资源uri”,“/users/12”);//假设12是我们假装创建的用户的ID
Gson Gson=新的Gson();
返回Response.status(200).entity(gson.toJson(jsonMap)).build();
}
私有布尔addUserToDatabase(字符串请求体){
Gson Gson=新的Gson();
Map user=gson.fromJson(requestBody,new-TypeToken()){
}.getType());
for(字符串键:user.keySet()){
System.out.println(key+”:“+user.get(key));
}
return true;//撒谎并说我们已将用户添加到数据库中
}
}
这里使用Post请求调用它,以下是示例

public HttpResponse postRequest(String relativePath, Map<String, String> map){
    try {

        String fullPath = buildURL(relativePath);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost getRequest = new HttpPost(fullPath);
        Gson gson = new Gson();
        String postEntity = gson.toJson(map);
        getRequest.setEntity(new StringEntity(postEntity));
        getRequest.addHeader("accept", "application/json");
        HttpResponse response = httpClient.execute(getRequest);
        httpClient.getConnectionManager().shutdown();

        return response;
  } catch (IOException e) {
        e.printStackTrace();
  }
    return null;
}

  // POST
  Map<String, String> map = new HashMap<>();
  map.put("username", "Bill");
  map.put("occupation", "Student");
  map.put("age", "22");
  map.put("DOB", "" + new Date(System.currentTimeMillis()));
  HttpResponse response = client.postRequest("/api/users", map);
  client.printResponse(response);
公共HttpResponse postRequest(字符串相对路径,映射){
试一试{
字符串fullPath=buildURL(relativePath);
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost getRequest=新的HttpPost(完整路径);
Gson Gson=新的Gson();
字符串postEntity=gson.toJson(map);
setEntity(新的StringEntity(postEntity));
addHeader(“接受”、“应用程序/json”);
HttpResponse response=httpClient.execute(getRequest);
httpClient.getConnectionManager().shutdown();
返回响应;
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
//职位
Map Map=newhashmap();
地图放置(“用户名”、“账单”);
地图放置(“职业”、“学生”);
地图放置(“年龄”、“22”);
map.put(“DOB”,“”+新日期(System.currentTimeMillis());
HttpResponse response=client.postRequest(“/api/users”,map);
客户。打印响应(响应);

现在我想用Delete和Update做一些类似的事情,但不知道从哪里开始任何帮助都会很好

使用适当的
@Path
@Delete
@Put
注释,并以与
@Post
类似的方式实现这些方法