2013-05-29 5 views

답변

4

메서드가 열거 형으로 전달되므로 일반 메서드로 새 메서드를 추가 할 수 없습니다. 다행히도 Groovy이므로 모든 것이 가능합니다. 우리는 폐쇄의 위임에 org.apache.http.client 방법을 대체 할 수 있습니다 :

import groovyx.net.http.* 
import org.apache.http.client.methods.HttpPatch 

@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.6') 
@Grab(group = 'org.apache.httpcomponents', module = 'httpcomponents-client', version = '4.2') 
def runPatch() { 
    //serverinfo.groovy just returns the request method 
    //Method.DELETE is switched, and won't be used (can't use null, NPE) 
    new HTTPBuilder('http://localhost:9090/serverinfo.groovy').request(Method.DELETE) { 
     delegate.request = new HttpPatch() 
     response.success = { resp, body -> 
      assert resp.status == 200 
      assert body == 'PATCH' 
     } 
    } 
} 

runPatch() 
+0

하지만 HTTP-Builder에서 httpcomponents에서 지원하는 방법에 대해 작동합니다. – JBaruch

0

다른 옵션을 - 0.7-SNAPSHOT를 사용합니다. JAX RS 클라이언트 API를 선호하는 사람들을위한

0

솔루션 :

def client = ClientBuilder.newClient() 
def response = client.target("$baseUrl$restUsersUrl/$userId") 
     .request("application/json") 
     .header("Authorization", "Basic ${authString}") 
     .build("PATCH", Entity.entity(json2Update, MediaType.APPLICATION_JSON)) 
     .invoke() 
if(Response.Status.NO_CONTENT.statusCode == response.status) 
{ 
    println "test" 
}