我正在尝试反序列化这个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:无法从START_ARRAY令牌中反序列化java.lang.Long的实例 在[资料来源:N / A; line:-1,column:-1](通过引用链:com.efsm.data.dto.user.UserRequestDto [“userProfileId”])
任何帮助将不胜感激
********************* EDIT ************************* ************
错误是愚蠢的我不会使用正确的类来解析
CreateUserDto userRequestDto = JsonParserUtility.parseRequest(data,CreateUserDto.class);
我使用UserDo而不是CreateUserDto
答案 0 :(得分:0)
错误是愚蠢的我不会使用正确的类来解析
CreateUserDto userRequestDto = JsonParserUtility.parseRequest(data,CreateUserDto.class);
我使用UserDo而不是CreateUserDto