jQuery alert 'undefined'错误而不是Java/Spring异常详细信息

jQuery alerts 'undefined' on error instead of Java/Spring Exception details

本文关键字:Java Spring 异常 详细信息 alert undefined 错误 jQuery      更新时间:2023-09-26

我一直在使用Backbone作为一个基于Spring的Java程序的前端接口。在页面上,用户提交一个文件,然后对其进行处理,并获得一个下载作为回报。当Java代码一切正常运行时,它们就会得到预期的下载。但是,如果出现问题,它会停止,并且页面返回一个警告框,上面写着'undefined'。

我希望能够返回交互式错误,以便用户可以知道出了什么问题。我有一个RestExceptionHandler类,看起来像这样,我想在警告框中将异常返回给用户。

@Slf4j
@ControllerAdvice
public abstract class RestExceptionHandler {
    @ExceptionHandler({ Exception.class })
    public ResponseEntity<String> handleGeneralException(final Exception exception) {
        logException("General Exception : ", exception);
        return new ResponseEntity<String>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
    @ExceptionHandler({ IllegalArgumentException.class, JsonException.class })
    public ResponseEntity<String> handleBadRequestException(final Exception exception) {
        logException("Bad Request Exception : ", exception);
        return new ResponseEntity<String>(exception.getMessage(), HttpStatus.BAD_REQUEST);
    }
    private void logException(final String info, final Exception exception) {
        log.error(info + exception.getMessage() + Throwables.getStackTraceAsString(exception));
    }
}
关于JavaScript的一点背景知识,我编写了一个用户单击submit时的事件函数。它从页面获取输入,将其设置为Backbone模型,然后…
// post to output and return download
    model.url = window.location + "output";
    model.save({}, 
    {
        error: function (model, response) {
            alert(response.message);
        },
    success: function (model, response) {
        // download stuff goes here ...

这是弹簧控制器…

@RestController
public class ValidateController extends RestExceptionHandler {
    @Autowired
    private OntologyValidate validate;
    @RequestMapping(value="/output", method= RequestMethod.POST)
    public @ResponseBody Object graph(@RequestBody String body) throws Exception {
        JSONObject jo = new JSONObject(body);
        // get user input arguments from body
        JSONArray JSONinputFiles = jo.getJSONArray("inputFile");
        JSONArray JSONfileFormats = jo.getJSONArray("fileFormat");
        JSONArray JSONfileNames = jo.getJSONArray("fileName");
        String label = jo.getString("label");
        String description = jo.getString("description");
        String fin = "";
        // Do processing stuff here
        jo.put("results", fin);
        return new ResponseEntity<Object>(jo.toString(), HttpStatus.OK);
    }
}

回答了我自己的问题!

我需要从xhr中获取信息,而不是尝试从response中获取错误信息。异常处理程序不需要返回一个响应实体,只需要返回一个带有异常的字符串(这样看起来也干净多了)

@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler({ Exception.class })
public String handleGeneralException(final Exception e) {
    logException("General Exception : ", e);
    return e.toString();
}

然后我的JavaScript警告消息,如果HttpStatus不确定(不确定是否模型和响应是必要的):

error: function (model, response, xhr) {
    var json = JSON.stringify(xhr.xhr.responseText);
    json = json.replace(/"/g, "");
    alert(json);
},

Stringify以双引号返回消息,因此我只是将它们替换为一个更简洁的消息。