2011-03-25 5 views
3

나는 파일 (이미지, PDF 파일 등 ,.) 역할을하는 컨트롤러가 있습니다스프링 MVC에서 Accept 헤더를 무시

: 내가 가진 파일을 요청하는 경우

@Controller 
public class FileController { 

    @ResponseBody 
    @RequestMapping("/{filename}") 
    public Object download(@PathVariable String filename) throws Exception { 
     returns MyFile.findFile(filename); 
    } 

} 

을 내가 406를 얻을 Accept 헤더를 다음

: 나는 Accept 헤더를 다음과 같은 파일을 요청하는 경우
Request  
URL: http://localhost:8080/files/thmb_AA039258_204255d0.png 
Request Method:GET 
Status Code:406 Not Acceptable 
Request Headers 
Accept:*/* 

나는 200 얻을

URL: http://localhost:8080/files/thmb_AA039258_204255d0.png 
Request Method: GET 
Status Code:200 OK 
Request Headers 
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 

이 내 스프링 MVC 컨텍스트에서 유일하게 뷰 리졸버 :

<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/> 
</bean> 

가 수락 헤더를 무시하는 스프링 MVC를 구성 할 수 어쨌든 있나요? ContentNegotiatingViewResolver를 사용하여이 작업을 수행하는 예제를 보았지만 xml 및 json을 처리하는 데만 사용되었습니다.

+0

이것은 매우 비슷한 질문이지만 json의 경우 : http://stackoverflow.com/questions/5315288/spring-returning-json-with-responsebody-when-the-accept-header-is-throws-htt –

답변

3

그래서 내가 그것을 작동하려면와 종료 코드입니다 (적어도 4.1.5에서) 경로 기반 컨텐트 협상을 선호합니다 (즉, URL이 ".xml"로 끝나면 XML 등을 반환하려고 시도합니다).

0

ResponseBody 주석을 사용하면 Accept 헤더를보고 일부 매핑을 시도하는 거래의 일부라고 생각합니다. 그 주석으로 어떻게해야하는지 알 수 없다면 응답을 보내는 다른 많은 방법이 있습니다.

@Controller 
public class FileController { 

    @ResponseBody 
    @RequestMapping("/{filename}") 
    public void download(@PathVariable String filename, ServletResponse response) throws Exception { 
     MyFile file = MyFile.find(filename); 
     response.setContentType(file.getContentType()); 
     response.getOutputStream().write(file.getBytes()); 

    } 


} 
0

내가 JSON 응답 형식으로 잠금이 사용 :

@Configuration 
@EnableWebMvc 
public class ApplicationConfiguration extends WebMvcConfigurerAdapter { 

    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { 

     configurer.favorPathExtension(false); 
     configurer.ignoreAcceptHeader(true); 
     configurer.defaultContentType(MediaType.APPLICATION_JSON); 

    } 

} 

favorPathExtension(false) 봄 때문에 기본적으로 필요한

관련 문제