2012-10-21 40 views
1
@ResponseBody

我想實現的Spring Web MVC @ExceptionHandler有向@ResponseBody返回包含驗證錯誤的對象。 (Strategy E documented here)。春3.1.2 MVC @ExceptionHandler與

在Spring 3.0.x中,有一個確認bug,因爲解決,即正常工作禁止這一點。我使用Spring 3.1.2,並且不應該運行到一個。

我,但是,運行到一個異常「無法找到可以接受的表示」。

這裏的例外:

[10/21/12 12:56:53:296 CDT] 00000045 ExceptionHand E redacted.loggi 
ng.jdk14.LogWrapper error Failed to invoke @ExceptionHandler method: public 
redacted.model.view.ValidationErrorResponse redacted.controller.Re 
stController.handleValidationException(redacted.util.ValidationExceptio 
n,javax.servlet.http.HttpServletResponse) 

Originating Class/Method:redacted.web.ui.filter.AccessControlFilter.pr 
ocessFilter() 

           org.springframework.web.HttpMediaTypeNotAccepta 
bleException: Could not find acceptable representation 
     at org.springframework.web.servlet.mvc.method.annotation.AbstractMessage 
ConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMeth 
odProcessor.java:115) 

而這裏的代碼:

@ExceptionHandler(ValidationException.class) 
@ResponseStatus(value = HttpStatus.PRECONDITION_FAILED) 
@ResponseBody 
public ValidationErrorResponse handleValidationException(ValidationException ex, HttpServletResponse response) { 
    List<ValidationError> errs = new LinkedList<ValidationError>(); 
    for (ObjectError er : ex.getErrors()) { 
     errs.add(new ValidationError(er.getObjectName(), er.getDefaultMessage())); 
    } 

    return new ValidationErrorResponse(errs); 

} 

@RequestMapping(value = "/search") 
@ResponseBody 
public SearchResult search(@Valid @ModelAttribute SearchRequest searchRequest, BindingResult bResult) { 
    if (bResult.hasErrors()) { 
     throw new ValidationException(bResult.getAllErrors()); 
    } 
    return searchService.search(searchRequest); 
} 

任何想法?

+0

您確實有一個「application/json」權限的Accept標頭,如果有的話,請您展示您的ValidationException的外觀。 –

回答

0

的問題似乎是,Spring並不怎麼啥子轉換的ValidationErrorResponse,例如它應該將其轉換爲JSON或XML或其他什麼東西?嘗試返回ResponseEntity,因爲你可以直接控制的內容類型。

protected ResponseEntity<ValidationErrorResponse> createResponseEntity(ValidationErrorResponse restApiError) 
    { 
     HttpHeaders headers = new HttpHeaders(); 
     headers.setContentType(MediaType.APPLICATION_JSON); 

      // assuming your ValidationErrorResponse object has an http code stored in it 
     HttpStatus responseStatus = HttpStatus.valueOf(restApiError.getHttpStatus()); 
     ResponseEntity<RestApiError> result = new ResponseEntity<>(restApiError, headers, responseStatus); 
     return result; 
    } 

我使用類似在我的申請,我也有一堆,我可以拋出包含wihtin他們足夠的信息能夠產生適當的錯誤響應RuntimeExceptions的。

+0

有沒有辦法爲「默認」或「鎖定」迴應表示,以JSON? – rustyx

1

如果你正在讀這篇文章,你可能有關於我如何得到這個工作的一些問題。簡短的回答是,我不知道。我恢復了我的代碼,並重新實現(實際上是複製並粘貼控制器位),從這裏,它突然開始工作。

如果你使用Spring 3.1.2,@ExceptionHandler @ResponseBody並沒有工作。

我試探性的解釋,爲什麼這並沒有最初的工作是,它可能是一個用戶錯誤或(非常複雜,這個應用程序是巨大的)部署我uing腳本錯誤。

1

,當我需要返回,與一些已知的擴展名,如「/blah/blah.xml」結尾的請求的JSON錯誤響應我有這個問題。在這種情況下,春季有利於擴展基於表示匹配,而忽略Accept頭(也就是用最新的Spring 4.1.5的情況下)。

我用下面的「鎖定」,以JSON響應無論請求是什麼。

@Configuration 
@EnableWebMvc 
public class ApplicationConfiguration extends WebMvcConfigurerAdapter { 
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { 
     configurer.favorPathExtension(false); 
     configurer.ignoreAcceptHeader(true); 
     configurer.defaultContentType(MediaType.APPLICATION_JSON); 
    } 
}