简体   繁体   中英

Getting null in response.getEntity() in ContainerResponse filter

Sometimes when I call to response.getEntity() , I'm getting null . But I don't understand the reason and I don't know how to ensure that the value that I'm getting in the response.getEntity() will always be not null .

Here is my code:

@Override
public ContainerRequest filter(ContainerRequest request) {
    durationTime = System.currentTimeMillis();
    InputStream in = null;
    try {
        requestBody = IOUtils.toString(request.getEntityInputStream(), Charsets.UTF_8);
        in = IOUtils.toInputStream(requestBody);
        request.setEntityInputStream(in);
    } catch(EOFException ex){
        log.error(ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return request;
}

@Override
public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
    ObjectMapper objMapper=null;
    String output = objMapper.writeValueAsString(response.getEntity());
    return response;
}

Setting the entity stream in the request filter only set's the entity for incoming request . It has nothing to do with the response entity. The getEntity() in the response filter is the response entity, and it will only every have something if you actually return an entity from your resource method . That's where the response entity will come from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM