我正在使用下面提到的Spring代码来处理异常并将响应返回给客户端。由于我在这里使用了ResponseBody注释,我希望spring能够在出现错误的情况下返回JSON响应,但我看到下面在客户端收到的响应(JSON响应在responseText内部,而不是直接返回到客户端)。请告知如何将JSON响应返回给Caller -
客户端收到的回复:
Object {readyState: 4, responseText: "{"status":false,"msg":"User Data not available","r…"MARSAPI003","noOfRecords":0,"responseList":null}", responseJSON: Object, status: 403, statusText: "Forbidden"}
春季代码: -
@ExceptionHandler(MarsUnAuthorizedOperation.class)
@ResponseBody
@ResponseStatus(value=HttpStatus.FORBIDDEN)
public MarsResponse unAuthorizedOperationExceptionHandler(final Exception ex){
final MarsResponse response = new MarsResponse();
response.setNoOfRecords(0);
response.setMsg(ex.getMessage());
response.setResponseCode(marsMessageProperties.getUnauthorizedOperationErrorCd());
response.setResponseMessage(marsMessageProperties.getUnauthorizedOperationErrorMsg());
response.setStatus(false);
return response;
}
答案 0 :(得分:-1)
我认为这是你的@ResponseBody注释阻止spring将MarsResponse对象编组为JSON对象。尝试删除它并再次检查。