2016-08-16 10 views
0

私は、SpringでバックアップされたJavaプログラムのフロントエンドインタフェースでバックボーンを使用しています。このページでは、ユーザーがファイルを送信し、処理されたファイルが返されます。 Javaコードを使ってすべてがうまく動くと、ダウンロードされたものがダウンロードされます。しかし、何かがうまくいかなかった場合、それは停止し、ページには「未定義」というアラートボックスが返されます。Java/Spring例外の代わりにjQueryアラート「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を少し背景として、ユーザーがサブミットをクリックしたときのイベント機能を記述しました。それは、ページからの入力を取得するバックボーンモデルにそれを設定し、それから...

// 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); 

    } 
} 

私は必要な追加のコードを提供するために満足しています。

答えて

0

自分の質問に答えました!

responseからの情報を誤って取得するのではなく、xhrから取得する必要がありました。 httpStatusモデルがあればわからない(ないOKであれば、例外ハンドラは、応答エンティティを返すために(も非常にきれいに見えます)を除いて、単なる文字列

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

その後、私はJavaScriptがメッセージに警告を必要としません。

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

文字列化は、二重引用符で囲まれたメッセージを返しますので、私はちょうどクリーナーメッセージの何もそれらを置き換え:及びレスポンス)が必要です。コントローラーで何も変更する必要はありませんでした。

関連する問題