Home > Blockchain >  field with @Transient annotation doesn't appear in returned JSON
field with @Transient annotation doesn't appear in returned JSON

Time:03-17

I need to get a custom json containing the fields of the class Utilisateur plus a fied centre_rc) of another class Operateur I tried to implement this using @Transient with centre_rc in Utilisateur.java but when i get my json this field centre_rc is missing.

here is Utilisateur class

@Entity
@Table(name = "utilisateur")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Utilisateur implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    @Column(name = "id")
    private Long id;

    @Column(name = "nom_utilisateur")
    private String nomUtilisateur;

    @Column(name = "prenom")
    private String prenom;

    @Column(name = "nom")
    private String nom;

    @Column(name = "date_inscription")
    private LocalDate dateInscription;

    @Column(name = "password")
    private String password;
 
    @Transient
    @JsonSerialize
    //@JsonView
    @JsonDeserialize
    @JsonInclude()
    private String centre_rc;

    @ManyToOne
    @JsonIgnoreProperties(value = { "utilisateurs" }, allowSetters = true)
    private Operateur operateur;
//getters & setters
}

here is Operateur class

@Entity
@Table(name = "operateur")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Operateur implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    @Column(name = "id")
    private Long id;

    @Column(name = "centre_rc")
    private String centreRc;

    @Column(name = "numero_rc")
    private String numeroRc;

    @OneToMany(mappedBy = "operateur")
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @JsonIgnoreProperties(value = { "operateur" }, allowSetters = true)
    private Set<Utilisateur> utilisateurs = new HashSet<>();
    //getters & setters
}

In UtilisateurRepository.java here is my SQL query:

@Query(value = "select u.*, o.centre_rc from utilisateur u inner join operateur o on o.id=u.operateur_id", nativeQuery = true)
    Page<Utilisateur> findAllOperateurs(Pageable pageable);

In UtilisateurResource.java

@GetMapping("/utilisateursOperateurs")
    public ResponseEntity<List<Utilisateur>> getAllUtilisateursCalledOperateurs(
        @org.springdoc.api.annotations.ParameterObject Pageable pageable
    ) {
        log.debug("REST request to get a page of Utilisateurs");
        Page<Utilisateur> page = utilisateurRepository.findAllOperateurs(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

the result of this query return something like this:

[
  {
    "id": 3,
    "nomUtilisateur": "e-business Granite drive",
    "prenom": "context-sensitive strategic calculate",
    "nom": "maximized Oberkampf withdrawal",
    "dateInscription": "2022-03-11",
    "password": "viral",
    "operateur": {
      "id": 8,
      "centreRc": "parsing primary invoice",
      "numeroRc": "Harpe"
    }
  }
]

CodePudding user response:

Jackson might not serialize the field because the following field is null. Try removing @JsonInclude() annotation from the field like this

@Transient
@JsonSerialize
@JsonDeserialize
private String centre_rc;

And add @JsonInclude annotation at the class level

@Entity
@Table(name = "utilisateur")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Utilisateur implements Serializable {
    // your code goes here
}

CodePudding user response:

@Transient
@JsonProperty
private String centre_rc;

You can try to use this annotation @JsonProperty, I use this in my project and it works.

  • Related