Im having issues with spring and jackson when i try to serialize them I have two classes
@Entity
@Table(name = "conditions")
@Data
@JsonInclude(Include.NON_EMPTY)
@NoArgsConstructor
public class Condition implements Serializable {
private static final long serialVersionUID = -2917417625227297889L;
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
@Column(name="nId")
@JsonView(View.Public.class)
private Integer id;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "conditions")
@JsonView(View.Public.class)
private Set<Rule> rules = new HashSet<Rule>(0);
public Condition(int conditionId) {
this.id = conditionId;
}
}
and
@Entity
@Table(name = "tblComplianceRules")
@Data
@NoArgsConstructor
public class Rule implements Serializable {
private static final long serialVersionUID = -4443637570696913241L;
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
@Column(nullable = false)
@JsonView(View.Public.class)
private Integer id;
@Column
@JsonView(View.Public.class)
@NotEmpty
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "tblComplianceRulesConditions", catalog = "swat", joinColumns = {
@JoinColumn(name = "ruleId", nullable = false, updatable = false) }, inverseJoinColumns = {
@JoinColumn(name = "conditionId", nullable = false, updatable = false) })
@JsonView(View.Public.class)
private Set<Condition> conditions = new HashSet<Condition>();
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "rules")
@JsonBackReference
@JsonView(View.Internal.class)
private Set<CompliancePolicy> policies = new HashSet<CompliancePolicy>(0);
public Rule(Integer id) {
this.id = id;
}
}
Also ill post the View class
public class View {
public static class Public {
}
public static class Internal extends Public {
}
}
what im trying to achieve is that the response im sending to the client will not send unnecessary data, but im keep getting the error in my title...
i was able to resolve this by not using the @JsonView and using the @JsonBackReference but its not what i want...
As requested im attaching the response
@GetMapping(value = "/api/condition")
@JsonView(View.Internal.class)
public ResponseEntity<?> getConditions(Principal user) {
if (user != null) {
log.debug("Getting conditions for user: " + user.getName());
} else {
log.error("Warning user is not defined");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
try {
List<Condition> conditions = conditionService.getConditionsByOrderById();
return ResponseEntity.ok().body(conditions);
} catch (Exception ex) {
log.error("Could not fetch condition from tblComplianceConditions: " + ExceptionUtils.getRootCauseMessage(ex), ex);
return ResponseEntity.badRequest().body(ExceptionUtils.getRootCauseMessage(ex));
}
}