2011-04-12 12 views
6

Bir MVP'deki bir özel durum işleyicisinde neden @ResponseStatus(reason = "My message") kullanamadığımı bilen herkes hala bir @ResponseBody döndürüyor mu? Ne olur gibi görünüyor ben reason ÖzellikSpring MVC: @ResponseStatus kullanarak (reason = '') tomcat'daki bir @ResponseBody kural dışı durum işleyicisi

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

kullanırsanız code for this example github üzerinde olmasıdır.

+0

aynı sorun :-(makineleri burada imho olmayan bir birinci sınıf DİNLENME çerçeve, Web olarak Bahar WebMVC düşünmüyoruz 29075160/no-responsebody-iade-den-exceptionhandler-in-yay-çizme-app-konuşlandırılmış-in –

cevap

5

Bu 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 
} 

Bu SPR-8251 yılında SpringSource bildirilmiştir aşağıdaki kod doğrudan bir sonucu olduğu görülmektedir: Bahar 3.2 beri Kayıt için

3

, bu daha da kötüleşti AnnotationMethodHandlerExceptionResolverResponseStatusExceptionResolver yerini ve olmuştur çünkü yapar:

T Onun bir hata raporuna değer. Ayrıca, @ResponseStatus, setStatus ile belgelenmiştir ve kötü tasarlanmıştır. @ResponseError olarak adlandırılmış olmalıydı.

Sonunda bunun için iki sorun oluşturdum: SPR-11192 ve SPR-11193.

Neredeyse bir yıl geçti ve iki sorunum hala açık. http://stackoverflow.com/questions/: Ben MVC humas için olup

+0

@BartoszKP sen MY yanıta düzenlemeler kaldırıldı mı Neden? –

+1

1) sadece "düzenlemek göstergelerini" dışarı düzenlenebilir. Cevap, bir bütün olarak tutarlı olmalı, sorunun parçası için bir çözüm arayan bir kişi ile tamamen alakasız olan kısmı ekledi. Birinin posta tarihi ile ilgilenmesi gerekiyorsa, yine de kolayca ulaşılabilir. 2) Bu senin * cevabın değil. Sen yazarsın, ama bu SO'ya ait. – BartoszKP

+0

@BartoszKP Ciddi, saçma! –

İlgili konular