简体   繁体   中英

How to pass user defined objects to REST service from angularjs

I am fairly new to Anuglar JS so please keep that in mind while answering this question. My goal here is to create and persist a new user in the oracle database. My frontend is written in AngularJS. A REST service (using JERSEY) is in place already with a method signature like below:

 @POST
 @Path("/create/{user}")
 public void createUser(@QueryParam("user") User user){ ...}

AngularJS Factory with REST call looks like this:

angular.module('app.services', ['ngResource'])
.factory('User', function($resource) 
 {
   return 
   {
        createUser: $resource('/myurl/create/:user', {}, {
            query: {
                method: 'POST',
                params: {user: '@user'},
                isArray: false
            }
        })
   });

User.java has the following attributes:

private String firstName;
private String lastName;
private String email;

I believe the above factory is set up correctly. I have verified that the backend REST service for create user also works correctly. The frontend has a form that lets a user enter FirstName, LastName and Email which should be part of the User object. I think the User object should be submitted to the Rest service via something like:

Step1: make an object with user's data Step2: User.createUser.query(user:)

I am not sure if the steps are right and if they are then how exactly to make the object. Can someone please provide some guidelines. Thank you

The QueryParam are commonly used with GET methods to send single attributes, Try this :

In your java code

 @POST
 @Path("/create/")
 public void createUser(User user){ ...}

In your js code

angular.module('app.services', ['ngResource'])
.factory('User', function($resource){

    var user = $resource('/myurl/create/', {}, {
      createUser: {method:'POST', params:{...}}
    });

    return user;        
});

You can find more information about rest in java here and here , about angular resources here .

Your resources should reflect the object not the action, your resource should be called "user". Creating, updating, querying is reflected in the http methods.

I'd implement it something like this. A large portion of this is lifted straight from the docs - http://docs.angularjs.org/api/ngResource .$resource

angular.module('app.services', ['ngResource'])

.controller('MyController', function(User) {

    // Query userID 123, change the firstname and lastname then save.
    var user = User.get({userId:123}, function() {
      user.firstname = "joe";
      user.lastname = "bloggs";
      user.$save();
    }

   // Create a new user like this.
   var anotherUser = new User({
     username: 'joe.bloggs',
     email: 'joe.bloggs@example.com',
     firstname: 'joe',
     lastname: 'bloggs'
   });

   anotheruser.$save();
 })    

.factory('User', function($resource) 
 {       
    // Represent the user resource.
    return $resource('/myurl/create/:userId', {userId:'@id'});    

 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM