jQuery 警告 'undefined' 错误而不是 Java/Spring 异常详细信息
jQuery alerts 'undefined' on error instead of Java/Spring Exception details
我一直在我的前端界面中使用 Backbone 作为 Spring 支持的 Java 程序。在该页面上,用户提交一个文件,该文件随后得到处理,并在 return 中获得下载。当 Java 代码一切正常时,他们就会按预期获得下载。但是,如果出现问题,它会停止,并且页面 return 是一个警告框,上面写着 'undefined'.
我希望能够return 交互错误,以便用户可以知道哪里出了问题。我有一个看起来像这样的 RestExceptionHandler class,我想 return 警告框中的用户异常。
@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));
}
}
为了了解 Java 脚本的一些背景知识,我为用户单击提交编写了一个事件函数。它从页面获取输入,将其设置为 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 ...
这是 Spring 控制器...
@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
获取信息。异常处理程序不需要 return 一个响应实体,只需要一个带有异常的字符串(看起来也更干净)
@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler({ Exception.class })
public String handleGeneralException(final Exception e) {
logException("General Exception : ", e);
return e.toString();
}
然后,如果 HttpStatus 不正常(不确定模型和响应是否必要),我的 JavaScript 会提醒消息:
error: function (model, response, xhr) {
var json = JSON.stringify(xhr.xhr.responseText);
json = json.replace(/"/g, "");
alert(json);
},
Stringify return 将消息用双引号引起来,因此我只是将它们替换为任何内容以获得更清晰的消息。控制器中无需更改任何内容。
我一直在我的前端界面中使用 Backbone 作为 Spring 支持的 Java 程序。在该页面上,用户提交一个文件,该文件随后得到处理,并在 return 中获得下载。当 Java 代码一切正常时,他们就会按预期获得下载。但是,如果出现问题,它会停止,并且页面 return 是一个警告框,上面写着 'undefined'.
我希望能够return 交互错误,以便用户可以知道哪里出了问题。我有一个看起来像这样的 RestExceptionHandler class,我想 return 警告框中的用户异常。
@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));
}
}
为了了解 Java 脚本的一些背景知识,我为用户单击提交编写了一个事件函数。它从页面获取输入,将其设置为 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 ...
这是 Spring 控制器...
@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
获取信息。异常处理程序不需要 return 一个响应实体,只需要一个带有异常的字符串(看起来也更干净)
@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler({ Exception.class })
public String handleGeneralException(final Exception e) {
logException("General Exception : ", e);
return e.toString();
}
然后,如果 HttpStatus 不正常(不确定模型和响应是否必要),我的 JavaScript 会提醒消息:
error: function (model, response, xhr) {
var json = JSON.stringify(xhr.xhr.responseText);
json = json.replace(/"/g, "");
alert(json);
},
Stringify return 将消息用双引号引起来,因此我只是将它们替换为任何内容以获得更清晰的消息。控制器中无需更改任何内容。