我有这个DAO:
@Entity
@Table(name="housing")
public class HousingObject implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String idhousing;
private String owner;
@Lob
private byte[] photo;
private int guests;
* more stuff here *
public HousingObject() {
}
* getters and setters *
我的API搜索属于特定用户的所有房屋,并将所有房屋存储在列表中:
@RequestMapping(value = "/housings", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<?>
getAllHousingsbyEmail(@RequestParam("email") String email){
Optional<List<HousingObject>> lista = daoHsg.findByowner(email);
if (lista.isPresent()){
return new ResponseEntity<>(lista, HttpStatus.OK);
}
问题:当我尝试从客户端调用API时,我在读取实体响应时遇到问题:
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient();
WebTarget webResource = client.target("http://localhost:10505").path("housings").queryParam("email",mail);
Invocation.Builder invocationBuilder = webResource.request(MediaType.APPLICATION_JSON);
Response respuesta = invocationBuilder.get();
int status = respuesta.getStatus();
if (status != 200) {
request.setAttribute("listaAlojamientos", housingResult);
return "not_found.jsp";
}
HousingObject[] listHousings = respuesta.readEntity(HousingObject[].class);
基本上在readEntity
上引发异常,并且堆栈跟踪为:
javax.ws.rs.ProcessingException:
Error deserializing object from entity stream.
Caused by: javax.json.bind.JsonbException: Internal error:
Event START_ARRAY not found. Last data: [EVENT: KEY_NAME KEY_NAME: guests]
问题:为什么json在readEntity上失败?我认为可能是因为我的形象。我将图像存储在Blob中,因为它们是byte[]
。为什么邮递员获得房屋清单,但java json失败?