【发布时间】:2015-05-11 15:30:45
【问题描述】:
给定一个特定的输入参数,对于一个 rest api,我想使用哈希码作为 etag。 json响应发生变化而hashcode不变的概率是多少?
或者有没有更好的方法来做到这一点?
@GET
public Response getConfigurationForView(@PathParam("in1") String in1, @Context Request request) throws Exception {
String jsonResponse = getJsonResponse(in1);
EntityTag etag = new EntityTag(Integer.toString(in1.hashCode()) + "-" + Integer.toString(jsonResponse.hashCode()));
ResponseBuilder builder = request.evaluatePreconditions(etag);
if(builder == null){
builder = Response.ok(jsonResponse, MediaType.APPLICATION_JSON);
builder.tag(etag);
}
return builder.build();
}
【问题讨论】:
-
据我了解,引用的帖子是关于字符串的哈希码是否在不同版本的 java 中相同。我在问,对于两个不同的字符串,我会得到相同的哈希码的概率是多少。
-
注意:hashcode 可以是负数。有时字符串不应包含连字符。
标签: java jersey probability