2014-04-24 2 views
0

후 나는 는 스프링 MVC : 예외도 200 OK 응답

@RequestMapping(value="fetchprofilepics/{profileId}/{column}/{random}",method=RequestMethod.GET) 
public void fetchProfilePhoto(HttpServletResponse response, @PathVariable String profileId, @PathVariable String column, @PathVariable String random) throws IOException, ContentDeletedException 
{ 
    BufferedOutputStream bufferedOutputStream = null; 
    try { 
     //............. 

     bufferedOutputStream   = new BufferedOutputStream(response.getOutputStream()); 
     ByteBuffer   byteBuffer = imgService.getProfilePhoto(cqlSession, column, profileId); 
     if(byteBuffer==null) throw new ContentDeletedException(); 
     byte    bytes[]  = new byte[byteBuffer.remaining()]; 

     byteBuffer.get(bytes, 0, bytes.length); 
     bufferedOutputStream.write(bytes); 
    } catch (IOException ex) {ex.printStackTrace();} 
    finally{if(bufferedOutputStream!=null) bufferedOutputStream.close();} 
    throw new ContentDeletedException(); 
} 

byteBuffernull이 tomcate 로그에 ContentDeletedException 발생하지만, 브라우저 콘솔에서 여전히 200OK 반응을 보여 경우 컨트롤러

아래에 있습니다. 그래서 클라이언트 측에서 빈 이미지를 보여줍니다.

왜? 200 OK 상태를 던져서는 안됩니다.

+0

ContentDeletedException가 @ExceptionHandling 표시되지 않거나 오류에 대한 web.xml의 설정을 가지고?하지만이 발생되고있다 exeption이 ContentDeletedException'' – Koitoer

+0

당신의 try catch 블록에 던져 캐치되고있는 것 같다 바람둥이 console.'IOException' 던져하지 않습니다 – manish

답변

0

해결책을 찾았습니다. 나는이 일을하고있다 :

if(byteBuffer==null){ 
    response.sendError(HttpServletResponse.SC_NOT_FOUND); 
} 
else{ 
    byte bytes[]  = new byte[byteBuffer.remaining()];    
    byteBuffer.get(bytes, 0, bytes.length); 
    bufferedOutputStream = new BufferedOutputStream(response.getOutputStream()); 
    bufferedOutputStream.write(bytes); 
}