0

I am running a REST service in Wildfly and I am attempting to use the Request.evaluatePreconditions method to generate a 304 Not Modified response.

I am passing an eTag value in the request's If-None-Match header, but the evaluatePreconditions method is not generating the 304 response even when the EntityTag matches the value in the If-None-Match header.

Here is the code:

import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.inject.Inject;
import javax.persistence.PersistenceException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.EntityTag
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;

import com.mycompany.DataClient;
import com.mycompany.ResponseData;

@Path ("entities")
@Stateless
@TransactionManagement (TransactionManagementType.BEAN)
public class RESTService
{
    @Inject
    private DataClient m_DataClient;

    @GET
    @Produces (MediaType.APPLICATION_JSON)
    public Response findAllEntities (@Context Request request)
    {
        Response.ResponseBuilder response = null;

        try
        {
            ResponseData responseData = m_DataClient.findAll ();

            EntityTag tag = new EntityTag (responseData.getETag ());

            response = request.evaluatePreconditions (tag);

            if (response == null)
            {
                response = Response.status (responseData.getStatusCode ()).entity (responseData.getEntities ()).tag (responseData.getETag ());
            }
        }
        catch (IllegalStateException | PersistenceException | InterruptedException | ExecutionException ex)
        {
            m_Logger.log (Level.SEVERE, "Find All Data Exception", ex);
            response = Response.serverError ();
        }

        return response.build ();
    }
}
  • ResponseData.getStatusCode () returns an int that corresponds to an HTTP status code.
  • ResponseData.getEntities () returns a List of serializable objects.
  • ResponseData.getETag () returns a String that contains a hash that corresponds to the entity objects returned by getEntities.

Here's my request to the server:

curl -H "Accept:application/json" -H "If-None-Match:7056166173985" -H "Authorization:Bearer adsf" -k -v https://mycompany.com/entities

Here's the response header from the server:

< HTTP/1.1 200 OK
< ETag: "7056166173985"
< X-Powered-By: caffeine
< Access-Control-Allow-Credentials: true
< Content-Type: application/json
< Content-Length: 1227
< Date: Fri, 27 Oct 2017 02:00:39 GMT
< Via: 1.1 google
< Alt-Svc: clear
<

As you can see, the ETag value in the response header matches the value in the If-None-Match request header. Why am I not receiving a 304 Not Modified response?

4

1 回答 1

0

我发现了这个问题。问题在于我的代码的另一部分将 eTag 值加载到responseData对象中。结果,该responseData.getETag ()方法返回了一个包含开始和结束双引号的字符串。我能够通过改变来让事情正常进行

EntityTag tag = new EntityTag (responseData.getETag ());

EntityTag tag = new EntityTag (responseData.getETag ().replaceAll ("^\"|\"$", ""));

具体来说,我正在从远程服务器中提取我的数据和关联的 eTag,并且我正在使用Apache HttpClient来检索它们。以下代码从标头中检索 eTag:

private String retrieveETag (HttpResponse response)
{
    Header eTagHeader = response.getLastHeader ("etag");

    if (eTagHeader != null)
    {
        return eTagHeader.getValue ();
    }

    return null;
}

原始 eTag 标头是ETag: \"etagvalue\". 我原以为会eTagHeader.getValue ()回来etagvalue,但它却回来了\"etagvalue\"

于 2017-10-30T22:32:37.390 回答