简体   繁体   中英

Duplicated Result Rest Service Spring boot

hey everyone i have a spring boot rest api that have duplicated result for every key and value like the code below

this my object

{
id: 2,
Nom: "ee",
Prenom: "az",
Profil: "RC",
Pseudo: "aze",
Password: null,
role: null,
password: null,
nom: "ee",
prenom: "az",
profil: "RC",
pseudo: "aze"
},
{
id: 3,
Nom: "xx",
Prenom: "xxx",
Profil: "dataa",
Pseudo: "data",
Password: null,
role: null,
password: null,
nom: "xx",
prenom: "xxx",
profil: "dataa",
pseudo: "data"
},
{

as you can see every column is duplicated one with uppercase First letter other all in lowercase

this is my class :

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;




@Entity
@Table(name="\"UTILISATEUR\"")
public class Utilisateur   {


@Id
@GeneratedValue(strategy=GenerationType.AUTO) 
@Column(name="\"IdUtilisateur\"")
public Long id ;
@Column(name="\"Nom\"")
public String Nom ; 
@Column(name="\"Prenom\"")
public String Prenom ; 
@Column(name="\"Profil\"")
public String Profil ; 
@Column(name="\"Pseudo\"")
public String Pseudo ; 
@Column(name="\"Password\"")
public String Password ;
@ManyToOne
@JoinColumn(name="\"id_role\"")
public Role role ;
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getNom() {
    return Nom;
}
public void setNom(String nom) {
    Nom = nom;
}
public String getPrenom() {
    return Prenom;
}
public void setPrenom(String prenom) {
    Prenom = prenom;
}
public String getProfil() {
    return Profil;
}
public void setProfil(String profil) {
    Profil = profil;
}
public String getPseudo() {
    return Pseudo;
}
public void setPseudo(String pseudo) {
    Pseudo = pseudo;
}


public String getPassword() {
    return Password;
}
public void setPassword(String password) {
    Password = password;
}
public Role getRole() {
    return role;
}
public void setRole(Role role) {
    this.role = role;
}
public Utilisateur(String nom, String prenom, String profil, String pseudo, String password,
        Role role) {
    super();
    Nom = nom;
    Prenom = prenom;
    Profil = profil;
    Pseudo = pseudo;

    Password = password;
    this.role = role;
}
public Utilisateur() {
    super();
}

am using postgres from my database and this is my code

CREATE TABLE "UTILISATEUR"
(
"IdUtilisateur" serial NOT NULL,
"Nom" character varying(50),
"Prenom" character varying(50),
"Profil" character varying(50),
"Pseudo" character varying(20),
"IdSite" integer DEFAULT 0,
"Password" character varying(1024),
id_role integer,
 )

and finaly this is my application.propreties

spring.datasource.url = jdbc:postgresql://localhost/baseecu
spring.datasource.username = postgres
spring.datasource.password =root


spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.database = MYSQL 
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto= update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.EJB3NamingStrategy

i thought maybe it's a jdbc issue because am using postgres 9.2 and jdbc is for 9.1 i had an issue with the dialect before JPA Uppercase table names after that issue i tried to make it work and now i got this

any help or guide will be appreciated

Make the following changes so as to make the Entity class to follow the POJO class rules: 1. Make variable which are public to private, like change public String Nom ; to private String Nom; 1. Change methods implementations like below. Change

public void setNom(String nom) {
Nom = nom;}

to

public void setNom(String nom) {
this.Nom = nom;}

Replace the field declaration part in your entity class with the code below:

...
@Id
@GeneratedValue(strategy=GenerationType.AUTO) 
@Column(name="\"IdUtilisateur\"")
private Long id;
@Column(name="\"Nom\"")
private String Nom; 
@Column(name="\"Prenom\"")
private String Prenom; 
@Column(name="\"Profil\"")
private String Profil; 
@Column(name="\"Pseudo\"")
private String Pseudo; 
@Column(name="\"Password\"")
private String Password;
@ManyToOne
@JoinColumn(name="\"id_role\"")
private Role role;
...

I suggest you to follow proper java bean standard naming conventions for your variables and methods.

Keep member variables private.

JSON serializer works on publicly exposed fields and getter methods. Since you have named your member variables starting with Capital letter and also there is getter for the same which follows standard java naming convention. Hence the Json serializer detects them as two different properties to be serialized.

To avoid these kind of mistakes, you can also use the lombok dependency in pom.xml as follows:

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.14</version>
    </dependency>

And your entity class will be as follows:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="\"UTILISATEUR\"")
public class Utilisateur   {


    @Id
    @GeneratedValue(strategy=GenerationType.AUTO) 
    @Column(name="\"IdUtilisateur\"")
    public Long id ;
    @Column(name="\"Nom\"")
    public String Nom ; 
    @Column(name="\"Prenom\"")
    public String Prenom ; 
    @Column(name="\"Profil\"")
    public String Profil ; 
    @Column(name="\"Pseudo\"")
    public String Pseudo ; 
    @Column(name="\"Password\"")
    public String Password ;
    @ManyToOne
    @JoinColumn(name="\"id_role\"")
    public Role role ;
}

So here,

  1. @Data will create all the getters and setters.

  2. @NoArgsConstructor will create a no args constructor.

  3. @AllArgsConstructor will create an all args constructor.

Thus, your code will more cleaner and less error prone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM