Error http code 500

- - Dernière réponse :  ndiayegawlo - 10 sept. 2019 à 22:15
Bonjour,

Mon problème est :
j'ai une fonction qui permet de se connecter à partir du login et du mot de passe
j'ai appelé la fonction dans mon controller
Si je lance l'application sur navigateur et que j'ai met le bon login et le bon mot de passe il me renvoie toujours null, alors que dans ma console il affiche très bien les informations
Afficher la suite 

1 réponse

Messages postés
18
Date d'inscription
lundi 2 avril 2018
Statut
Membre
Dernière intervention
10 septembre 2019
0
Merci
Merci de montrer ton code souce .
Dans mon controller
 
 @Path("getusereleve/{login}/{password}")
 @GET
 @Produces(MediaType.APPLICATION_JSON)
    public Usereleve getUserEleveByLoginAndPassword(@PathParam("login") String login, @PathParam("password") String password) {
       return iuser.ConnexionUsereleve(login, password);      
    }
Ici c'est l'implementation de la methode getUser
@Override
    public Usereleve ConnexionUsereleve(String login, String password) {
        try {
            return (Usereleve) em.createQuery("SELECT u FROM Usereleve u "
                    + " WHERE u.login = :login AND u.password = :password").
                    setParameter("login", login).setParameter("password", password).getSingleResult();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
Entite Eleve
package Beans;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
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.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@Table(name = "eleve")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Eleve.findAll", query = "SELECT e FROM Eleve e"),
    @NamedQuery(name = "Eleve.findById", query = "SELECT e FROM Eleve e WHERE e.id = :id"),
    @NamedQuery(name = "Eleve.findByMatricule", query = "SELECT e FROM Eleve e WHERE e.matricule = :matricule"),
    @NamedQuery(name = "Eleve.findByNom", query = "SELECT e FROM Eleve e WHERE e.nom = :nom"),
    @NamedQuery(name = "Eleve.findByPrenom", query = "SELECT e FROM Eleve e WHERE e.prenom = :prenom"),
    @NamedQuery(name = "Eleve.findByDatenaissance", query = "SELECT e FROM Eleve e WHERE e.datenaissance = :datenaissance"),
    @NamedQuery(name = "Eleve.findByLieunaissance", query = "SELECT e FROM Eleve e WHERE e.lieunaissance = :lieunaissance"),
    @NamedQuery(name = "Eleve.findBySexe", query = "SELECT e FROM Eleve e WHERE e.sexe = :sexe"),
    @NamedQuery(name = "Eleve.findByEtat", query = "SELECT e FROM Eleve e WHERE e.etat = :etat")})
public class Eleve implements Serializable {

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ideleve")
    private List<Usereleve> usereleveList;

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "matricule")
    private String matricule;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "nom")
    private String nom;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "prenom")
    private String prenom;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "datenaissance")
    private String datenaissance;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "lieunaissance")
    private String lieunaissance;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 2)
    @Column(name = "sexe")
    private String sexe;
    @Basic(optional = false)
    @NotNull
    @Column(name = "etat")
    private int etat;
    @JoinColumn(name = "iddepartement", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Departement iddepartement;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ideleve")
    private List<Tuteureleve> tuteureleveList;
    @OneToOne(mappedBy = "ideleve")
    private Utilisateur utilisateur;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "ideleve")
    private List<Inscription> inscriptionList;

    public Eleve() {
    }

    public Eleve(Integer id) {
        this.id = id;
    }

    public Eleve(Integer id, String matricule, String nom, String prenom, String datenaissance, String lieunaissance, String sexe, int etat) {
        this.id = id;
        this.matricule = matricule;
        this.nom = nom;
        this.prenom = prenom;
        this.datenaissance = datenaissance;
        this.lieunaissance = lieunaissance;
        this.sexe = sexe;
        this.etat = etat;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMatricule() {
        return matricule;
    }

    public void setMatricule(String matricule) {
        this.matricule = matricule;
    }

    public String getNom() {
        return nom;
    }

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

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getDatenaissance() {
        return datenaissance;
    }

    public void setDatenaissance(String datenaissance) {
        this.datenaissance = datenaissance;
    }

    public String getLieunaissance() {
        return lieunaissance;
    }

    public void setLieunaissance(String lieunaissance) {
        this.lieunaissance = lieunaissance;
    }

    public String getSexe() {
        return sexe;
    }

    public void setSexe(String sexe) {
        this.sexe = sexe;
    }

    public int getEtat() {
        return etat;
    }

    public void setEtat(int etat) {
        this.etat = etat;
    }

    public Departement getIddepartement() {
        return iddepartement;
    }

    public void setIddepartement(Departement iddepartement) {
        this.iddepartement = iddepartement;
    }

    @XmlTransient
    public List<Tuteureleve> getTuteureleveList() {
        return tuteureleveList;
    }

    public void setTuteureleveList(List<Tuteureleve> tuteureleveList) {
        this.tuteureleveList = tuteureleveList;
    }

    public Utilisateur getUtilisateur() {
        return utilisateur;
    }

    public void setUtilisateur(Utilisateur utilisateur) {
        this.utilisateur = utilisateur;
    }

    @XmlTransient
    public List<Inscription> getInscriptionList() {
        return inscriptionList;
    }

    public void setInscriptionList(List<Inscription> inscriptionList) {
        this.inscriptionList = inscriptionList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Eleve)) {
            return false;
        }
        Eleve other = (Eleve) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Beans.Eleve[ id=" + id + " ]";
    }

    @XmlTransient
    public List<Usereleve> getUsereleveList() {
        return usereleveList;
    }

    public void setUsereleveList(List<Usereleve> usereleveList) {
        this.usereleveList = usereleveList;
    }
    
}


