Restlet Getting HTTP Status code 204 instead of 200

梦想与她 提交于 2021-01-27 20:04:29

问题


For the 1st request, I get the JSON response. From the next request onwards I start getting this log and HTTP Status Code 204, even though the ServerResource is successfully returning a representation

org.restlet.engine.adapter.ServerAdapter commit
WARNING: A response with an unavailable and potentially non empty entity was returned. Ignoring the entity for resource http://localhost:8888/xyz?abc=def

Application class for wiring routes

@Override
public Restlet createInboundRoot() {
    router = new Router(getContext());
    CorsService corsService = new CorsService();         
    corsService.setAllowedOrigins( new HashSet<String>(Arrays.asList("http://example.com")));
    corsService.setAllowedCredentials(true);
    getServices().add(corsService);
    router.attach("/xyz", XYZ.class);
}

Server Resource which handles and returns a JSON Representation

public class XYZ extends ServerResource {

    private static final Logger logger = Logger.getLogger("API:XyZ");

    @Get(":json")
    public Representation handleGetRequest() {
         ..
         return API_RESPONSE_JSON_REPRESENTATION_SUCCESS;
    }
}

回答1:


After releasing the response, the representation state available is set to false. So subsequent calls to the ServerResource, returns the Representation but in handle() method it is set to 204 since getResponseEntity().isAvailable() returns false.

From ServerResource:

@Override
public Representation handle() {
       ...
        } finally {
            if (Status.CLIENT_ERROR_METHOD_NOT_ALLOWED.equals(getStatus())) {
                updateAllowedMethods();
            } else if (Status.SUCCESS_OK.equals(getStatus())
                    && (getResponseEntity() == null || !getResponseEntity()
                            .isAvailable())) {
                getLogger()
                        .fine("A response with a 200 (Ok) status should have an entity. "
                                + "Changing the status to 204 (No content).");
                setStatus(Status.SUCCESS_NO_CONTENT);
            }
        }
    }
    return result;
}

SOLUTION

Either return a new representation every time or setAvailable to true before returning the representation



来源:https://stackoverflow.com/questions/37975772/restlet-getting-http-status-code-204-instead-of-200

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!