2014-09-23 41 views
2

以下是使用GlassFish wadl2java工具wadl2java生成客戶端拋出的成功發佈UniformInterfaceException並提出

public com.sun.jersey.api.client.ClientResponse putXmlAsClientResponse(com.client.BedOrder input) { 
       UriBuilder localUriBuilder = _uriBuilder.clone(); 
       com.sun.jersey.api.client.WebResource resource = _client.resource(localUriBuilder.buildFromMap(_templateAndMatrixParameterValues)); 
       com.sun.jersey.api.client.WebResource.Builder resourceBuilder = resource.getRequestBuilder(); 
       resourceBuilder = resourceBuilder.type("application/xml"); 
       com.sun.jersey.api.client.ClientResponse response; 
       response = resourceBuilder.method("PUT", com.sun.jersey.api.client.ClientResponse.class, new JAXBElement(new QName("", "bedOrder"), com.client.BedOrder.class, input)); 
       if (response.getStatus()>= 400) { 
        throw new OrderEntryFapi.WebApplicationExceptionMessage(javax.ws.rs.core.Response.status(response.getClientResponseStatus()).build()); 
       } 
       return response.getEntity(com.sun.jersey.api.client.ClientResponse.class); 
      } 

生成的代碼示例每當我用產生的任何職位的方法,或者將方法拋出UniformInterfaceException。原因是每個方法都返回一個完美工作的ClientResponse對象。但是,而不是返回響應對象本身的方法調用getEntity從而未能在204 No Content Status code

其他的情況下比手動修改生成的代碼,這樣,如果響應202的狀態代碼我返回response而不是response.getEntity(com.sun.jersey.api.client.ClientResponse.class)我能做?

基本上,我想對所有的請求以包含204個特例看起來像這樣:

if (response.getStatus() == 204) { 
    return response; 
} 

回答

1

在你的WADL,定義錯誤狀態和你期望的類型是..

<wadl:response status="200 204"> 
    <wadl:representation mediaType="application/xml" 
    element="xs:SuccessResponse" /> 
</wadl:response> 

<wadl:response status="400 401 406 415"> 
    <wadl:representation mediaType="application/xml" 
    element="xs:ErrorResponse" /> 
</wadl:response> 

這將改變生成的代碼有類似:

switch (response.getStatus()) { 
    case 400 : 
    case 401 : 
    case 406 : 
    case 415 : 
     throw new ErrorResponseException(response, response.getEntity(ErrorResponse.class)); 
    default: 
     if (!com.sun.jersey.api.client.ClientResponse.class.isAssignableFrom(returnType)) { 
      if (response.getStatus()>= 400) { 
       throw new OrderEntryFapi.WebApplicationExceptionMessage(Response.status(response.getClientResponseStatus()).build()); 
       } 
      } 
     } 
相關問題