gpt4 book ai didi

java - 仅使用至少 2 个参数进行 JSON 反序列化

转载 作者:行者123 更新时间:2023-11-30 11:07:01 29 4
gpt4 key购买 nike

我正在为 TomEE Plus 1.7.1 实现一个 RESTful 服务应用程序,并将 Jettison 作为默认的 json 提供程序。我的实体类有几个外观类,可以为它们中的每一个提供 CRUD 功能。服务门面由 netbeans 生成。

这是 POST 方法:

@POST
public void create(Course entity) {
super.create(entity);
}

使用此方法(在数据库中创建新实例)时出现以下错误:

No message body reader has been found for request class Object, ContentType : application/json.

经过几个小时的尝试,我让它工作了:我只需要向该方法添加另一个参数,就像这样:

@POST
public void create(@Context Context uriInfo, Course entity) {
super.create(entity);
}

我不明白为什么我必须添加这个 Context 参数。我不需要上下文变量,所以实际上我想删除它.​​..

有人知道原因吗?

最佳答案

好的,我想我找到了解决方案:

我所有的其余服务都已作为外观类实现。抽象外观(所有服务的父类(super class))有几个方法,如:

public void create(T entity) { getEntityManager().persist(entity); }
public void edit(T entity) {getEntityManager().merge(entity);}

这些方法被外观类使用:

public void create(Course entity) {
super.create(entity);
}

public void edit(@PathParam("id") Integer id, Course entity) {
super.edit(entity);
}

(为了更好看,我已经删除了这里的注释)

这两个方法的区别在于,edit 方法有第二个参数“id”,因此不会覆盖父类(super class)的 edit() 方法。但是 create() 方法确实只有一个参数,这会导致覆盖父类(super class)方法“create()”。我不知道为什么,但 cxf 现在正在创建两个端点:

POST http://localhost:8080/webprog/api/course/  ->      void create(Course)           

POST http://localhost:8080/webprog/api/course/ -> void create(Object)

这也是我让它使用第二个参数的原因:create() 方法不再被覆盖。

所以我现在所做的只是重命名父类(super class)中的方法,而不是在外观类中覆盖它们。

顺便说一句:所有服务类都是由 netbeans 生成器创建的...可能其中有一个错误

关于java - 仅使用至少 2 个参数进行 JSON 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29083172/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com