Restlet 获取 HTTP 状态代码 204 而不是 200

Restlet Getting HTTP Status code 204 instead of 200

对于第一个请求,我得到了 JSON 响应。从下一个请求开始,我开始获取此日志和 HTTP 状态代码 204,即使 ServerResource 已成功返回表示

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

申请class布线路线

@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);
}

处理和returns一个JSON表示的服务器资源

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;
    }
}

释放响应后,表示状态available设置为false。因此随后调用 ServerResource、returns 表示,但在 handle() 方法中它被设置为 204,因为 getResponseEntity().isAvailable() returns false

来自 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;
}

解决方案

每次 return 一个新的表示或在 return 表示之前将 Available 设置为 true