IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Boot Java Discussion :

Spring boot jpa hibernate probleme recuperation element par son id


Sujet :

Spring Boot Java

  1. #1
    Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2012
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2012
    Messages : 92
    Points : 61
    Points
    61
    Par défaut Spring boot jpa hibernate probleme recuperation element par son id
    Bonjour,

    Je suis en train de développer une application spring boot jpa hibernate. J'ai créé un api pour récupérer la liste de mes clients après génération de mon entité client via hibernate. Je peux ajouter un client mais je n'arrive pas à récupérer un client via son id.

    Je tester mon contrôleur via advanced rest client et j'ai un erreur voir piece jointe


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
     
    package tata.entite;
     
    import java.io.Serializable;
     
    import javax.persistence.Entity;
    import javax.persistence.EntityListeners;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
     
    import org.springframework.data.jpa.domain.support.AuditingEntityListener;
     
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
     
    @Entity
    @EntityListeners(AuditingEntityListener.class)
    @JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, 
            allowGetters = true)
    public class Client implements Serializable {
     
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	private Integer id;
     
    	private String raisonSociale;
    	private String adresse;
    	private int siren;
    	private String mail;
     
    	public Client() {
    		super();
    	}
     
    	public Client(String raisonSociale, String adresse, int siren, String mail) {
    		super();
    		//this.id = id;
    		this.raisonSociale = raisonSociale;
    		this.adresse = adresse;
    		this.siren = siren;
    		this.mail = mail;
    	}
     
    	public Integer getId() {
    		return id;
    	}
     
    	public String getRaisonSociale() {
    		return raisonSociale;
    	}
     
    	public void setRaisonSociale(String raisonSociale) {
    		this.raisonSociale = raisonSociale;
    	}
     
    	public String getAdresse() {
    		return adresse;
    	}
     
    	public void setAdresse(String adresse) {
    		this.adresse = adresse;
    	}
     
    	public int getSiren() {
    		return siren;
    	}
     
    	public void setSiren(int siren) {
    		this.siren = siren;
    	}
     
    	public String getMail() {
    		return mail;
    	}
     
    	public void setMail(String mail) {
    		this.mail = mail;
    	}
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    package dev.repository;
     
    import org.springframework.data.jpa.repository.JpaRepository;
     
    import tata.entite.Client;
     
    public interface ClientRepository extends JpaRepository<Client, Integer> {
     
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
     
     
    package dev.controleur;
     
    import java.util.List;
     
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.PutMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
     
    import dev.entite.Client;
    import dev.metier.ClientMetier;
    import dev.repository.ClientRepository;
     
    @RestController
    @RequestMapping("/api")
    public class ClientControleur {
     
    	@Autowired 
    	private ClientMetier clientMetier;
     
    	@Autowired 
    	private ClientRepository clientRepository;
     
    	@GetMapping("/clients")
    	public List<Client> listeClients() {
    		return clientMetier.listClient();
    	}
     
    	@PostMapping("/clients")
    	public Client ajouterClient(@RequestBody Client client) {
    		return clientMetier.ajouterClient(client);	
    	}
     
    	@GetMapping("/clients/{id}")
    	public Client recupereClientById(Client c) {
    		return clientRepository.findById(c.getId()).get();
    	}
     
    	@PutMapping("/clients/{id}")
    	public Client modifierClient(@PathVariable(value = "id") Integer id,@RequestBody Client client) {
    		Client clientDetail = clientRepository.findById(id).get();
    		clientDetail.setAdresse(client.getAdresse());
    		clientDetail.setMail(client.getMail());
    		clientDetail.setRaisonSociale(client.getRaisonSociale());
    		clientDetail.setSiren(client.getSiren());
    		return clientDetail;
    		//return clientMetier.modifierClient(client);	
    	}
     
     
     
    }
    localhost:8080/api/clients/1 , j'ai une erreur 500 , internal server via advanced client rest

  2. #2
    Membre régulier
    Homme Profil pro
    Développeur Java
    Inscrit en
    Octobre 2012
    Messages
    100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Congo-Kinshasa

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2012
    Messages : 100
    Points : 91
    Points
    91
    Par défaut
    Bonjour,

    Client clientDetail = clientRepository.getOne(id);

  3. #3
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 937
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 937
    Points : 4 358
    Points
    4 358
    Par défaut
    Citation Envoyé par lnquaidorsay Voir le message
    Bonjour,


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
     
        @GetMapping("/clients/{id}")
        public Client recupereClientById(Client c) {
            return clientRepository.findById(c.getId()).get();
        }
     
     }
    Relisez attentivement ce mapping…

  4. #4
    Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2012
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2012
    Messages : 92
    Points : 61
    Points
    61
    Par défaut
    Merci Wangi et Jeit Emgie,mais mon soucis est que quand je teste avec

    http://localhost:8080/clients : Ajout d'un client via un json dans le body e mon adanced client rest
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    {
    "raisonSociale":"client 1"
    "adresse":"38000 grenoble",
    "siren":124578,
    "email":"client1@client1.fr"
    }
    ça marche bien, il m'enregistre bien dans la table Client avec l'id 1

    une fois l'ajout d'un second client
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    {
    "raisonSociale":"client 2"
    "adresse":"06000 nice",
    "siren":1296358,
    "email":"client2@client2.fr"
    }
    il marche aussi

    et quand je vais recuperer ce même client via l'outil advanced rest client en faisant

    http://localhost:8080/api/clients/2, ça me donne error 500, internal server la resource ne dit pas être null en base de données et pourtant il est bien là.

    Du coup , je ne sais pas comment tester ce cas là.

    Et comment faire update puisque pour faire un update de mon client , je dois le recuperer via son id.

    pouvez vous me montrer via un capture avec advanced cleint rest

  5. #5
    Membre émérite
    Homme Profil pro
    Ingénieur en génie logiciel
    Inscrit en
    Juin 2012
    Messages
    860
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Ingénieur en génie logiciel
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Juin 2012
    Messages : 860
    Points : 2 449
    Points
    2 449
    Par défaut
    JeitEmgie a raison...

    tu as un problème dans ton mapping

    pour
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    @GetMapping("/clients/{id}")
    public Client recupereClientById(Client c) {
        return clientRepository.findById(c.getId()).get();
    }
    essaye plutôt

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    @GetMapping("/clients/{id}")
    public Client recupereClientById(@PathVariable(value = "id") Integer id) {
        return clientRepository.findById(id);
    }

  6. #6
    Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2012
    Messages
    92
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2012
    Messages : 92
    Points : 61
    Points
    61
    Par défaut
    Bonjour à tous et merci.

    Finalement j'ai pu trouver mon bonheur en faisant comme ceci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    @GetMapping("/client/{id}")
    	public Client recupereClientParSonId(@PathVariable("id")Integer id) {
    		return clientMetier.recupererClientParSonId(id);
    	}
    Et dans la classe métier
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    @Autowired
    	private ClientRepository clientRepository;
    public Client recupererClientParSonId(Integer id) {
    		return clientRepository.getOne(id);
    	}

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Spring Boot : Configurer Hibernate Session
    Par paladice dans le forum Spring Boot
    Réponses: 1
    Dernier message: 26/05/2016, 09h27
  2. Réponses: 0
    Dernier message: 26/03/2015, 19h11
  3. [Security] Spring Security + JPA +Hibernate
    Par gabz57 dans le forum Spring
    Réponses: 0
    Dernier message: 01/05/2011, 02h21
  4. Réponses: 2
    Dernier message: 29/11/2007, 15h25

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo