【发布时间】:2017-08-01 10:18:01
【问题描述】:
我有一个这样的 Java Jersey 端点:
public Response changeState(@HeaderParam(CLIENT_HEADER_PARAM) @NotNull Long clientId,
@FormParam("section") @NotNull String section,
@FormParam("state") @NotNull Boolean state) {
Map<String,String> result=new HashMap<>();
........
if(condition1){
result.put("key1","val1");
return Response.ok().entity(composeJson(unsuccessMap(result))).build();
}
if(condition2){
result.put("key2","val2");
return Response.ok().entity(composeJson(unsuccessMap(result))).build();
}
}
然后在jquery方面:
if(data.hasOwnProperty('key1') && data.key1=='val1'){
swal({
title: "Error",
text: "KEY1 ERROR",
type: "error",
allowEscapeKey: true
});
}
问题是我不喜欢这种方式。我需要记住魔法常数、字符串或数字,然后在那里检查,啊啊啊:)
如何以最优雅的方式做到这一点?
【问题讨论】: