【问题标题】:When deserializing List of objects, JSON looks for a unexistent class [duplicate]反序列化对象列表时,JSON 会查找不存在的类 [重复]
【发布时间】:2018-12-07 00:15:39
【问题描述】:

我使用 Spring 创建了一个微服务,可以在数据库中为特定用户查找所有消息。

控制器:

@RestController
public class Controller {

@Autowired
private MessageRepository daoMsg;

@RequestMapping(value = "/Mensajes", method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public List<MessageObject> enviados (@RequestParam("mail") String mail) {
    return daoMsg.findByEmisorOrDestinatario(mail, mail);   

}

}

道:

public class MessageObject implements Serializable{

private static final long serialVersionUID = 1L;
@Id
private String id;
private String emisor;
private String destinatario;
private String mensaje;
private String tipo;
private LocalDate fecha;
private String id_housing;

public MessageObject() {

}

public MessageObject(String id, String emisor, String destinatario, String tipo, LocalDate fecha, String id_housing) {
    this.id = id;
    this.emisor = emisor;
    this.destinatario = destinatario;
    this.tipo = tipo;
    this.fecha = fecha;
    this.id_housing = id_housing;
}

从我的客户端应用调用微服务时:

Client client = ClientBuilder.newClient();

    WebTarget webResource = 
   client.target("http://localhost:8082").path("Mensajes").queryParam(mail);
    Invocation.Builder invocationBuilder = 
    webResource.request(MediaType.APPLICATION_JSON);
    Response respuesta = invocationBuilder.get();
    int status = respuesta.getStatus();
    System.out.println("el status es "+ status);
    MessageObject[] listMessages = 
    respuesta.readEntity(MessageObject[].class);

堆栈跟踪:

 javax.ws.rs.ProcessingException: Error deserializing object from entity 
 stream.Caused by: javax.json.bind.JsonbException: Can't create instance of 
 a class: class [LMessages.MessageObject;, No default constructor 
 found.Caused by: java.lang.NoSuchMethodException: 
 [LMessages.MessageObject;.<init>()

我的客户端与微服务中的客户端具有相同的 MessageObject DAO,在:

问题:如果我的 MessageObject 类在 Messages 包(不是 LMessages)中,为什么 JSON 会在 LMessages.MessageObject 中寻找默认构造函数

【问题讨论】:

  • 您能否通过 ResponseEntity> 更改 List 并添加到您的方法 @ResponseBody 来更改您的 rest 方法的返回类型?
  • 检查您对 MessageObject 的导入在双方(服务器和客户端)是否正确,您可能在其中一个中导入了不同的 messageObject。

标签: json rest spring-boot json-deserialization jsonb


【解决方案1】:

解决了。问题是我使用.queryparam(mail) 没有键值结构,只有键。现在使用.queryparam ("mail", mail) 有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2017-12-30
    相关资源
    最近更新 更多