3 Replies Latest reply on Mar 16, 2011 7:23 AM by olasamuel

    How to update a database with resteasy @PUT. Please help

    olasamuel
      Hi All,

      Please, I need a help regarding this and I will appreciate it if someone can put me through. OK this is the problem (and please, note, I am new to RestEasy and I am trying with it and always ready to learn).

      I am creating a resteasy application where a user's account will be deducted based on. So when the user initiate a transaction, the system will reserve some units from his/her account and then after the transaction, the total money will be deducted and then the system will then update the user's account table using the RestEasy @PUT request method. I have tried my best but all I can get is to GET inside the user's account and fetch the units out but my system is not deducting nor updating table using the @PUT request method. I have also tried to go through the example but the fact is that most of the examples that I have seen so far have always gone in one direction which only to explain and empahsize on the the @GET Request Method.

      Can someone please help put me through? I will greatly appreciate this. Below is my code. Thank you.


      public interface SubscriberTopUpResource {
           
           
           @GET
           @PUT
           @Path("/reserveUnits")
           @Produces(MediaType.TEXT_PLAIN)
           public String reserveUnits(@PathParam("msisdn") String msisdn, @PathParam("requestedUnits") long requestedUnits);

            }




      public String reserveUnits(@PathParam("msisdn") String msisdn, @PathParam("requestedUnits") long requestedUnits) {

                String balInquiry = "select balance from SubscriberTopUp where msisdn= :msisdn";
                System.out.println("I was able to get here");
                Query balInquiryQuery = entityManager.createQuery(balInquiry);
                System.out.println("Something happened here");
                long allocatedUnits = 0;
                System.out.println("Another thing happened here");
                @SuppressWarnings("unchecked")
                List<Long> bals = (List<Long>)balInquiryQuery.setParameter("msisdn", Long.valueOf(msisdn)).getResultList();
                System.out.println("OK Lets see if another thing happened here again");
                //BigInteger newbal = new BigInteger(String.valueOf(requestedUnits));
                
                if (bals.size() > 0)          
                {
                     allocatedUnits = Math.min(requestedUnits, bals.get(0));
                     if (bals.get(0) > requestedUnits)
                     FacesMessages.createFacesMessage(FacesMessage.SEVERITY_WARN, "Insufficient Fund.", "The System could only allocated " + allocatedUnits + "units");
                     System.out.println("You Reuested " + requestedUnits + " The current units in your account is " + bals + "units");
                
                     //update the balance
                     long oldBalance = bals.get(0);
                     long newBalance = oldBalance - allocatedUnits;
                     //subscriberTopUp = new SubscriberTopUp();
                     String balQuery = "Update SubscriberTopUp SET balance = :newBalance WHERE msisdn = :msisdn";
                     System.out.println("The Update statement is :"+balQuery);
                     Query query = entityManager.createQuery(balQuery);
                     System.out.println("I can also see entity manager getting through " + query);
                     query.setParameter("newBalance", newBalance);
                     System.out.println("I finally got here " + query);
                     query.setParameter("msisdn", Long.valueOf(msisdn));
                     System.out.println("And Lastly " + query);
                     entityManager.flush();
                     //int rowsAffected = (Integer) query.getSingleResult();
                     System.out.println("Also the commit was done");
                     
                               entityManager.getTransaction().commit();
                  
                     //System.out.println("I just got here : updated : "+String.valueOf(rowsAffected));
                     
                     
                } else {
                     System.out.println("Null Record Returned" + bals.toString());
                     }
                          
                
                allocatedUnits = bals.get(0);
                System.out.println("Returning : "+allocatedUnits);
                return String.valueOf(bals);
                
                
                
                          
           }

      }
        • 1. Re: How to update a database with resteasy @PUT. Please help
          jharting

          Firstly, what kind of client do you use the web service from? (javascript, Java fat client, or?) - This should not matter at all, however, since you use FacesMessages in your code I just want to make sure you know what you're doing.


          Secondly, it is not a good idea to both @GET and @PUT annotations on a single method. I am event not sure if this is supported. You should:



          1. Define a @GET method which only displays an account balance

          2. Define a @PUT - or even better a @POST method for deducting - (i.e. POST to /reserveUnits/deduct?requestedUnits=123)



          Finally,


          you did not write what's wrong. Are you getting an exception? Is your method never invoked?

          • 2. Re: How to update a database with resteasy @PUT. Please help
            olasamuel
            Thank you for your response Josef. However, I am not using Javascript neither am I using Java fat client. what I am using is purely seam and Jsf in an eclipse environment. I have also try another way to test if this works but when I try to invoke this method I get the following error displayed on the browser:


            type Status report

            message

            description The specified HTTP method is not allowed for the requested resource ().
            JBoss Web/2.1.3.GA. My console does not make any move as to display what the real error was and there was no error message from JBoss as I checked the log file

            This is a 405 Error Message. See below the code that I am using and this is just to test the system if @PUT really works. I have also used the @POST as suggested. Can you please help and tell me what I am doing wrong. Thank you



            public void updateBalance(@PathParam("msisdn") String msisdn, @PathParam("newBal")long newBal) {
                      String balInquiry = "update SubscriberTopUp SET balance =:newBal where msisdn= :msisdn";
                      System.out.println("I was able to get here");
                      Query balInquiryQuery = entityManager.createQuery(balInquiry);
                      balInquiryQuery.setParameter("balance", newBal);
                      balInquiryQuery.setParameter("msisdn", msisdn);
                      System.out.println("Something happened here");
                      System.out.println("Another thing happened here");
                      //@SuppressWarnings("unchecked")
                      System.out.println("OK Lets see if another thing happened here again");
                      int result  = balInquiryQuery.executeUpdate();
                      System.out.println("Returning: " + String.valueOf(result));
                      
                      
                 }


            The interface
                    @PUT
                 @Path("/updateBalance")
                 @Produces(MediaType.APPLICATION_XML)
                 public void updateBalance(@QueryParam("msisdn") String msisdn, @QueryParam("newBal") long newBal);
                 
            • 3. Re: How to update a database with resteasy @PUT. Please help
              olasamuel

              Any help on this issue please.