如何通过JAX-RS和Jackson(Quarkus)接收带有多个节点的JSON文件

发布于 2025-01-25 14:22:33 字数 1491 浏览 2 评论 0原文

当Im Tryin通过我的JAX-RS API获得发布请求时,它总是发送无效的值。我不知道杰克逊注释是不正确的还是我需要使用对象拍摄者。

这些是我的类:

public class ClassA{

  private String name;
  private ClassB classB;

public ClassA(){}

public ClassA(String name, ClassB classB){
   this.name = name;
   this.classB = classB;
}

@JsonGetter
public String getName(){ return name; }
@JsonGetter
public ClassB getClassB(){ return classB; }

classB

public class ClassB{

@JsonProperty("type")
private String type;
@JsonProperty("number")
private int number;

public ClassB(){}

@JsonPropertyOrder({"type, number"})
public ClassB(String type, int number){
   this.type= type;
   this.number= number;
}

@JsonGetter
public String getType(){ return type; }
@JsonGetter
public int getNumber(){ return number; }

这是我的JSON文件的

{
  "type": "typeExample;
  "classB": {
    "type": "classBTypeExample";
    "int": 10;
  }
}

:我希望杰克逊读取该文件,然后在列表中添加对象类型的classa(问题是甚至没有读取它) 这是API代码:

@Path("/path")
public class Requests {
    private Set<ClassA> classesA = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));

    @GET
    public Set<ClassA> list() {
            return classesA;
        }

    @POST
    public Set<ClassA> add(ClassA classA){
        classesA.add(classA);
        return classesA;
    }
}

我已经添加了Quarkus.jackson.fail-On-in-in-innown-Properties = true true

When im tryin got do a Post request through my JAX-RS API it always sends a null value. I dont know if the Jackson annotations are incorrect or if i need to use an ObjectMapper.

These are my classes:

public class ClassA{

  private String name;
  private ClassB classB;

public ClassA(){}

public ClassA(String name, ClassB classB){
   this.name = name;
   this.classB = classB;
}

@JsonGetter
public String getName(){ return name; }
@JsonGetter
public ClassB getClassB(){ return classB; }

and this is the classB

public class ClassB{

@JsonProperty("type")
private String type;
@JsonProperty("number")
private int number;

public ClassB(){}

@JsonPropertyOrder({"type, number"})
public ClassB(String type, int number){
   this.type= type;
   this.number= number;
}

@JsonGetter
public String getType(){ return type; }
@JsonGetter
public int getNumber(){ return number; }

My JSON file:

{
  "type": "typeExample;
  "classB": {
    "type": "classBTypeExample";
    "int": 10;
  }
}

I want Jackson to read the file and then add an Object type ClassA to a list (the problem is that is not even reading it)
This is the API code:

@Path("/path")
public class Requests {
    private Set<ClassA> classesA = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));

    @GET
    public Set<ClassA> list() {
            return classesA;
        }

    @POST
    public Set<ClassA> add(ClassA classA){
        classesA.add(classA);
        return classesA;
    }
}

I already added the quarkus.jackson.fail-on-unknown-properties=true to the application.properies file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2025-02-01 14:22:33

我没有注意到这是一件简单的事情。我忘了将值设置在限制器上。因此,我只需要将@ConsustructorProperties({})添加到类构造函数中,然后才能完成。

It was a simple thing that i didnt notice. I forgot to set the values on the constructer. So i just had to add the @ConstructorProperties({}) to the classes constructors and it worked out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文