用图像反序列化Object时,Json失败。找不到事件START_ARRAY

时间:2018-12-11 18:09:40

标签: json rest spring-boot blob json-deserialization

我有这个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);
    }

使用Postman进行测试将以JSON格式返回列表: enter image description here

问题:当我尝试从客户端调用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失败?

0 个答案:

没有答案