简体   繁体   English

客户端响应过滤器读取实体问题(泽西)

[英]Client response filter read entity problem (Jersey)

Im using Jerseys client side filters for caching.我使用 Jerseys 客户端过滤器进行缓存。 Request for reading and response for caching values.请求读取和响应缓存值。 How to read entity body from client response filter, dont have methods like read, getEntity when using normal client calls from client builder.如何从客户端响应过滤器中读取实体主体,在使用来自客户端构建器的普通客户端调用时没有读取、getEntity 等方法。 Only method i found maybe usefull is getEntityStream but not having much luck with it.我发现可能有用的唯一方法是 getEntityStream 但运气不佳。 For caching im using EhCache.使用 EhCache 缓存即时消息。 Im novice user help:)我是新手用户帮助:)

REQUEST:要求:

public class ClientCacheResponseFilter implements ClientResponseFilter公共 class ClientCacheResponseFilter 实现 ClientResponseFilter

private Ehcache ehcache;

public ClientCacheRequestFilter(Ehcache ehcache) {
    this.ehcache = ehcache;
}

@Override
public void filter(ClientRequestContext request) throws IOException {
    System.out.println("REQUEST FILTER");

    if (!request.getMethod().equalsIgnoreCase("GET")) {
        return;
    }
    String key = request.getUri().toString();
    System.out.println("REQ KEY: " + key);

    System.out.println("LOOKING FOR CACHE");
    if (!ehcache.getClientCache().containsKey(key)) {
        System.out.println("EMPTY CACHE");
        return;
    }

    Cache<String, Object> cache = ehcache.getClientCache();
    Object value = cache.get(key);
    if (value != null) {

        System.out.println("REQUEST FILTER - SECOND TIME READING CACHE");
        System.out.println("CACHE ENTRY VALUE: " + value);

        Response response = Response.ok()
                .entity(value)
                .type(MediaType.APPLICATION_JSON)
                .build();

        System.out.println("SENDING ENTITY");
        request.abortWith(response);
    }
}

RESPONSE:回复:

private Ehcache ehcache;

public ClientCacheResponseFilter(Ehcache ehcache) {
    this.ehcache = ehcache;
}

@Override
public void filter(ClientRequestContext request, ClientResponseContext response) throws IOException {
    System.out.println("RESPONSE FILTER");
    if (!request.getMethod().equalsIgnoreCase("GET")) {
        return;
    }

    if (response.getStatus() == 200) {

        System.out.println("CACHING VALUE");
        String key = request.getUri().toString();

        ehcache.getClientCache().put(key,  ?);  // GET ENTITY FROM RESPONSE ?
        
    }
}

CLIENT CALL:客户来电:

    WebTarget webTarget = CLIENT.target(API_URL).path("games");
    Invocation.Builder builder = webTarget.request(MediaType.APPLICATION_JSON);
    
    try (Response response = builder.get()) {
        
        if (response.getStatus() == 200) {
            return response.readEntity(new GenericType<List<Game>>() {
            });
        }
    }
    return Collections.EMPTY_LIST;
}

Assuming you are using Jersey 2.x, the ClientResponseContext can be cast to the Jersey specific ClientResponse , which has the readEntity(..) method (similar to the client Response#readEntity() method).假设您使用的是 Jersey 2.x,则ClientResponseContext可以转换为 Jersey 特定的ClientResponse ,它具有readEntity(..)方法(类似于客户端Response#readEntity()方法)。 You also can call bufferEntity() before you read the entity.您还可以在读取实体之前调用bufferEntity() This way the client will be able to read the entity again.这样,客户端将能够再次读取实体。

So for example if you want to get the JSON string response, you can simply do因此,例如,如果您想获得 JSON 字符串响应,您可以简单地执行

// in response filter
ClientResponse res = (ClientResponse) response;
res.bufferEntity();
String jsonResponse = res.readEntity(String.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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