我正在使用Enum中的静态工厂播放@JsonCreator,但是当请求有效负载中存在“Sex”:“Male”的Json对象时,CXF不会检测并调用updateUser方法。当我取出枚举的字符串表示时,它可以工作。
如何构造这三个类/枚举以获得从String到Enum的反序列化的任何想法。谢谢!
UserResource:
@PUT
@Produces("application/json")
@Consumes("application/json")
@Path("/{user-id}")
public Response updateUser(UserBean user, @PathParam("user-id") Long userId) {
user.setId(userId);
UserBean registeredUser = userService.updateUser(user);
return Response.ok().entity(registeredUser).build();
}
和UserBean:
public class UserBean {
private long id;
private String firstName;
private String lastName;
private String email;
private String phone;
private String password;
private Sex sex;
.....
和Enum:
public enum Sex {
FEMALE("Female", 0),
MALE("Male", 1);
private String desc;
private int value;
Sex(String desc, int value) {
this.desc = desc;
this.value = value;
}
public String desc() {
return desc;
}
@JsonValue
public int value() {
return value;
}
@JsonCreator
public static Sex fromString(@JsonProperty("sex") String desc) {
for (Sex type : Sex.values()) {
if (type.equals(desc)) {
return type;
}
}
return null;
}
}