Java 如何从 angularjs 将用户定义的对象传递给 REST 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19543654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:14:09  来源:igfitidea点击:

How to pass user defined objects to REST service from angularjs

javarestangularjs

提问by chapstick

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:

我对 Anuglar JS 还很陌生,所以在回答这个问题时请记住这一点。我的目标是在 oracle 数据库中创建并保留一个新用户。我的前端是用 AngularJS 编写的。REST 服务(使用 JERSEY)已经到位,方法签名如下:

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

AngularJS Factory with REST call looks like this:

带有 REST 调用的 AngularJS 工厂如下所示:

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:

User.java 具有以下属性:

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:

我相信上述工厂设置正确。我已经验证了用于创建用户的后端 REST 服务也可以正常工作。前端有一个表单,允许用户输入名字、姓氏和电子邮件,它们应该是用户对象的一部分。我认为 User 对象应该通过以下方式提交给 Rest 服务:

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

Step1:用用户的数据创建一个对象 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

我不确定这些步骤是否正确,如果它们正确,那么如何制作对象。有人可以提供一些指导方针。谢谢

采纳答案by eddiec

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

你的资源应该反映对象而不是动作,你的资源应该被称为“用户”。创建、更新、查询都体现在 http 方法中。

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

我会像这样实现它。其中很大一部分是直接从文档中提取的 - 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: '[email protected]',
     firstname: 'joe',
     lastname: 'bloggs'
   });

   anotheruser.$save();
 })    

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

 });

回答by Maxi Baez

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

QueryParam 通常与 GET 方法一起使用来发送单个属性,试试这个:

In your java code

在你的java代码中

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

In your js code

在你的 js 代码中

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 hereand here, about angular resources here.

您可以在此处此处找到有关 Java 中休息的更多信息,在此处找到有关 angular 资源的信息