我试图反序列化这个json:
{
"login": "tamtoum",
"lastName": "brahim",
"emailAddress": "brahim@aqqss.com",
"firstName": "sldmldm",
"userProfileId": [
"profil1",
"profilmobile"
],
"organizationId":[
"oui",
"non"
]
}
java类:
@JsonTypeInfo(use = Id.NONE, include = As.WRAPPER_OBJECT)
public class CreateUserDto {
private String login;
private String firstName;
private String lastName;
private String emailAddress;
private List<String> userProfileId;
private List<String> organizationId;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public List<String> getUserProfileId() {
return userProfileId;
}
public void setUserProfileId(List<String> userProfileId) {
this.userProfileId = userProfileId;
}
public List<String> getOrganizationId() {
return organizationId;
}
public void setOrganizationId(List<String> organizationId) {
this.organizationId = organizationId;
}
}
解析器: public static <T> T parseRequest(String jsonRequest, Class<T> returnType) {
final ObjectMapper mapper = new ObjectMapper();
T requestJsonParsed = null;
JsonNode jsonNode;
boolean hasError = false;
try {
jsonNode = mapper.readValue(jsonRequest, JsonNode.class);
requestJsonParsed = mapper.readValue(jsonNode, returnType);
} catch (JsonParseException e) {
logger.error(REQ_ERR, e);
hasError = true;
} catch (JsonMappingException e) {
logger.error(REQ_ERR, e);
hasError = true;
} catch (IOException e) {
logger.error(REQ_ERR, e);
hasError = true;
} finally {
if (hasError) {
throw new JsonParsingException(REQ_ERR);
}
}
return requestJsonParsed;
}
我在这个函数中调用解析器:
@RequestMapping(value = "/mobile/rest/create/user", method = RequestMethod.POST)
@ResponseBody
public CRUDResultDto createUser(@RequestBody String data) {
CRUDResultDto crudResultDto = new CRUDResultDto();
try {
crudResultDto.setSuccess(false);
CreateUserDto userRequestDto = JsonParserUtility.parseRequest(data, CreateUserDto.class);
if (userRequestDto != null) {
if (retrieveUserSA.findByLogin(userRequestDto.getLogin()) != null) {
UserDto userDto = userDtoFactory.getInstance(userRequestDto);
userDto.setPassword(passwordEncoder.encodePassword(userDto.getPassword(), null));
addUserSA.insert(userDto);
crudResultDto.setSuccess(true);
}
}
} catch (JsonParsingException e) {
throw new FunctionalException(CommonError.BAD_JSON_REQUEST, null);
} catch (NullPointerException e) {
throw new TechnicalException();
}
return crudResultDto;
}
但是我得到了这个奇怪的错误无法反序列化java.lang.Long out of START_ARRAY令牌的实例来源:N/A;line: -1, column: -1](通过引用链:com.efsm.data.dto.user.UserRequestDto["userProfileId"])
任何帮助都将不胜感激
********************* 编辑 *************************************
错误是愚蠢的,我没有使用正确的类来解析
CreateUserDto userRequestDto = JsonParserUtility。parseRequest(数据、CreateUserDto.class);
我用UserDo代替CreateUserDto
错误是愚蠢的,我没有使用正确的类来解析
CreateUserDto userRequestDto = JsonParserUtility。parseRequest(数据、CreateUserDto.class);
我用UserDo代替CreateUserDto