2015-03-03 19 views
6

Hiện tại, tôi nhận được HttpResponseException, chỉ có statusCode. Làm cách nào tôi có thể nhận được phản hồi đầy đủ?Cách nhận phản hồi hoàn chỉnh cho phản hồi Groovy RestClient không thành công

Đây là mã Tôi đang sử dụng

restClient = new RESTClient("http://${Server}") 
try { 
    HttpResponseDecorator resp = restClient.post(path,body,requestContentType)  
     as HttpResponseDecorator 
    return JSONObject.fromObject(resp.getData()).get("topKey",""); 
    } 
catch (HttpResponseException e) { 
      error(e.toString()) 
    } 

Và nó chỉ kết quả này:

[oaf.error] groovyx.net.http.HttpResponseException: Internal Server Error 

Trả lời

4

Thêm xử lý phản ứng tùy chỉnh thất bại:

 restClient = new RESTClient("http://${Server}") 
     restClient.handler.failure = { resp, data -> 
      resp.setData(data) 
      String headers = "" 
      resp.headers.each { 
       headers = headers+"${it.name} : ${it.value}\n" 
      } 
      throw new HttpResponseException(resp.getStatus(),"HTTP call failed. Status code: ${resp.getStatus()}\n${headers}\n"+ 
              "Response: "+(resp as HttpResponseDecorator).getData()) 
      return resp 
     } 
0

Trên thực tế, bạn có thể trích xuất các phản ứng đầy đủ từ ngoại lệ được ném. Ví dụ: nếu ngoại lệ bị bắt của bạn là e và nội dung phản hồi JSON phải chứa một trường có tên là myCustomErrorCode, bạn có thể kiểm tra giá trị của nó bằng cách xem e.response.data.myCustomErrorCode ngoài e.statusCode.

Các vấn đề liên quan