【发布时间】:2015-08-18 09:27:38
【问题描述】:
我正在尝试反序列化这个 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;
}
但我遇到了这个奇怪的错误 org.codehaus.jackson.map.JsonMappingException:无法反序列化 java.lang.Long 的实例超出 START_ARRAY 令牌 [来源:不适用;行:-1,列:-1](通过引用链:com.efsm.data.dto.user.UserRequestDto["userProfileId"])
任何帮助将不胜感激
*********************编辑****************************** ************
错误很愚蠢,我没有使用正确的类来解析
CreateUserDto userRequestDto = JsonParserUtility.parseRequest(data, CreateUserDto.class);
我使用了 UserDo 而不是 CreateUserDto
【问题讨论】:
-
你如何调用你的
parseRequest方法,你可以展示一下吗? -
我编辑我的问题...