如何通过JAX-RS和Jackson(Quarkus)接收带有多个节点的JSON文件
当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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有注意到这是一件简单的事情。我忘了将值设置在限制器上。因此,我只需要将
@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.