2013-05-29 131 views

回答

4

由於該方法以Enum方式傳遞,因此無法以正常方式添加新方法。幸運的是,它是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建設者通過httpcomponents支持的任何方法工作。 – JBaruch

0

解決方案爲那些,誰喜歡JAX RS客戶端API:

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" 
}