How can I pass two parameters @Path and @PathParam

I am trying to create an authentication method using java angular js and mysql. I know how to pass one parameter, but I could not pass two, can you help me.

Here is my DAO method:

public Client authentifier(int numerocompte,String mdp) {

            System.out.println(numerocompte + mdp);

            try {
            Connection con = Connexion.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from client WHERE numerocompte = ? AND mdp = ?");
            ps.setInt(1, numerocompte);
            ps.setString(2, mdp);
            Client e = new Client();
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {

                e.setIdclient(rs.getInt("idclient"));
                e.setNomcomplet(rs.getString("nomcomplet"));
                e.setMail(rs.getString("mail"));
                e.setNumerocompte(rs.getInt("numerocompte"));
                e.setMdp(rs.getString("mdp"));

            }
            rs.close();
            return e;

            }catch (Exception e) {
                System.out.println("Erreur avec  authentifier() -->" + e.getMessage());
                return (null);
            }

        }

Here is my controller method in wehere I would like to pass 2 parameters:

@GET
    @Path("authentifier/{numerocompte}{mdp}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Client authentifier(@PathParam("numerocompte , mdp") int numerocompte, String mdp) {
        ClientDao dao = new ClientDao();
        System.out.println(numerocompte);
        return dao.authentifier(numerocompte,mdp);
    }

and here is the angular js line, where I will also pass two parameters:

$http.get('rest/client/authentifier/'+$scope.nouveauClient.numerocompte+$scope.nouveauClient.mdp).then(function(data){
                 alert(data.data);
                })
+4
source share
3 answers

All that is required is an additional annotation @PathParam("mdp")in the signature of your controller method.

@GET
@Path("authentifier/{numerocompte}/{mdp}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Client authentifier(@PathParam("numerocompte") int numerocompte, 
                           @PathParam("mdp") String mdp) {

    ClientDao dao = new ClientDao();
    System.out.println(numerocompte);
    return dao.authentifier(numerocompte,mdp);
}

Also, make sure that you have entered your path correctly. In your example, you declared

@Path("authentifier/{numerocompte}{mdp}")

JAX-RS will not be able to find out where it starts numerocompteand where it ends. Separate them from each other like this

@Path("authentifier/{numerocompte}/{mdp}")

and then

$http.get('rest/client/authentifier/'+$scope.nouveauClient.numerocompte+'/'+$scope.nouveauClient.mdp).then(function(data){
             alert(data.data);
})
+2

a @PathParam("pathParamName")

public Client authentifier(
    @PathParam("numerocompte") int numerocompte, 
    @PathParam("mdp") String mdp
)

, .

+2

numerocompte mdp @Path.

Your function authentifiershould look like this

@GET
@Path("authentifier/{numerocompte}/{mdp}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Client authentifier(@PathParam("numerocompte") int numerocompte, @PathParam("mdp") String mdp) {
    ...
}

And the same thing in Angular code

$http.get('rest/client/authentifier/'+$scope.nouveauClient.numerocompte+'/'+$scope.nouveauClient.mdp).then(function(data) { alert(data.data); })
+1
source

Source: https://habr.com/ru/post/1692873/


All Articles