how do i Post a JSON object in a jax-rs service?

406 views
Skip to first unread message

johnson Eyo

unread,
Nov 5, 2013, 4:06:30 PM11/5/13
to codenameone...@googlegroups.com
i have a jax-rs service as this 

@POST
    @Path("/userlogin")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response userlogin(Users user){
      System.out.printf("the %s",user.getEmail());  
        
   Query q =     em.createQuery("select u from Users u where u.email = :email and u.password = :password");
   q.setParameter("email",user.getEmail());
     q.setParameter("password",user.getPassword());
     
     try{
    Users u = (Users)q.getSingleResult();
    if( u.getStatus().equals("UN-VERIFIED")){
             try {
                 throw new UnVerifiedUserException("hi "+u.getFirstName()+", you haven't verified account");
             } catch (UnVerifiedUserException ex) {
                  return Response.status(Response.Status.FORBIDDEN).entity(ex.getUsermessage()).build();
             }
       
    }
    else{
    
    return Response.ok(u, MediaType.APPLICATION_JSON).build();
    }
     }
     catch(NoResultException no){
         return Response.status(Response.Status.NOT_FOUND).build();
     } 
        
    }

    

since it comes with a payload, i did a CN1 client as this ;


 Hashtable user = new Hashtable();
            user.put("email",tx1.getText());
            user.put("password", tx2.getText());
           
           final  String payload = Result.fromContent(user).toString();
           
             ConnectionRequest con = new ConnectionRequest(){

                   @Override
                   protected void buildRequestBody(OutputStream os) throws IOException {
                         os.write(payload.getBytes("UTF-8")); 
                   }

                  
            
                   
            
            
            };
            con.setPost(true);
            
            con.setContentType("application/json");
             con.addRequestHeader("Accept", "application/json");
            
             
            NetworkManager.getInstance().addToQueue(con);
         
         
           }
        }



    when i click the button to login ,i expect it to make a post request to login, but it returns a 404 with Dialog, method or uri not found,i dont want to write a queryparam method where i can easily set the argument  query to login, i intend to maintain an SOA architecture where all consumers of this api share the same method
so when i click this button.it seems the api method is not reached because i dont see any log on the method been invocated. please help
    

Eric Coolman

unread,
Nov 5, 2013, 7:37:45 PM11/5/13
to codenameone...@googlegroups.com
 
Hi, you need to replace "localhost" in your CodenameOne code to the actual address of your webservice, otherwise you're trying to post to the device itself.

Eric

johnson Eyo

unread,
Nov 6, 2013, 1:42:15 AM11/6/13
to codenameone...@googlegroups.com
@eric coolman, im testing locally so, i have no issues with using a local application server, i make @GET requests easily  from a rest resources, i just want to send a payload of a User object class in json format, to this uri and the post method consumes a json object.
please look @ the code and tell me what could be the cause of the 404 response, i know 404 means NOT FOUND status,do i need to append the payload object to the uri? im thinking

Eric Coolman

unread,
Nov 6, 2013, 3:15:19 AM11/6/13
to codenameone...@googlegroups.com
No, I don't believe appending the payload to the URL will work.  I've scanned the rest of the code a few times though and I'm not noticing anything else that might cause the issue.  You're able to successfully do GETs from Codename One to the usermanager service?   (If not, please include the servlet mapping config fragment for the service, and maybe the initialization of the application context). 

Eric

johnson Eyo

unread,
Nov 6, 2013, 5:45:24 AM11/6/13
to codenameone...@googlegroups.com
I initially didn't require a web.xml descriptor since I'm using ApplicationConfig class to configure the resource classes,but I later added the web.xml so I could configure a CORS filter when Cross Domain issues from a JSON angular js client required access to the service. That's basically what makes my web.xml file. A CORS filter servlet allowing origins.

Without allow origins,I can still consume from this service from CN1 mobile client, I can make get requests ,getData() all that,but still figuring how to send a JSON payload in a post method

Message has been deleted

coolman...@gmail.com

unread,
Nov 6, 2013, 1:21:21 PM11/6/13
to codenameone...@googlegroups.com
The code you've posted should be sending the json payload as expected, and similar to how the jax-rs streaming client would send the payload.  You can verify this from the Codename One simulator by turning on the network monitor.  I wonder if you need to include a Content-Length header?

Shai Almog

unread,
Nov 6, 2013, 1:22:05 PM11/6/13
to codenameone...@googlegroups.com
You can add any argument to the post using addArgument().
If you just want the body of the post to include the JSON data without any type before it you can just override the method:

protected void buildRequestBody(OutputStream os) throws IOException

And just build the body in any way you like (e.g. by just writing your JSON string).

coolman...@gmail.com

unread,
Nov 6, 2013, 1:24:36 PM11/6/13
to codenameone...@googlegroups.com
That's what he's done in the OP.

Shai Almog

unread,
Nov 6, 2013, 1:37:46 PM11/6/13
to codenameone...@googlegroups.com, coolman...@gmail.com
Missed that line ;-)

OK that might be a different issue then. Try this:
OutputStreamWriter w = new OutputStreamWriter(os, "UTF-8");
w
.write(val.toString());


johnson Eyo

unread,
Nov 6, 2013, 4:46:47 PM11/6/13
to codenameone...@googlegroups.com
im really sory guyz,it worked , iwasnt paying attention to the uri, the method was in a differenturi ,tis solved
Reply all
Reply to author
Forward
0 new messages