状态400和反序列化对象列表时出错.找不到默认的构造函数 [英] Status 400 and Error deserializing List of Objects. No default constructor found

查看:163
本文介绍了状态400和反序列化对象列表时出错.找不到默认的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Spring Repository:

I have this Spring Repository:

public interface MessageRepository extends CrudRepository<MessageObject, String>{

public List<MessageObject> findByEmisorOrDestinatario(String emisor, String destinatario);
}

我的DAO是:

@Entity
@Table(name = "messages")
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;
@JsonFormat(pattern="yyyy-MM-dd")
private LocalDate fecha;
private String id_housing;

public MessageObject() {

  }

现在在我的控制器中,我想接收Get请求并在我的数据库中搜索,以便:

Now in my Controller I want to receive the Get request and search in my DB so:

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

}   

}

现在,我可以从客户端致电该服务了,

Now I can call the service from my client, so:

ClientConfig config = new ClientConfig();
    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(status);
    MessageObject[] listMessages = respuesta.readEntity(MessageObject[].class);

问题:我收到的状态码为400.反序列化entryRead时也会出错.使用Postman进行请求不会返回任何错误,并且会以JSON格式返回对象列表.

Problems: I'm receiving a 400 status code. Also an error deserializing entityRead. Doing the request with Postman returns no errors and return the list of objects in JSON format.

StackTrace:

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

问题:我怎么知道我的代码在哪里失败?我是否很好地使用了服务调用?

Question: how can I know where is my code failing? am I using the service invocation well?

我尝试过的事情:将Mediatype更改为GenericType

Things I tried: changing Mediatype to GenericType

编辑,我尝试从路径中删除/,但仍获得状态400

EDIT I tried removing the / from the path, still getting status 400

推荐答案

已解决.问题是我正在使用没有键值结构的.queryparam.因此,将.queryparam(mail)更改为.queryparam("mail", mail)即可解决此问题.

Solved. Problem was I was using .queryparam without key-value structure. So changing .queryparam(mail) to .queryparam("mail", mail) solved it.

这篇关于状态400和反序列化对象列表时出错.找不到默认的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