2017-11-27 20 views
-2

コンテキストは: アップロード用のcsvサービスを作成しています。コンテンツの検証を行うときに、エラーメッセージと問題の行を含むカスタムランタイム例外をスローしたいとします。2つのカスタム情報を含むjsonレスポンスを送信するカスタムランタイム例外を書き込む方法は?

public class NotValidCsvException extends RuntimeException { 
    private static final long serialVersionUID = 1L; 
    private Integer _line; 

    public NotValidCsvException(final String message, final Integer line) { 
    super(formatMessage(message, line)); 
    } 

    public static String formatMessage(String message, Integer line) { 
    return new StringBuilder() 
       .append("{message: ") 
       .append("'") 
       .append(message) 
       .append("'") 
       .append(", ligne: ") 
       .append("'") 
       .append(line) 
       .append("'") 
       .append("}").toString(); 
    } 
} 

しかし、問題は私のようなレスポンスだということです::

何私は今のところ得たことはある

{"timestamp":1511785651810,"status":500,"error":"Internal Server Error","exception":"com.sstrn.pa.service.impl.exception.NotValidCsvException","message":"{"message": "message_import_csv_undefined_thing", "line": 0}"} 

をしかし、私はしたいのですが、何かのように:

{"timestamp":1511785651810,"status":500,"error":"Internal Server Error","exception":"com.sstrn.pa.service.impl.exception.NotValidCsvException","message":"message_import_csv_undefined_thing", "line": 0} 
+2

EHM ...あなたが他の合成オブジェクトを作成するだけで同じように、しかし、この1つは(ランタイム)例外を拡張? – Stultuske

+0

しかし、考え方は、jsonの応答で 'errCode'を持っていることです – Snipzer

+1

ok ..と?私は問題が表示されません – Stultuske

答えて

0

これを行うには、私はNotValidCsvExceptionによって実装されたAttributeLineEnabledを作成してインターフェイスします。

public interface AttributeLineEnabled { 
    public Integer getLine(); 
} 

public class NotValidCsvException extends RuntimeException implements AttributeLineEnabled { 
    private static final long serialVersionUID = 1L; 

    private Integer _line; 

    public NotValidCsvException(final String message) { 
    super(message); 
    } 

    public NotValidCsvException(final String message, Integer line) { 
    super(message); 
    _line = line; 
    } 

    public Integer getLine() { 
    return _line; 
    } 

    public void setLine(final Integer line) { 
    _line = line; 
    } 
} 

は、私はDefaultErrorAttributesを拡張し、ErrorAttributesを実装コントローラを作成しました。 このコントローラーは、jsonResponseを設定したgetErrorAttributesメソッドをオーバーライドします。

public class BaseErrorAttributes extends DefaultErrorAttributes implements ErrorAttributes { 

private static final String LINE = "line"; 

public BaseErrorAttributes() { 
    super(); 
} 

public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) { 
    Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace); 
    Throwable error = getError(requestAttributes); 
    if (error instanceof AttributeLineEnabled) { 
     AttributeLineEnabled attributeLineEnabled= (AttributeLineEnabled) error; 
     errorAttributes.put(LINE, attributeLineEnabled.getLine()); 
    } 
    return errorAttributes; 
} 
} 

と私は輸入プロセスを作る私のコントローラに注入:

private BaseErrorAttributes _baseErrorAttributes; 
// getter and setter too 
関連する問題