2016-01-08 43 views
0

我管我的项目弹簧安置异常处理程序返回XML格式

异常这是我的GET端点:

@RequestMapping(method = RequestMethod.GET) 
public ResponseEntity<V6SubnetRec> get(@RequestBody V6SubnetRequest requestBody) throws QIPException { 
    Site site = getSite(requestBody.getOrganization()); 
    V6SubnetRec wsSubnet = (V6SubnetRec) requestBody.getV6Subnet(); 
    V6SubnetRec v6SubnetRec = null; 
    try { 
     v6SubnetRec = getQipService1().getV6Subnets(wsSubnet, site); 
    } catch (Exception e) { 
     log.error(Keys.QIP_CALLOUT_ERROR, e); 
     throw new RestException(e); 
    } 
    return new ResponseEntity<V6SubnetRec>(v6SubnetRec, HttpStatus.OK); 
} 

@ExceptionHandler(RestException.class) 
public ResponseEntity rulesForRestException(RestException restEx){ 
    return new ResponseEntity(restEx.getResponse().getContent(), restEx.getResponse().getStatus()); 
} 

RestException.java

@XmlRootElement(name = "RestException") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class RestException extends RuntimeException{ 

    @XmlElement 
    RestResponse response; 

    public RestException(Exception e){ 
    //... 
    } 
} 

当我以URL http://localhost/api/v1/v6subnet.json(以JSON格式返回)请求时,它将返回HTTP状态码404a包括内容。没关系。 enter image description here

但是,当我与URL http://localhost/api/v1/v6subnet.xml请求(具有返回XML格式)具有相同的请求,其返回的HTTP状态代码500等,其没有限制,JSON格式处理的正常的异常

enter image description here

我希望获得像我向JSON格式请求的结果。

谢谢。

回答

0

我解决了我的问题。这是一个从

restEx.getResponse().getContent() 

唯一改变成

restEx.getResponse().getContent().toString()