2015-01-16 2 views
1

grails2.4.4 및 grails-rest-client-builder : 2.0.0 플러그인을 사용하고 있습니다. 요청 메소드 인 PATCH를 허용하는 REST URL을 호출해야합니다. 그러나 나는이 플러그인 할 수 아니에요 : 내가 코드 아래 사용하고 있습니다 :PATCH 요청 메소드가있는 Grails 'grails-rest-client-builder'플러그인

def rest = new RestBuilder() 
def resp = rest.patch("$URL") { 
    header 'Authorization', "Bearer $accessToken" 
} 

나는 오류가 아래에 받고 있어요 :

Invalid HTTP method: PATCH. Stacktrace follows: 
Message: Invalid HTTP method: PATCH 
Line | Method 
    440 | setRequestMethod in java.net.HttpURLConnection 
    307 | invokeRestTemplate in grails.plugins.rest.client.RestBuilder 
    280 | doRequestInternal . in  '' 

사람이 나 좀 도와 주시겠습니까?

답변

3

확인. 마침내 몇 번의 시행 착오 끝에 그것을 만들었습니다. java.net.HttpURLConnection은 기본적으로 PATCH와 같은 맞춤 요청 방법을 지원하지 않으므로 해당 오류가 발생합니다. 그래서 나는 그러한 요청 방법을 지원하는 commons-httpclient과 같은 제 3 자 라이브러리를 갈 필요가있다. 그래서 commons-httpclient(now it is named as apache-httpcomponents)을 삽입하여 PATCH 요청 방법과 작동되도록했습니다.

아래는 내가 만드는했던 변화는 작동됩니다

먼저 Grails를 BuildConfig.groovy에 종속성을 추가 솔루션 # 1

수동 객체 생성으로 이동하려면

runtime "org.apache.httpcomponents:httpclient:4.3.6" 

:

RestTemplate restTemplate=new RestTemplate() 
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); 

def rest=new RestBuilder(restTemplate) 
def resp = rest.patch("$URL"){ 
     header 'Authorization', "Bearer $accessToken" 
    } 

솔루션 # 2

사용 Grails의-봄 주입 :

이 클래스를 주입 restBuilderresources.groovy

import grails.plugins.rest.client.RestBuilder 
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory 
import org.springframework.web.client.RestTemplate 

beans={ 
    httpClientFactory (HttpComponentsClientHttpRequestFactory) 
    restTemplate (RestTemplate,ref('httpClientFactory')) 
    restBuilder(RestBuilder,ref('restTemplate')) 
} 

에 구성 아래에있는 추가 :이 사람을 도움이

class MyRestClient{ 
    def restBuilder 

    .... 

    def doPatchRequest(){ 
    def resp=restBuilder.patch("$API_PATH/presentation/publish?id=$presentationId"){ 
      header 'Authorization', "Bearer $accessToken" 
     }; 

    //do anything with the response 
    } 

} 

희망.

관련 문제