Я создал собственный десериализатор для своих сущностей, но он продолжает выдавать исключение:
У меня есть два класса: AppUser и AppUserAvatar
AppUser.java
@Entity
@Table(name = "user")
public class AppUser implements Serializable {
@Transient
private static final long serialVersionUID = -3536455219051825651L;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Column(name = "password", nullable = false, length = 256)
private String password;
@JsonIgnore
@Column(name = "is_active", nullable = false)
private boolean active;
@JsonIgnore
@OneToMany(mappedBy = "appUser", targetEntity = AppUserAvatar.class, fetch = FetchType.LAZY)
private List<AppUserAvatar> appUserAvatars;
//// Getters and Setters and toString() ////
}
AppUserAvatar.java
@Entity
@Table(name = "user_avatar")
public class AppUserAvatar extends BaseEntityD implements Serializable {
@Transient
private static final long serialVersionUID = 8992425872747011681L;
@Column(name = "avatar", nullable = false)
@Digits(integer = 20, fraction = 0)
@NotEmpty
private Long avatar;
@JsonDeserialize(using = AppUserDeserializer.class)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private AppUser appUser;
//// Getters and Setters and toString() ////
}
AppUserDeserializer.java пакет com.nk.accountservice.deserializer;
import com.edoctar.accountservice.config.exception.InputNotFoundException;
import com.edoctar.accountservice.domain.candidate.AppUser;
import com.edoctar.accountservice.service.candidate.AppUserService;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.io.Serializable;
public class AppUserDeserializer extends JsonDeserializer implements Serializable {
private static final long serialVersionUID = -9012464195937554378L;
private AppUserService appUserService;
@Autowired
public void setAppUserService(AppUserService appUserService) {
this.appUserService = appUserService;
}
@Override
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
Long userId = node.asLong();
System.out.println(node);
System.out.println(node.asLong());
AppUser appUser = appUserService.findById(userId);
System.out.println("appuser: " + appUser);
if (appUser == null) try {
throw new InputNotFoundException("User not found!");
} catch (InputNotFoundException e) {
e.printStackTrace();
return null;
}
return appUser;
}
}
Пример xhr boy:
{
"appUser": 1,
"avatar": 1
}
Исключение выдается каждый раз, когда я отправляю запрос.
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.edoctar.accountservice.domain.candidate.AppUserAvatar["appUser"])]
Я обнаружил, что метод appUserService.findById () не вызывается.Я действительно смущен.Я не знаю, где я ошибся.Буду благодарен за любое решение.Спасибо.