状态400和反序列化对象列表时出错。找不到默认构造函数

时间:2018-12-06 20:29:55

标签: json spring rest spring-boot jsonb

我有这个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请求并在我的数据库中搜索,以便:

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

}   

}

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

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状态代码。反序列化EntityRead时也会出错。使用Postman进行请求不会返回错误,并且会以JSON格式返回对象列表。

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

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

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

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

尝试对此进行测试:

   @RestController
    public class Controller {

    @Autowired
    private MessageRepository daoMsg;

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

    }