/** * Enable use of a {@link ContentNegotiatingViewResolver} to front all other * configured view resolvers and select among all selected Views based on * media types requested by the client (e.g. in the Accept header). * <p>If invoked multiple times the provided default views will be added to * any other default views that may have been configured already. * @see ContentNegotiatingViewResolver#setDefaultViews */ public void enableContentNegotiation(boolean useNotAcceptableStatus, View... defaultViews) { ContentNegotiatingViewResolver vr = initContentNegotiatingViewResolver(defaultViews); vr.setUseNotAcceptableStatusCode(useNotAcceptableStatus); }
/** * Enable use of a {@link ContentNegotiatingViewResolver} to front all other * configured view resolvers and select among all selected Views based on * media types requested by the client (e.g. in the Accept header). * <p>If invoked multiple times the provided default views will be added to * any other default views that may have been configured already. * @see ContentNegotiatingViewResolver#setDefaultViews */ public void enableContentNegotiation(boolean useNotAcceptableStatus, View... defaultViews) { ContentNegotiatingViewResolver vr = initContentNegotiatingViewResolver(defaultViews); vr.setUseNotAcceptableStatusCode(useNotAcceptableStatus); }
@Test public void resolveViewNoMatchUseUnacceptableStatus() throws Exception { viewResolver.setUseNotAcceptableStatusCode(true); request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9"); ViewResolver viewResolverMock = mock(ViewResolver.class); viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock)); viewResolver.afterPropertiesSet(); View viewMock = mock(View.class, "application_xml"); String viewName = "view"; Locale locale = Locale.ENGLISH; given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(viewMock); given(viewMock.getContentType()).willReturn("application/pdf"); View result = viewResolver.resolveViewName(viewName, locale); assertNotNull("Invalid view", result); MockHttpServletResponse response = new MockHttpServletResponse(); result.render(null, request, response); assertEquals("Invalid status code set", 406, response.getStatus()); }
/** * Enable use of a {@link ContentNegotiatingViewResolver} to front all other * configured view resolvers and select among all selected Views based on * media types requested by the client (e.g. in the Accept header). * <p>If invoked multiple times the provided default views will be added to * any other default views that may have been configured already. * @see ContentNegotiatingViewResolver#setDefaultViews */ public void enableContentNegotiation(boolean useNotAcceptableStatus, View... defaultViews) { initContentNegotiatingViewResolver(defaultViews); this.contentNegotiatingResolver.setUseNotAcceptableStatusCode(useNotAcceptableStatus); }