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;
}   
6
  • 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. Commented Dec 26, 2017 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: stackoverflow.com/questions/16930473/… and stackoverflow.com/questions/21667613/… Commented Dec 26, 2017 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? Commented Dec 26, 2017 at 16:24
  • Use @RequestParam in your method arguments so Spring can bind them, also use the @RequestMapping.params array to narrow the method that will be used by spring. See this answer for details. Commented Dec 26, 2017 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? Commented Dec 26, 2017 at 17:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.