3

I am trying to post query parameters from angular JS to rest server. But the value received in the rest is always null. Can anyone please tell what i am missing?

Angular code,

$http({
                method : 'POST',
                url    : '/rest/user/changePassword',
                data   : {'id':$scope.user.id, 'password':$scope.user.password, 'newpassword':$scope.user.newpassword},
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).success(function(data){
                console.log(' data ');
            });

Rest code,

    @Path("/changePassword")
        @POST
        @Produces(MediaType.APPLICATION_JSON)
        @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
        public Response changePassword(@QueryParam("id") int id,
                @QueryParam("password") String password,
                @QueryParam("newpassword") String newpassword, @Context UriInfo request) {


            System.out.println(" id " + id + " re " + request.getQueryParameters().getFirst("id"));
            System.out.println(" password " + password  +   " "+  request.getQueryParameters().getFirst("password"));

            System.out.println(" newpassword " + newpassword + " " + request.getQueryParameters().getFirst("newpassword")  );
return new Response('asd');    
        }

I tried $.param in angular side, tried to get parameters from HttpServletRequest but the result is always null.

user2724215
  • 165
  • 5
  • 16

2 Answers2

1

@QueryParam is used to pick up values passed across in a query string in Get Requests.

Create a Java Object that has the variables matching your data structure, and use that as an input instead of the separate variables.

M21B8
  • 1,867
  • 10
  • 20
  • Thanks for your reply, is there any other way to get this parameters instead of creating java object. – user2724215 Jun 24 '14 at 10:50
  • If i use get method then is it possible to retrieve the query param? – user2724215 Jun 24 '14 at 11:18
  • 1
    You could, but, based on what your service is called, I would not recommend it. Your attributes are not really query parameters, there are data items to be committed to the database, so a Post is correct. – M21B8 Jun 24 '14 at 12:25
1

Since you are using HTTP POST, you need to replace @QueryParam with @FormParam. Your code would then be:

@Path("/changePassword")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response changePassword(@FormParam("id") int id,
                               @FormParam("password") String password,
                               @FormParam("newpassword") String newpassword, @Context UriInfo request) {


    System.out.println(" id " + id + " re " + request.getQueryParameters().getFirst("id"));
    System.out.println(" password " + password  +   " "+  request.getQueryParameters().getFirst("password"));

    System.out.println(" newpassword " + newpassword + " " + request.getQueryParameters().getFirst("newpassword")  );
    return new Response('asd');
}

Check out this tutorial

You also need to force AngularJS to POST the data as form parameters like so:

$http({
    method : 'POST',
    url : '/rest/user/changePassword',
    data : $.param({
        'id' : $scope.user.id,
        'password' : $scope.user.password,
        'newpassword' : $scope.user.newpassword
    }),
    headers : {
        'Content-Type' : 'application/x-www-form-urlencoded'
    }
}).success(function (data) {
    console.log(' data ');
});

Check out this SO answer.

The way your code used the $http object forces angular to post a json object

Community
  • 1
  • 1
geoand
  • 60,071
  • 24
  • 172
  • 190
  • Can you post the HTTP request that is sent over the network? It is easy to see this using the network tab on Firebug or Chrome dev tools – geoand Jun 24 '14 at 10:57
  • Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8,ta;q=0.6 Cookie: JSESSIONID=C5AADCF13827B76669877E9645E8A3E4 Form Dataview sourceview URL encoded {"id":1,"password":"D A2ph3tWAEE92N1Lh8J7dBtBlg:","newpassword":"asd"} Response Headersview source Allow:GET,OPTIONS Content-Length:1034 Content-Type:text/html;charset=utf-8 Date:Tue, 24 Jun 2014 11:02:50 GMT Server:Apache-Coyote/1.1 ConsoleSearchEmulationRendering – user2724215 Jun 24 '14 at 11:06