【问题标题】:RequestBody POJO always emptyRequestBody POJO 始终为空
【发布时间】:2015-04-08 08:09:49
【问题描述】:

我想使用 Spring 将 HTML 表单(使用 AngularJS)中的一些数据存储到我的数据库中。 为此,我将@RequestBody 注释与 POJO 一起使用,但我无法使其工作:我的 POJO 已实例化,但看起来 POJO 属性未与我的表单值映射(它们都是空的) .

控制器:

@RequestMapping(value = "/createEntities", method = RequestMethod.POST)
@ResponseBody
public List<Entity> createEntities(@RequestBody final EntityList resource, @RequestParam final String kind) {
    System.out.println("Creating entity for: " + kind);
    Preconditions.checkNotNull(resource);
    List<Entity> newEntities = new ArrayList<Entity>();
    System.out.println("Entity test = " + resource.getTest()); // Prints "Entity test = null"
    // Code below returns NullException
    //System.out.println("Entity list nb = " + resource.getEntity().size());
    if (resource.getEntities() != null && !resource.getEntities().isEmpty()) {
        System.out.println("Entity list is OK");
        for (EntityForm eForm : resource.getEntities()) {
            if (eForm.getGrant() != null) {
                Entity ent = new Entity();
                if ("RTS".equals(kind)) {
                    ent.setDept(deptService.findByAbr(DeptEnum.RTS.name()));
                } else {
                    ent.setDept(deptService.findByAbr(DeptEnum.RTB.name()));
                }
                ent.setGrant(eForm.getGrant());
                ent.setCountry(eForm.getCountry());
                ent.setName(eForm.getName());
                ent = service.create(ent);
                newEntities.add(ent);
            }
        }
    }
    return newEntities;
}

EntityList 是我表单的 POJO。此 POJO 包含 EntityForm 列表(+ 用于测试目的的字符串),这是我的数据库实体 Entity 的 DTO。

EntityList POJO:

public class EntityList implements Serializable {
    private static final long serialVersionUID = 6335318686899794229L;
    private List<EntityForm> entities;
    private String test;

    public EntityList() {
        super();
    }

    public EntityList(List<EntityForm> entities, String test) {
        super();
        this.entities = entities;
        this.test = test;
    }

    public List<EntityForm> getEntities() {
        return entities;
    }

    public void setEntities(List<EntityForm> entities) {
        this.entities = entities;
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    } 
}

我认为问题出在我的表单中的实体列表与我的 POJO 中的 List&lt;EntityForm&gt; 之间的错误映射,这就是为什么我在我的 POJO 中添加了一个简单的字符串。

AngularJS 方面

服务:

app.factory("Entities", function($resource) {
    return $resource("api/auth/entities", null, 
    {
        createEntities: {method:'POST', url: "api/auth/entities/createEntities", params: { kind: '@kind' }, isArray:true}
    });
})

控制器:

$scope.EntForm = {};
$scope.EntForm.entities = [];
$scope.EntForm.test = "myTest";

/* ... */

$scope.saveEnt= function() {
    console.log($scope.EntForm);
    Entities.createEntities($scope.EntForm,{kind:"RTS"},function(res) {
        var msg = 'Entities created...'; 
        ngToast.create(msg); 
        $location.path("/entities");
    });
}

在我的 Firefox 控制台中,我看到 $scope.EntForm 设置正确(我的所有实体对象都设置了字段,以及控制器中定义的测试字符串)。

结果

所有这些代码都会显示:

Creating entity for: RTS
Entity test = null

我做错了什么?

【问题讨论】:

    标签: angularjs spring spring-mvc jakarta-ee


    【解决方案1】:

    您是否使用 Firefox 开发人员工具检查过 POST 有效负载,您的自定义 createEntities 方法是否正常工作?

    (会将此添加为评论,但不幸的是我还没有足够的声誉。)

    【讨论】:

    • 如果你说的是不使用 AngularJS createEntities 方法发送我自己的 POST 请求,我不能。有身份验证要求(我不知道它是如何工作的,因为我没有对应用程序的这一部分进行编码,所以我不能直接关闭它),如果我尝试发送它,每个 GET/POST 请求都会失败火狐开发工具。还有其他方法可以测试我的方法是否有效吗?
    • @Chugrothas 我的意思是检查您的 AngularJS 应用程序生成的实际 POST 请求内容。我有一段时间没有出于开发目的使用 Firefox,甚至没有在我当前的计算机上安装它,所以我自己无法检查它,但是使用 Web Console 你应该能够监控例如网络提出的要求。然后您应该能够检查您的 AngularJS 应用程序是否正确填充了 POST 内容。
    • 哦,是的,对不起,我完全忘记了 Firefox 控制台中网络查看器的存在。我检查了,发送的 POST 请求具有正确的参数(我可以看到我的实体和“测试”字符串)。
    • 好的,这缩小了一点。 :) 不幸的是,我已经有一段时间没有使用 Spring 后端了,但也许这是 JSON -> POJO 反序列化的问题。也许this SO question + answers 可以帮助你找到正确的方向?
    • @LShetty 很抱歉以这种方式发表评论,但您是否没有阅读说 的部分(会将此添加为评论,但不幸的是我还没有足够的声誉.)?是的,如果我当时有足够的声誉(50+)能够这样做,我会在开场白下面发表评论。但是,是的,谢谢您的批评。
    【解决方案2】:

    我不得不从我的 Spring 控制器中删除 @RequestParam final String kind 部分,以及 AngularJS 代码中的参数。 为了得到那种,我刚刚在我的 AngularJS 控制器中添加了$scope.EntForm.kind = "theValueIWant"

    我不知道这是否是使其在良好实践方面发挥作用的好方法,但我现在得到了 @RequestBody 内容。

    【讨论】:

      猜你喜欢
      • 2016-03-09
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      相关资源
      最近更新 更多