0

I have this situation in Angular . I want to pass some parameter for a SQL query to spring.

This is the variable in angular i want to pass in Spring

var medicoRicercato = {
   nome: $scope.medico.nome,
   cognome: $scope.medico.cognome,
   codiceFiscale: $scope.medico.codiceFiscale,
   codiceSpecialita: $scope.specialitaSelezionata.codice,
   codicePrestazione: $scope.prestazione.codice
};

This is the http.get request i try to do

$http({
    url: 'http://localhost:8080/ProgettoClinicaAngular/getMedici/',
    method: "GET",
    params:medicoRicercato
}).then(
    function successCallback(response) {
    },
    function errorCallback(response) {
    }
);

And this is my Spring controller

@RequestMapping(value="/getMedici", method=RequestMethod.GET)
 public MedicoView authenticateUser(@RequestBody MedicoView medico) {
     return medico;
 } 

i try to do so much solution i read on internet , but i not find one work for me , all solution have a problem. I try also with @PathVariable in spring controller , but from angular how i struct a string to pass ? i can pass this like a simple string with space for difference the parameter i want to pass? i cant pass this like a JSON?

Then , can anyone tell me :

Wich is the best way for pass parameter in HTTP GET from angular to Spring?

SOLUTION

With help of all i found a solution.

This is my angular code for pass parameter

var medicoRicercato = {
    nome: $scope.medico.nome,
    cognome: $scope.medico.cognome,
    codiceFiscale: $scope.medico.codiceFiscale,
    codiceSpecialita: $scope.specialitaSelezionata.codice,
    codicePrestazione: $scope.prestazione.codice
};

$http.get('http://localhost:8080/ProgettoClinicaAngular/getMedici/',{params: 
medicoRicercato}).then(
   function successCallback(response) {},
   function errorCallback(response) {}
);

This is my Spring controller

@RequestMapping(value ="/getMedici")
public List<Medico> getUsersForGrid( @RequestParam(value = "nome",required=false) String nome,  @RequestParam(value = "cognome",required=false ) String cognome, @RequestParam(value = "codiceFiscale", required=false) String codiceFiscale,  @RequestParam(value = "codiceSpecialita", required=false) Integer codiceSpecialita, @RequestParam(value = "codicePrestazione", required=false) Integer codicePrestazione)
{
    List<Medico> listaMedici=null;
            
    listaMedici=medicoBuisiness.getMediciWithQuery(nome, cognome, codiceFiscale, codiceSpecialita, codicePrestazione);
    
    return listaMedici;
}   
Narm
  • 10,677
  • 5
  • 41
  • 54
Simone
  • 29
  • 1
  • 7
  • thanks for reply , but i'm not understand how i can pass in json format from angular , and how i can get json format in spring? I try so much solution , but all the solution i try i have error. – Simone Dec 26 '17 at 16:05
  • First of all I think you mean to use "POST" rather than "GET"? Since you wanted to send data from your app to Spring, correct? Aside from that, your data needs to be serialized first. Here are two links from StackOverflow that describe how to do that in detail: https://stackoverflow.com/questions/16930473/angularjs-factory-http-get-json-file and https://stackoverflow.com/questions/21667613/in-angular-how-to-pass-json-object-array-into-directive – Koenigsberg Dec 26 '17 at 16:12
  • No with post i have not problem , but with get i have so much problem. I want to pass a parameter for a query , for this i use GET and not POST . how i can pass this parameter from angular , and get this parameter in spring controller? – Simone Dec 26 '17 at 16:24
  • Use [@RequestParam](http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html) in your method arguments so Spring can bind them, also use the [@RequestMapping.params](http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params%28%29) array to narrow the method that will be used by spring. See [this answer](https://stackoverflow.com/a/13213193/5535245) for details. – georgeawg Dec 26 '17 at 17:26
  • Thanks for reply , but i have not understand what is the right way with pathvariable or or requestparam . I hava to do this with requestParam , but i have a error , i think because i not struct a url in right way from angular . How i can create url from angular , and then receive the parameters correctly in spring with @requestparam? – Simone Dec 26 '17 at 17:50
  • The AngularJS framework will automatically create the proper URl from the `params` object. Use the network tab in the Developer Console to verify that the GET request is properly constructed. – georgeawg Dec 26 '17 at 20:12

0 Answers0