Resteasy @FormParam null when passing params with AngularJS

chidar

I'm trying to use AngularJS in front end and Resteasy as a Rest API. My problem is that my @FormParam are always null when sending params with AngularsJS.

Here is how my JS :

$scope.addPerson = function(newFirstName, newLastName) {
            $http({
                method: 'POST',
                url: "rest/persons/savePerson",
                data: {firstName: 'newFirstName', lastName: 'newLastName'},
                headers: {'Content-Type': 'application/json'}
            })
            .success(function(data) {
                alert("DATA  : " + data);
            }).error(function(data, status, headers, config) {
                alert("Could not save new person");
            });
        };

And this is my code server side :

@POST
@Path("/savePerson")
@Produces("application/json")
@Secured({ "ROLE_USER" })
public PersonBean savePerson(@FormParam("firstName") String firstName,
        @FormParam("lastName") String lastName) {
    if (firstName== null || lastName== null) {
        return null;
    }
    PersonBean person = personDao.savePerson(firstName,
            lastName);
    return person ;
}

Any help is appreciated.

slipperyseal

You are posting the fields as JSON, not as form encoded data.

headers: {'Content-Type': 'application/json'}

But you can't just change this header. The values will need to be Form encoded. See this page on how to post form data from Angular.

http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm

Although, you may find it better to stick with JSON and let your framework map these fields to a Bean for you.

I haven't used Resteasy but I think it should be as simple as...

@POST
@Path("/savePerson")
@Consumes("application/json")
@Produces("application/json")
@Secured({ "ROLE_USER" })
public PersonBean savePerson(SavePersonBean savePersonBean) {
    PersonBean person = personDao.savePerson(savePersonBean.getFirstName(),
            savePersonBean.getLastName());
    return person;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

confusion with params passing and routes

分類Dev

Passing Null Value when loading an async map

分類Dev

Null reference exception when passing EditText as parameter

分類Dev

$ stateProvider params null with ionic

分類Dev

getting null value in list when passing by ajax call to mvc controller

分類Dev

angularJS set route params in controller

分類Dev

GET Params in AngularJS Route URL

分類Dev

Best practice for passing enum params in Web API

分類Dev

Best practice for passing enum params in Web API

分類Dev

Python requests module not passing params in session

分類Dev

Magento - passing params in URL to template file

分類Dev

@Context object not injected when unit testing resteasy

分類Dev

Passing parameters to AngularJS $timeout

分類Dev

Angular 2 passing object via route params possible?

分類Dev

How can I get back to previous page with passing params?

分類Dev

Direct Link to URL with route params breaks AngularJS App

分類Dev

Angularjs $http.post - sending params as JSON to ASPX webmethod

分類Dev

sent params from angularJs to php symfony2 api

分類Dev

Passing Date/moment object via attribute in AngularJS

分類Dev

AngularJS: Navigate passing GET parameters in URL

分類Dev

Warnings when passing arguments to a function

分類Dev

Params object []の値は常にnullです

分類Dev

Angularjs 1x Passing Functions to AngularJS Directives

分類Dev

Problem with Passing Null String Between Classes in Java

分類Dev

Passing the null object from a test method

分類Dev

Data from View to controller is passing null value

分類Dev

InvalidCastException - passing null values to stored procedure

分類Dev

No parent found for "avoid passing null as the view root"

分類Dev

When are variables evaluated when passing them into functions?

Related 関連記事

  1. 1

    confusion with params passing and routes

  2. 2

    Passing Null Value when loading an async map

  3. 3

    Null reference exception when passing EditText as parameter

  4. 4

    $ stateProvider params null with ionic

  5. 5

    getting null value in list when passing by ajax call to mvc controller

  6. 6

    angularJS set route params in controller

  7. 7

    GET Params in AngularJS Route URL

  8. 8

    Best practice for passing enum params in Web API

  9. 9

    Best practice for passing enum params in Web API

  10. 10

    Python requests module not passing params in session

  11. 11

    Magento - passing params in URL to template file

  12. 12

    @Context object not injected when unit testing resteasy

  13. 13

    Passing parameters to AngularJS $timeout

  14. 14

    Angular 2 passing object via route params possible?

  15. 15

    How can I get back to previous page with passing params?

  16. 16

    Direct Link to URL with route params breaks AngularJS App

  17. 17

    Angularjs $http.post - sending params as JSON to ASPX webmethod

  18. 18

    sent params from angularJs to php symfony2 api

  19. 19

    Passing Date/moment object via attribute in AngularJS

  20. 20

    AngularJS: Navigate passing GET parameters in URL

  21. 21

    Warnings when passing arguments to a function

  22. 22

    Params object []の値は常にnullです

  23. 23

    Angularjs 1x Passing Functions to AngularJS Directives

  24. 24

    Problem with Passing Null String Between Classes in Java

  25. 25

    Passing the null object from a test method

  26. 26

    Data from View to controller is passing null value

  27. 27

    InvalidCastException - passing null values to stored procedure

  28. 28

    No parent found for "avoid passing null as the view root"

  29. 29

    When are variables evaluated when passing them into functions?

ホットタグ

アーカイブ