反序列化对象列表时,JSON查找不存在的类

时间:2018-12-07 00:15:39

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

使用Spring,我创建了一个微服务,该服务在DB中查找特定用户的所有消息。

控制器:

@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);   

}

}

DAO:

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);

stacktrace:

 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: enter image description here

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

1 个答案:

答案 0 :(得分:0)

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