我正在开发一个小型Web应用程序。
那是我的问题:
当用户尝试在我的数据表中添加新记录时执行此操作
@RequestMapping(value="/saveJokerRule", method= RequestMethod.POST, consumes= "application/json", produces = "application/json")
@ResponseBody
public String saveJokerRule(@RequestBody String json) {
System.out.println("JSON EDITOR:" + json.toString());
EditorResponse editorResponse = new EditorResponse();
JokerRuleForm jokerRuleForm = new GsonBuilder().serializeNulls().create().fromJson(json, JokerRuleForm.class);
...
}
这是服务器接收并通过系统调用打印的有效json:
{"action":"create","data":{"0":{"jokerRule":{"group":1,"campaignPhase":"","dailyLimit":"","weeklyLimit":"","monthlyLimit":"","validFrom":"","activity":1}}}}
那是JokerRuleForm类
public class JokerRuleForm {
@Expose
String action;
@Expose
@SerializedName("data")
Map<String, JokerRuleView> data;
...
}
那是JokerRuleView类
public class JokerRuleView {
String idJokerRule;
private AgentGroup group;
private JokerRule jokerRule;
private Activity activity;
public class JokerRule{
private String campaignPhase;
private Integer dailyLimit;
private Integer weeklyLimit;
private Integer monthlyLimit;
private Date validFrom;
private Date dateUpdate;
private String group;
private String activity;
/**
* @return the campaignPhase
*/
public String getCampaignPhase() {
return campaignPhase;
}
/**
* @param campaignPhase the campaignPhase to set
*/
public void setCampaignPhase(String campaignPhase) {
this.campaignPhase = campaignPhase;
}
/**
* @return the dailyLimit
*/
public Integer getDailyLimit() {
return dailyLimit;
}
/**
* @param dailyLimit the dailyLimit to set
*/
public void setDailyLimit(Integer dailyLimit) {
this.dailyLimit = dailyLimit;
}
/**
* @return the weeklyLimit
*/
public Integer getWeeklyLimit() {
return weeklyLimit;
}
/**
* @param weeklyLimit the weeklyLimit to set
*/
public void setWeeklyLimit(Integer weeklyLimit) {
this.weeklyLimit = weeklyLimit;
}
/**
* @return the monthlyLimit
*/
public Integer getMonthlyLimit() {
return monthlyLimit;
}
/**
* @param monthlyLimit the monthlyLimit to set
*/
public void setMonthlyLimit(Integer monthlyLimit) {
this.monthlyLimit = monthlyLimit;
}
/**
* @return the validFrom
*/
public Date getValidFrom() {
return validFrom;
}
/**
* @param validFrom the validFrom to set
*/
public void setValidFrom(Date validFrom) {
this.validFrom = validFrom;
}
/**
* @return the dateUpdate
*/
public Date getDateUpdate() {
return dateUpdate;
}
/**
* @param dateUpdate the dateUpdate to set
*/
public void setDateUpdate(Date dateUpdate) {
this.dateUpdate = dateUpdate;
}
/**
* @return the group
*/
public String getGroup() {
return group;
}
/**
* @param group the group to set
*/
public void setGroup(String group) {
this.group = group;
}
/**
* @return the activity
*/
public String getActivity() {
return activity;
}
/**
* @param activity the activity to set
*/
public void setActivity(String activity) {
this.activity = activity;
}
}
public class Activity {
String idActivity;
String name;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the idGroup
*/
public String getIdActivity() {
return idActivity;
}
/**
* @param idGroup the idGroup to set
*/
public void setIdActivity(String idActivity) {
this.idActivity = idActivity;
}
}
public class AgentGroup {
String idGroup;
String name;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the idGroup
*/
public String getIdGroup() {
return idGroup;
}
/**
* @param idGroup the idGroup to set
*/
public void setIdGroup(String idGroup) {
this.idGroup = idGroup;
}
}
/**
* @return the idJokerRule
*/
public String getIdJokerRule() {
return idJokerRule;
}
/**
* @param idJokerRule the idJokerRule to set
*/
public void setIdJokerRule(String idJokerRule) {
this.idJokerRule = idJokerRule;
}
/**
* @return the agentGroup
*/
public AgentGroup getGroup() {
return group;
}
/**
* @param agentGroup the agentGroup to set
*/
public void setGroup(AgentGroup group) {
this.group = group;
}
/**
* @return the jokerRule
*/
public JokerRule getJokerRule() {
return jokerRule;
}
/**
* @param jokerRule the jokerRule to set
*/
public void setJokerRule(JokerRule jokerRule) {
this.jokerRule = jokerRule;
}
/**
* @return the activity
*/
public Activity getActivity() {
return activity;
}
/**
* @param activity the activity to set
*/
public void setActivity(Activity activity) {
this.activity = activity;
}
}
JokerRuleForm jokerRuleForm = new GsonBuilder().serializeNulls().create().fromJson(json, JokerRuleForm.class);
执行此行时,我得到了一个NumberFormatException空字符串,并且我认为它是dailyLimit或date的错误,因为它为空,并且fromJson()方法无法完成他需要做的事情。
我已经阅读了有关TypeAdapter的内容,该内容似乎适合我的情况,但是我真的不知道如何进行,并且不确定是否有效。
有人可以帮我吗?
答案 0 :(得分:0)
限制字段,例如dailyLimit
是JSON中的空字符串。在JsonParseException when encountering an empty value for an int or long #472问题中提出了此建议,但Gson团队已将其关闭,因为将""
解析为int
毫无意义。
其中一位用户提供了a solution in his comment,可轻松解析数字值。我不会自己走这条路线,而是要修复JSON,但这取决于您:
public static final TypeAdapter<Number> UNRELIABLE_INTEGER = new TypeAdapter<Number>() {
@Override
public Number read(JsonReader in) throws IOException {
JsonToken jsonToken = in.peek();
switch (jsonToken) {
case NUMBER:
case STRING:
String s = in.nextString();
try {
return Integer.parseInt(s);
} catch (NumberFormatException ignored) {
}
try {
return (int)Double.parseDouble(s);
} catch (NumberFormatException ignored) {
}
return null;
case NULL:
in.nextNull();
return null;
case BOOLEAN:
in.nextBoolean();
return null;
default:
throw new JsonSyntaxException("Expecting number, got: " + jsonToken);
}
}
@Override
public void write(JsonWriter out, Number value) throws IOException {
out.value(value);
}
};
public static final TypeAdapterFactory UNRELIABLE_INTEGER_FACTORY = TypeAdapters.newFactory(int.class, Integer.class, UNRELIABLE_INTEGER);
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(UNRELIABLE_INTEGER_FACTORY)
.create();