2011-04-12 10 views
6

में एक @ResponseBody अपवाद संचालक पर @ResponseStatus का उपयोग कर किसी को भी पता है कि मैं क्यों वसंत MVC में अपवाद संचालक पर @ResponseStatus(reason = "My message") उपयोग नहीं कर सकते, जबकि अभी भी एक @ResponseBody लौटने। क्या लगता है कि अगर मैं reason विशेषता का उपयोगवसंत MVC: (कारण = '') बिल्ला

// this exception handle works, the result is a 404 and the http body is the json serialised 
// {"message", "the message"} 
@ExceptionHandler 
@ResponseStatus(value = HttpStatus.NOT_FOUND) 
public Map<String, String> notFoundHandler(NotFoundException e){ 
    return Collections.singletonMap("message", e.getMessage()); 
} 

// this doesn't... the response is a 404 and the status line reads 'Really really not found' 
// but the body is actually the standard Tomcat 404 page 
@ExceptionHandler 
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Really really not found") 
public Map<String, String> reallyNotFoundHandler(ReallyNotFoundException e){ 
    return Collections.singletonMap("message", e.getMessage()); 
} 

code for this example GitHub पर खत्म हो गया है है।

+0

वही समस्या यहां: http://stackoverflow.com/questions/ 29075160/no-responsebody-लौटे-से-exceptionhandler में वसंत-बूट, एप्लिकेशन के तहत तैनात-इन –

उत्तर

5

ऐसा लगता है कि इस AnnotationMethodHandlerExceptionResolver

private ModelAndView getModelAndView(Method handlerMethod, Object returnValue, ServletWebRequest webRequest) 
     throws Exception { 

    ResponseStatus responseStatusAnn = AnnotationUtils.findAnnotation(handlerMethod, ResponseStatus.class); 
    if (responseStatusAnn != null) { 
     HttpStatus responseStatus = responseStatusAnn.value(); 
     String reason = responseStatusAnn.reason(); 
     if (!StringUtils.hasText(reason)) { 
      // this doesn't commit the response 
      webRequest.getResponse().setStatus(responseStatus.value()); 
     } 
     else { 
      // this commits the response such that any more calls to write to the 
      // response are ignored 
      webRequest.getResponse().sendError(responseStatus.value(), reason); 
     } 
    } 
    /// snip 
} 

यह SPR-8251 में Springsource सूचित किया गया है से निम्नलिखित कोड का एक सीधा परिणाम है:

3

रिकॉर्ड के लिए, स्प्रिंग 3.2 के बाद, यह और भी बदतर हो गई क्योंकि AnnotationMethodHandlerExceptionResolverResponseStatusExceptionResolver द्वारा बदल दिया गया है और यह करता है:

टी वह एक बग रिपोर्ट के लायक है। इसके अलावा, @ResponseStatussetStatus के साथ प्रलेखित है और यह खराब है। इसे @ResponseError कहा जाना चाहिए था।

मैंने इस के लिए दो मुद्दों का निर्माण किया है: SPR-11192 और SPR-11193

लगभग एक साल बीत चुका है और मेरे दो मुद्दों अभी भी खुले हैं। मैं स्प्रिंग वेबएमवीसी को प्रथम श्रेणी के आरईएसटी ढांचे के रूप में नहीं मानता, जो कि यह नहीं है, वेब एमवीसी humas के लिए है और मशीन नहीं :-(

+0

@BartoszKP आप मेरा जवाब पर संपादन हटाया क्यों किया? –

+1

1) मैं केवल बाहर संपादित "संकेतक संपादित करें"। उत्तर पूरी तरह से संगत होना चाहिए, तथ्य यह है कि कौन सा हिस्सा जोड़ा गया था जब किसी व्यक्ति को उनकी समस्या का समाधान ढूंढने के लिए पूरी तरह से अप्रासंगिक है। क्या किसी को पोस्ट के इतिहास में रुचि होनी चाहिए, यह अभी भी आसानी से उपलब्ध है। 2) यह * आपका * उत्तर नहीं है। आप लेखक हैं, लेकिन यह एसओ से संबंधित है। – BartoszKP

+0

@ BartoszKP हास्यास्पद, गंभीरता से! –

संबंधित मुद्दे