2017-09-26 11 views
0

나는 하나 개의 인수 소요 Spring 컨트롤러가 - Pageable : Postman를 통해Diffrent 솔루션을 사용하여 컨트롤러에 PARAMS 각도

@RequestMapping(value = "/all", method = RequestMethod.GET) 
    public Page<Item> getAll(Pageable pageable) {  
     return itemService.findAll(pageable); 
    } 

내가 같은 query params 제공하여이 api를 호출 할 수 있습니다 : http://localhost:8080/api/item/all?page=0&size=2을하고 난 json있어 두 가지 요소가있는 응답.

Angular 4에서 어떻게 이런 종류의 API를 호출 할 수 있습니까?

ngOnInit() { 
    let search = new URLSearchParams(); 
    search.set('page', '0'); 
    search.set('size', "2"); 
    console.log('------------', search); 
    this.itemService.getItems({search:search}) 
     .subscribe(res => { 
     console.log('teitems', res.json()); 
     this.items = res.json().content; 
     }); 
    } 

그러나 일반적으로 모든 및 탐색 대상의 모습에서 작동하지 않습니다이

getItems(page, size){ 
    return this.http.get(this.url + '/all?page=' + page + '&size=' + size); 
    } 

내가 같은 것을 사용하고 싶습니다? :보다 Angular 4query params을 보낼 수있는 다른 방법이 있나요 enter image description here

브라우저 요청을 더에서 network 탭의 모습은 무엇 :

때문에 사실을 의심 당신이 보는대로

enter image description here

는 그래서 request URL 내가 Postman에서 위의 게시하는 것과 다릅니다.

+0

getItems()는 숫자로 간주되는 두 개의 인수 인 page와 size를 필요로합니다. 객체 ('getItems ({search : search})') 인 단일 인수로 호출합니다. 그게 어떻게 작동할까요? –

+0

나는 또한 그것을 검사하고 작동하지 않는다 – bielas

답변

0

귀하의 질문은 많이 발견 된 것과 같습니다 here. 거기에서 답을보십시오. 요약

:

import { HttpParams } from '@angular/common/http'; 

params = params.append('var1', val1); 

this.http.get('base_url', {params: params}); 

수동 (HttpParams를 사용되지 않음) PARAM 개체를 만들 수있다.

this.http.get('base_url', {params: {'var1': val1}}); 
+0

이 해결책에서는 나의'request url'는 그 매개 변수없이'http : // localhost : 4200/api/item/all'과 같이 보이기 때문에'rest controller'는 읽을 수 없다 그것 제대로. – bielas