1

I am extremely new to spring, but I managed to come up with this code from my javascript

$scope.addMe = function(){
        var params = {
            date :111111,
            username: 'thisGuy',
            notetext: 'this is a new note that was just added'
        };
        $http.post(afHttp.baseUrl + "/blah/" + $scope.dog.id, {params: params})
            .success(function(data) {
            });
    }

and in elclipe I have

@POST
@Path("/{id}")
public void getAddMssg(@PathParam("id") int id, @FormParam("date") int date, @FormParam("username") String username, @FormParam("notetext") String notetext) {
    System.out.println("date:      "+date);
    System.out.println("username:   "+ username);
    System.out.println("notetext:   "+ notetext);
    System.out.println("id:   "+ id);

}

And when I look at my console log in eclipse it says

date:   0
username:   null
notetext:   null
id:   123124

What am I doing wrong? I have been working on this for awhile with no avail. Can someone please help?

Oh sorry I forgot to add I am using angularjs.

alex wilhelm
  • 233
  • 5
  • 16

2 Answers2

2

Pass params into that argument, not {params:params}. Also, add the Content-Type header as the third argument:

$http.post(afHttp.baseUrl + "/blah/" + $scope.dog.id, params, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded'}})

And on the server-side, proceed @POST with @Consumes(MediaType.APPLICATION_FORM_URLENCODED):

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)

The fully qualified name for MediaType is javax.ws.rs.core.MediaType, in case you need to import it.

sdanzig
  • 4,510
  • 1
  • 23
  • 27
1

what is $http? if it is jQuery, post method need 'data' not "params". http://api.jquery.com/jQuery.post/

baltov
  • 194
  • 5
  • I am using angularjs, sorry about that. – alex wilhelm Oct 26 '13 at 15:53
  • ok, that is a error: replace {params:params} with params only: [link](http://docs.angularjs.org/api/ng.$http#methods_post) post(url, data, config) – baltov Oct 26 '13 at 16:02
  • I don't quite undestand, can you give an example of what you mean? – alex wilhelm Oct 26 '13 at 16:06
  • Oh sorry I was looking at the wrong thing, what would config be though? – alex wilhelm Oct 26 '13 at 16:08
  • $http.post(afHttp.baseUrl + "/blah/" + $scope.dog.id, params) .success(function(data) { }); In AngularjS post methot take second parameter data object, you send other object with name param. I think so is all right. Third parameter is optional. Not needed for you. – baltov Oct 26 '13 at 16:08
  • Yeah, I am still getting 0's and nulls – alex wilhelm Oct 26 '13 at 16:13
  • ok, add " HttpServletRequest request" as last parameter in java method and debug what is in. In _parameters variable. Sorry now i can't create a project to test that. – baltov Oct 26 '13 at 16:19
  • Can you elaberate on what you mean/ example? I have been googling HttpServletRequest request and I have not come up with anything that uses it as a java param. – alex wilhelm Oct 26 '13 at 16:30
  • [link](http://stackoverflow.com/questions/8504258/spring-3-mvc-accessing-httprequest-from-controller) I mean that: public void getAddMssg(@PathParam("id") int id, @FormParam("date") int date, @FormParam("username") String username, @FormParam("notetext") String notetext, HttpServletRequest request) {... Show us, please, new code for javascript function? – baltov Oct 26 '13 at 16:33
  • Am I suppose to use it in the body too? – alex wilhelm Oct 26 '13 at 16:35
  • debug it, set a breakpoint in first line in body, and see what is in – baltov Oct 26 '13 at 16:36
  • I have never really used debug before, but here are the errors I got for running it and putting a breaker on the first line: SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container com.sun.jersey.api.container.ContainerException: Exception obtaining parameters and Caused by: java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded – alex wilhelm Oct 26 '13 at 16:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40048/discussion-between-baltov-and-alex-wilhelm) – baltov Oct 26 '13 at 17:08