我正在尝试反序列化此json:
{
"login": "tamtoum",
"lastName": "brahim",
"emailAddress": "[email protected]",
"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;行:-1,列:-1](通过参考链:com.efsm.data.dto.user.UserRequestDto [“ userProfileId”])
任何帮助,将不胜感激
*********************编辑**************************** *********
错误很愚蠢,我没有使用正确的类进行解析
CreateUserDto userRequestDto = JsonParserUtility.parseRequest(data,CreateUserDto.class);
我用UserDo代替CreateUserDto
最佳答案
错误很愚蠢,我没有使用正确的类进行解析
CreateUserDto userRequestDto = JsonParserUtility.parseRequest(data,CreateUserDto.class);
我用UserDo代替CreateUserDto