Entite UserEleve
package Beans;

import java.io.Serializable;
import javax.persistence.Basic;
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.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;


@Entity
@Table(name = "usereleve")
//@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Usereleve.findAll", query = "SELECT u FROM Usereleve u"),
    @NamedQuery(name = "Usereleve.findById", query = "SELECT u FROM Usereleve u WHERE u.id = :id"),
    @NamedQuery(name = "Usereleve.findByLogin", query = "SELECT u FROM Usereleve u WHERE u.login = :login"),
    @NamedQuery(name = "Usereleve.findByPassword", query = "SELECT u FROM Usereleve u WHERE u.password = :password")})
public class Usereleve implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "login")
    private String login;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "password")
    private String password;
    @JoinColumn(name = "ideleve", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Eleve ideleve;

    public Usereleve() {
    }

    public Usereleve(Integer id) {
        this.id = id;
    }

    public Usereleve(Integer id, String login, String password) {
        this.id = id;
        this.login = login;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Eleve getIdeleve() {
        return ideleve;
    }

    public void setIdeleve(Eleve ideleve) {
        this.ideleve = ideleve;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Usereleve)) {
            return false;
        }
        Usereleve other = (Usereleve) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Beans.Usereleve[ id=" + id + " ]";
    }
    
}


Interface IUtilisateur
package Dao;

import Beans.Usereleve;
import Beans.Utilisateur;


public interface IUtilisateur {
    public Utilisateur ConnexionUserPersonnel(String login, String password);
    public Usereleve ConnexionUsereleve(String login, String password);
}


UtilisateurDao
package Dao;

import Beans.Usereleve;
import Beans.Utilisateur;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

<code java>
package ui;

import Beans.Usereleve;
import Beans.Utilisateur;
import Dao.IUtilisateur;
import javax.ejb.EJB;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;

/**
 * REST Web Service
 *
 * @author User
 */
@Path("generic")
public class GenericResource {

    @Context
    private UriInfo context;
    @EJB
    private IUtilisateur iuser;


    public GenericResource() {
    }


    @Path("getuser/{login}/{password}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Utilisateur getUserByLoginAndPassword(@PathParam("login") String login, @PathParam("password") String password) {
       return iuser.ConnexionUserPersonnel(login, password);
      
    }
    
    @Path("getusereleve/{login}/{password}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Usereleve getUserEleveByLoginAndPassword(@PathParam("login") String login, @PathParam("password") String password) {
       return iuser.ConnexionUsereleve(login, password);      
    }
    
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public String getXml() {
        //TODO return proper representation object
        throw new UnsupportedOperationException();
    }

    /**
     * PUT method for updating or creating an instance of GenericResource
     * @param content representation for the resource
     */
    @PUT
    @Consumes(MediaType.APPLICATION_XML)
    public void putXml(String content) {
    }
}

@Stateless

public class UtilisateurDao implements IUtilisateur{
@PersistenceContext
EntityManager em;

@Override
public Utilisateur ConnexionUserPersonnel(String login, String password) {
try {
return (Utilisateur) em.createQuery("SELECT u FROM Utilisateur u WHERE u.login = :login AND u.password =:password").
setParameter("login", login).setParameter("password", password).getSingleResult();

} catch (Exception e) {
e.printStackTrace();
return null;
}
}

@Override
public Usereleve ConnexionUsereleve(String login, String password) {
try {
return (Usereleve) em.createQuery("SELECT u FROM Usereleve u "
+ " WHERE u.login = :login AND u.password = :password").
setParameter("login", login).setParameter("password", password).getSingleResult();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

}
</code>

Controller
Commenter la réponse de lallana2re