Search in sources :

Example 1 with ContentNegotiatingViewResolver

use of cn.taketoday.web.view.ContentNegotiatingViewResolver in project today-infrastructure by TAKETODAY.

the class ViewResolverRegistryTests method contentNegotiation.

@Test
public void contentNegotiation() {
    MappingJackson2JsonView view = new MappingJackson2JsonView();
    this.registry.enableContentNegotiation(view);
    ContentNegotiatingViewResolver resolver = checkAndGetResolver(ContentNegotiatingViewResolver.class);
    assertThat(resolver.getDefaultViews()).isEqualTo(Arrays.asList(view));
    assertThat(this.registry.getOrder()).isEqualTo(Ordered.HIGHEST_PRECEDENCE);
}
Also used : ContentNegotiatingViewResolver(cn.taketoday.web.view.ContentNegotiatingViewResolver) MappingJackson2JsonView(cn.taketoday.web.view.json.MappingJackson2JsonView) Test(org.junit.jupiter.api.Test)

Example 2 with ContentNegotiatingViewResolver

use of cn.taketoday.web.view.ContentNegotiatingViewResolver in project today-infrastructure by TAKETODAY.

the class WebMvcAutoConfiguration method viewResolver.

@Component
@ConditionalOnBean(ViewResolver.class)
@ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class)
public ContentNegotiatingViewResolver viewResolver(@Nullable ContentNegotiationManager contentNegotiationManager) {
    ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
    resolver.setContentNegotiationManager(contentNegotiationManager);
    // ContentNegotiatingViewResolver uses all the other view resolvers to locate
    // a view so it should have a high precedence
    resolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
    return resolver;
}
Also used : ContentNegotiatingViewResolver(cn.taketoday.web.view.ContentNegotiatingViewResolver) ConditionalOnBean(cn.taketoday.context.condition.ConditionalOnBean) ConditionalOnMissingBean(cn.taketoday.context.condition.ConditionalOnMissingBean) Component(cn.taketoday.lang.Component)

Example 3 with ContentNegotiatingViewResolver

use of cn.taketoday.web.view.ContentNegotiatingViewResolver in project today-infrastructure by TAKETODAY.

the class ViewResolverRegistry method enableContentNegotiation.

/**
 * 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);
}
Also used : ContentNegotiatingViewResolver(cn.taketoday.web.view.ContentNegotiatingViewResolver)

Example 4 with ContentNegotiatingViewResolver

use of cn.taketoday.web.view.ContentNegotiatingViewResolver in project today-infrastructure by TAKETODAY.

the class ViewResolverRegistry method initContentNegotiatingViewResolver.

private ContentNegotiatingViewResolver initContentNegotiatingViewResolver(View[] defaultViews) {
    // ContentNegotiatingResolver in the registry: elevate its precedence!
    this.order = order != null ? order : Ordered.HIGHEST_PRECEDENCE;
    if (contentNegotiatingResolver != null) {
        if (ObjectUtils.isNotEmpty(defaultViews) && CollectionUtils.isNotEmpty(contentNegotiatingResolver.getDefaultViews())) {
            ArrayList<View> views = new ArrayList<>(contentNegotiatingResolver.getDefaultViews());
            CollectionUtils.addAll(views, defaultViews);
            contentNegotiatingResolver.setDefaultViews(views);
        }
    } else {
        this.contentNegotiatingResolver = new ContentNegotiatingViewResolver();
        contentNegotiatingResolver.setDefaultViews(Arrays.asList(defaultViews));
        contentNegotiatingResolver.setViewResolvers(viewResolvers);
        if (contentNegotiationManager != null) {
            contentNegotiatingResolver.setContentNegotiationManager(contentNegotiationManager);
        }
    }
    return contentNegotiatingResolver;
}
Also used : ArrayList(java.util.ArrayList) ContentNegotiatingViewResolver(cn.taketoday.web.view.ContentNegotiatingViewResolver) View(cn.taketoday.web.view.View)

Example 5 with ContentNegotiatingViewResolver

use of cn.taketoday.web.view.ContentNegotiatingViewResolver in project today-framework by TAKETODAY.

the class ViewResolverRegistry method initContentNegotiatingViewResolver.

private ContentNegotiatingViewResolver initContentNegotiatingViewResolver(View[] defaultViews) {
    // ContentNegotiatingResolver in the registry: elevate its precedence!
    this.order = order != null ? order : Ordered.HIGHEST_PRECEDENCE;
    if (contentNegotiatingResolver != null) {
        if (ObjectUtils.isNotEmpty(defaultViews) && CollectionUtils.isNotEmpty(contentNegotiatingResolver.getDefaultViews())) {
            ArrayList<View> views = new ArrayList<>(contentNegotiatingResolver.getDefaultViews());
            CollectionUtils.addAll(views, defaultViews);
            contentNegotiatingResolver.setDefaultViews(views);
        }
    } else {
        this.contentNegotiatingResolver = new ContentNegotiatingViewResolver();
        contentNegotiatingResolver.setDefaultViews(Arrays.asList(defaultViews));
        contentNegotiatingResolver.setViewResolvers(viewResolvers);
        if (contentNegotiationManager != null) {
            contentNegotiatingResolver.setContentNegotiationManager(contentNegotiationManager);
        }
    }
    return contentNegotiatingResolver;
}
Also used : ArrayList(java.util.ArrayList) ContentNegotiatingViewResolver(cn.taketoday.web.view.ContentNegotiatingViewResolver) View(cn.taketoday.web.view.View)

Aggregations

ContentNegotiatingViewResolver (cn.taketoday.web.view.ContentNegotiatingViewResolver)8 ConditionalOnBean (cn.taketoday.context.condition.ConditionalOnBean)2 ConditionalOnMissingBean (cn.taketoday.context.condition.ConditionalOnMissingBean)2 Component (cn.taketoday.lang.Component)2 View (cn.taketoday.web.view.View)2 MappingJackson2JsonView (cn.taketoday.web.view.json.MappingJackson2JsonView)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2