简体   繁体   中英

how to pass a optional param into spring controller and jpql query

this is my repository:

@Query( "SELECT new com.app.gsc.entities.ListeBI(lm.listeDeCriteres.designation,count(*) as totalNumber,lm.ficheDeMission.dateEtHeurDeDepart) from ListeDeControleDetailsMission lm where lm.boolDepart=false OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission GROUP BY lm.listeDeCriteres.designation,DATE(lm.ficheDeMission.dateEtHeurDeDepart)" )
List<ListeBI> getIncidentDepart( @Param( "CodeMission" ) Integer missId );

and heres my controller :

 @RequestMapping( value = "admin/dashboard/getIncidentDepart/{codeMission}", method = RequestMethod.GET )
public Reponse getIncidentDepart( HttpServletResponse response, @PathVariable Integer codeMission ) { // entêtes
    CorsController.getIncidentDepart( response, codeMission );
    if ( messages != null ) {
        return new Reponse( -1, messages );
    }
    List<ListeBI> listeBIs = null;
    try {
        listeBIs = application.getIncidentDepart( codeMission );
    } catch ( Exception e ) {
        return new Reponse( 5, Static.getErreursForException( e ) );
    }
    return new Reponse( 0, Static.getMapForMissionIncidents( listeBIs ) );
}

My problems are: this first problem i want to pass the codeMission as choosen variable, i dont how i can put that but i will explain, if i passe the codeMission variable in the controller the request should work, and if i dont passe the codeMission variable in the controlle the request also should work, how i can do that please, and also i'm not sure if the condition in the request works level is fine:

where lm.boolDepart=false OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission

About JPQL question

Here :

where lm.boolDepart=false OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission


If your :CodeMission param is not null valued, your filter works as expected but if it has null value, it filters on :codeMission==null condition and it is not what you wish.

To address both cases ( null :CodeMission param and not null :CodeMission param) , try that :

where lm.boolDepart=false OR 
      (:CodeMission is null OR lm.ficheDeMission.codeDeFicheDeMission=:CodeMission)

Concretely :

If it is filled with for example 100 as value for :CodeMission , it results in :

where lm.boolDepart=false OR 
      (lm.ficheDeMission.codeDeFicheDeMission=:100)

If it is filled with null as value for :CodeMission , it results in :

where lm.boolDepart=false OR 
      (null is null)

About Rest configuration question

If you use Spring 4 and Java 8, you can make codeMission optional :

@RequestMapping( value = "admin/dashboard/getIncidentDepart/{codeMission}", method = RequestMethod.GET )
public Reponse getIncidentDepart( HttpServletResponse response, @PathVariable Optional<Integer> codeMission ) {
...
}

Otherwise, create two methods with two distinct paths.

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