[英]Restlet Getting HTTP Status code 204 instead of 200
For the 1st request, I get the JSON response. 对于第一个请求,我得到了JSON响应。 From the next request onwards I start getting this log and HTTP Status Code 204, even though the ServerResource is successfully returning a representation 从下一个请求开始,即使ServerResource成功返回了表示形式,我也开始获取此日志和HTTP状态代码204
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 服务器资源,用于处理并返回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;
}
}
After releasing the response, the representation state available
is set to false. 释放响应后, available
的表示状态将设置为false。 So subsequent calls to the ServerResource
, returns the Representation but in handle()
method it is set to 204 since getResponseEntity().isAvailable()
returns false
. 因此,随后对ServerResource
调用返回了Representation,但在handle()
方法中将其设置为204,因为getResponseEntity().isAvailable()
返回false
。
From ServerResource
: 从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 每次返回一个新的表示形式,或者在返回该表示形式之前将setAvailable设置为true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.