问题
控制器处理
@ApiOperation(value="短信验证")
@RequestMapping(value = "vailSms", method = RequestMethod.POST)
public UserSmsTokenVM vailAutoLogin(@Valid @RequestBody VailSmsParm parm)
{
// 业务逻辑...
}
请求参数对象
@Data
public class VailSmsParm {
@ApiModelProperty(value = "国家地区代码",required = true)
@NotBlank(message = "国家地区代码不能为空")
@Pattern(regexp = "^[1-9]\\d*$",message = "国家地区代码格式不正确")
@JSONField(format="trim")
private String areaCode;
@ApiModelProperty(value = "手机号码",required = true)
@NotBlank(message = "手机号码不能为空")
@Pattern(regexp = "^[1-9]\\d*$", message = "手机号码格式不正确")
@JSONField(format="trim")
private String phone;
@ApiModelProperty(value = "验证码",required = true)
@NotNull(message = "验证码不能为空")
@Pattern(regexp = "^[1-9]\\d*$",message = "验证码格式不正确")
@JSONField(format="trim")
private String code;
@ApiModelProperty(value = "操作类型,默认为自动登录/注册")
private RequestSmsType type = RequestSmsType.AutoLogin;
}
枚举信息
@Getter
public enum RequestSmsType implements IntegerEnum {
AutoLogin(99,"自动登录/注册"),
RecoverPwd(1,"密码找回");
private Integer code;
private String desc;
RequestSmsType(Integer code, String desc)
{
this.code = code;
this.desc = desc;
}
}
IntegerEnum 关系
public interface IntegerEnum extends IEnum<Integer> {
}
public interface IEnum<T> extends com.alibaba.fastjson.serializer.JSONSerializable{
T getCode();
String getDesc();
default void write(JSONSerializer jsonSerializer, Object o, Type t, int i) {
jsonSerializer.write(this.getCode());
}
}
测试场景
使用String to Enum是没问题的,但使用Integer to Enum时候出错,
但如果想处理Integer to Enum类型有什么方法或扩展点?
请求参数
{
"areaCode": "86",
"phone": "13800138000",
"type": 99
}
错误信息
JSON parse error: parse enum com.demo.webapi2.model.enums.RequestSmsType error, value : 99; nested exception is com.alibaba.fastjson.JSONException: parse enum com.demo.webapi2.model.enums.RequestSmsType error, value : 99
4条答案
按热度按时间qq24tv8q1#
嗯,我现在也出现了这个问题,问一下你是怎么解决的啊?
xkftehaa2#
我换了 Gson ,建议换成GSON吧。如果你做手机端和服务端公共的JSON处理裤的时候就会发现,Gson才是最爽的···
6ju8rftf3#
好的,感谢,不过我也已经从fastjson换成了jackson了。技术太菜,搞不定。 在 2019-12-19 23:47:18,"xcli" notifications@github.com 写道: 我换了Gson ,建议换成GSON吧。如果你做手机端和服务端公共的JSON处理裤的时候就会发现,Gson才是最爽的··· — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.
o2gm4chl4#
好的~哈!