Weedoze
Weedoze

Reputation: 13943

AngularJS consumes JAX-RS rest web service

This is the first time that I am using a Java back-end for my web application. I have a Jax-rs webservice that I am trying to consumes with my AngularJS app

AngularJS call

$http({
    url : REST_END_POINT + '/checkuser',
    method : "GET",
    data : {
        'userId' : credentials.username,
        'pwd' : credentials.password
    },
    dataType : "json",
    headers : {
        "Content-Type" : "application/json"
    }
});

Webservice

@GET
@Produces("application/json")
@Consumes("application/json")
@Path("checkuser/")
public string getCheckUser(@QueryParam("userId") String userId, @QueryParam("pwd") String pwd){
    try{
        if(port != null){
            boolean result = port.checkUser(userId, pwd);
            return new java.lang.Boolean(result).toString();
        }
    }catch(Exception ex){
        //TODO
    }
    return null;
}

Both userId and pwd are always null

With Firebug I can see that data contains

Object{
    userId="aaa",
    pwd="aa"
}

I also tried with JSON.stringify which send those data :

"{"userId":"aaa","pwd":"aa"}"

Upvotes: 0

Views: 791

Answers (2)

adhikari
adhikari

Reputation: 110

You can use Jackson api for converting json to/ from Java objects.

Jackson contains simple mapper methods to implicitly map your json properties to Java class member variables.

Instead of @querypram use a Java class having fields as userId and pwd

Upvotes: 0

Forbsey1
Forbsey1

Reputation: 155

I believe that the way you are trying to access your userID and pwd is incorrect, you are using the @QueryParam which would look for the userID and pwd as query parameters of the GET request like so:

http://myservice:port/checkuser?userId=myuserid&pwd=pass

if you change your GET request to

$http({
url : REST_END_POINT + '/checkuser',
method : "GET",
params : {
    'userId' : credentials.username,
    'pwd' : credentials.password
},
dataType : "json",
headers : {
    "Content-Type" : "application/json"
}
});

Then you should have more luck.

However I wouldn't advise this method as it could be insecure. I'd instead look at trying to utilize an existing authentication system rather than rolling your own as these existing authentication systems will be far more secure.

Upvotes: 2

Related Questions