From 5a66d00926b25f1bad425f84a2ad621a4106d841 Mon Sep 17 00:00:00 2001 From: Massimiliano Cristarella Date: Fri, 15 Jul 2022 00:27:41 +0200 Subject: [PATCH] Fix recursive object serializing and lazy fields access --- .../com/mcris/triprecorder/models/entities/Geopoint.java | 2 ++ .../java/com/mcris/triprecorder/models/entities/Trip.java | 2 ++ .../java/com/mcris/triprecorder/models/entities/User.java | 5 +++++ .../java/com/mcris/triprecorder/resources/UsersResource.java | 2 +- 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mcris/triprecorder/models/entities/Geopoint.java b/src/main/java/com/mcris/triprecorder/models/entities/Geopoint.java index fdc652b..faf88f3 100644 --- a/src/main/java/com/mcris/triprecorder/models/entities/Geopoint.java +++ b/src/main/java/com/mcris/triprecorder/models/entities/Geopoint.java @@ -1,5 +1,6 @@ package com.mcris.triprecorder.models.entities; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import java.sql.Timestamp; @@ -28,6 +29,7 @@ public class Geopoint { private String label; @ManyToOne @JoinColumn(name = "trip_id", referencedColumnName = "id", nullable = false) + @JsonIgnore private Trip trip; public int getId() { diff --git a/src/main/java/com/mcris/triprecorder/models/entities/Trip.java b/src/main/java/com/mcris/triprecorder/models/entities/Trip.java index 6a8963f..ef180a5 100644 --- a/src/main/java/com/mcris/triprecorder/models/entities/Trip.java +++ b/src/main/java/com/mcris/triprecorder/models/entities/Trip.java @@ -1,5 +1,6 @@ package com.mcris.triprecorder.models.entities; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import java.sql.Timestamp; @@ -29,6 +30,7 @@ public class Trip { private String notes; @OneToMany(mappedBy = "trip") private Collection geopoints; + @JsonIgnore @ManyToOne @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false) private User user; diff --git a/src/main/java/com/mcris/triprecorder/models/entities/User.java b/src/main/java/com/mcris/triprecorder/models/entities/User.java index b8f8cb5..f45005e 100644 --- a/src/main/java/com/mcris/triprecorder/models/entities/User.java +++ b/src/main/java/com/mcris/triprecorder/models/entities/User.java @@ -1,11 +1,14 @@ package com.mcris.triprecorder.models.entities; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; import jakarta.persistence.*; import java.security.Principal; import java.sql.Timestamp; import java.util.Collection; +@JsonInclude(JsonInclude.Include.NON_NULL) @Entity @Table(name = "users", schema = "trip_recorder") @NamedQuery(name = "User.byId", query = "select u from User u where u.id = :userId") @@ -17,12 +20,14 @@ public class User implements Principal { @Basic @Column(name = "username") private String username; + @JsonIgnore @Basic @Column(name = "password") private String password; @Basic @Column(name = "email") private String email; + @JsonIgnore @OneToMany(mappedBy = "user", fetch = FetchType.LAZY) private Collection sessions; @OneToMany(mappedBy = "user", fetch = FetchType.LAZY) diff --git a/src/main/java/com/mcris/triprecorder/resources/UsersResource.java b/src/main/java/com/mcris/triprecorder/resources/UsersResource.java index 19246fe..0885201 100644 --- a/src/main/java/com/mcris/triprecorder/resources/UsersResource.java +++ b/src/main/java/com/mcris/triprecorder/resources/UsersResource.java @@ -17,7 +17,7 @@ public class UsersResource { @Produces(MediaType.APPLICATION_JSON) public User getLoggedUser(@Context ContainerRequest containerRequest){ User user = (User) containerRequest.getSecurityContext().getUserPrincipal(); - //TODO: PERCHé NON FAI IL JSON!?!?!?! + user.setTrips(null); return user; } }