{ -6ren">
gpt4 book ai didi

java - Spark Java IllegalArgumentException : class org. eclipse.jetty.server.handler.ErrorHandler 声明多个名为 _listeners 的 JSON 字段

转载 作者:行者123 更新时间:2023-12-02 10:08:54 49 4
gpt4 key购买 nike

我正在使用 Java Spark 开发一个简单的项目,目前我正在尝试实现一些 RESTful 服务。我有这个帖子方法:

post("/users", (request, response) -> {
response.type("application/json");
User user = new Gson().fromJson(request.body(),User.class);
userService.addUser(user);
return new Gson().toJson(response);
});

我使用 postman 来指定请求的正文:

{   "id": "1012", 
"name": "Mac",
"lastname": "Mason1",
"email": "email"
}

用户类是:

 public class User {

public int id;
public String name;
public String lastname;
public String email;

public User (int id, String name, String lastname, String email) {
this.id = id;
this.name = name;
this.lastname = lastname;
this.email = email;
}

每当我尝试执行发布请求时,都会弹出以下错误:

ERROR spark.http.matching.GeneralError - 
java.lang.IllegalArgumentException: class
org.eclipse.jetty.server.handler.ErrorHandler declares multiple JSON fields
named _listeners

最佳答案

return new Gson().toJson(response);

此代码尝试将 response 对象序列化为 JSON。这可能是一个拼写错误(当然你的意思是return new Gson().toJson(user);,或者完全不同的东西)并且由于Gson尝试递归序列化类中所有字段的方式而失败响应的层次结构

关于java - Spark Java IllegalArgumentException : class org. eclipse.jetty.server.handler.ErrorHandler 声明多个名为 _listeners 的 JSON 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55139572/

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