我有这个Spring仓库:
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格式返回对象列表。
堆栈跟踪:
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
/
解决了。问题是我使用的.queryparam没有键值结构。因此,改变.queryparam(mail)以.queryparam("mail", mail)解决它。
.queryparam
.queryparam(mail)
.queryparam("mail", mail)