首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在RESTful应用程序中使用webResource删除记录时出现问题

在RESTful应用程序中使用webResource删除记录时出现问题
EN

Stack Overflow用户
提问于 2015-07-04 07:39:40
回答 1查看 385关注 0票数 2

我正在尝试使用webResource.type.delete删除记录。但是,我无法使用此方法删除。我已经尝试使用邮递员,我可以通过此实用程序删除记录。我在执行过程中没有得到任何错误,只是servlet没有进入资源来删除记录。

源代码如下:

DeletePolicy.java (主servlet):

代码语言:javascript
运行
复制
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter printWriter = response.getWriter();

        Client client= Client.create();
        WebResource webResource= client.resource("http://localhost:8080/clientLibrary/webapi/policy");

        //create an object of RequestDispatcher 
        RequestDispatcher rd = request.getRequestDispatcher("GetPolicy"); 

        // send the client data available with req of delete to req of getPolicy with include() 
        rd.include(request, response);

        List<Policy> policies = (List<Policy>) request.getAttribute("policies");

        printWriter.print("List of policies in Delete: ");

        for(Policy policy : policies) {
             printWriter.println("<li>"+"ID: "+policy.getId()+"<br>"+"Max Number of Books: "+policy.getMax_books()+"<br>"+"Year of Book: "+policy.getYear_book()+"<br>"+"Activated: "+policy.getActivate()+"<br></li><br>");
         }

        //Show to the user the possible options to delete using radio button
        request.setAttribute("policies", policies);

        RequestDispatcher rd2 = getServletConfig().getServletContext().getRequestDispatcher("/showRecordsToDelete.jsp");
        rd2.include(request,response);

        printWriter.println("I am comming back from showRecordsToDelete.jsp");

        //Receive the id from showRecordsToDelete.jsp
        String policyID = request.getParameter("id");

        ClientResponse rs = webResource.type(MediaType.APPLICATION_JSON).delete(ClientResponse.class);

        //Receive the answer and provide status to user
        printWriter.print("Delete a policy");
}

PolicyResource.java:

代码语言:javascript
运行
复制
@Path("/policy")
public class policyResource {
    policyDB db = new policyDB();
    policyService PolicyService = new policyService();

    @DELETE
    @Path("/{policyID}")
    @Produces(MediaType.APPLICATION_JSON)   
    public String removeBook(@PathParam("policyID") int ID){
        System.out.println("id :"+ID);
        boolean removed= PolicyService.deletePolicy(ID);
        String answer="Removed successfully";
        if(removed = false){
            answer="Not removed";
        }
        return answer;
    }   
}

PolicyDB.java

代码语言:javascript
运行
复制
/** Delete a policy
     * @param id
     * @return true if everything is done otherwise returns false
     */
    public boolean delete(int id) {
        Connection c = null;
        System.out.println("delete id :"+id);
        c = accessDB();
        if (c != null) {
            try {
                Statement stmt = null;
                // Execute a query
                stmt = c.createStatement();
                String sql = "DELETE FROM POLICIES WHERE ID='"+ id +"';";
                System.out.println(sql);
                stmt.executeUpdate(sql);
                c.commit();

                stmt.close();
                c.close();
            } catch ( Exception e ) {
                // Handle errors for Class.forName
                System.err.println( e.getClass().getName() + ": " + e.getMessage() );
                return false;
            }
        }
        System.out.println("Deleted "+id);
        return true;

    }

预先感谢您的帮助

EN

Stack Overflow用户

发布于 2015-07-04 08:48:41

问题的解决方法是在策略的URL中包含ID:

代码语言:javascript
运行
复制
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String policyID = request.getParameter("id");
        if (policyID == null) return;

        Client client= Client.create();
        WebResource webResource= client.resource("http://localhost:8080/clientLibrary/webapi/policy/"+policyID);

        ClientResponse rs = webResource.accept("application/json")
                .type("application/json").delete(ClientResponse.class);

        // display response
        String output = rs.getEntity(String.class);
        System.out.println("Output from Server .... ");
        System.out.println(output + "\n");
    }
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31215358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档