问题
I have this Spring Repository:
public interface MessageRepository extends CrudRepository<MessageObject, String>{
public List<MessageObject> findByEmisorOrDestinatario(String emisor, String destinatario);
}
My DAO is:
@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() {
}
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);
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:
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?
Things I tried: changing Mediatype to GenericType
EDIT I tried removing the /
from the path, still getting status 400
回答1:
Solved. Problem was I was using .queryparam
without key-value structure. So changing .queryparam(mail)
to .queryparam("mail", mail)
solved it.
回答2:
Try to test with this :
@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);
}
来源:https://stackoverflow.com/questions/53659213/status-400-and-error-deserializing-list-of-objects-no-default-constructor-found