示例#1
0
 @Bean
 public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
   ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
   resolver.setViewResolvers(Arrays.asList(urlBasedViewResolver()));
   resolver.setDefaultViews(Arrays.asList(mappingJacksonJsonView()));
   return resolver;
 }
  @Test
  public void resolveViewNameRedirectView() throws Exception {
    request.addHeader("Accept", "application/json");
    request.setRequestURI("/test");

    StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
    webAppContext.setServletContext(new MockServletContext());
    webAppContext.refresh();

    UrlBasedViewResolver urlViewResolver = new InternalResourceViewResolver();
    urlViewResolver.setApplicationContext(webAppContext);
    ViewResolver xmlViewResolver = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(xmlViewResolver, urlViewResolver));

    View xmlView = createMock("application_xml", View.class);
    View jsonView = createMock("application_json", View.class);
    viewResolver.setDefaultViews(Arrays.asList(jsonView));

    String viewName = "redirect:anotherTest";
    Locale locale = Locale.ENGLISH;

    expect(xmlViewResolver.resolveViewName(viewName, locale)).andReturn(xmlView);
    expect(jsonView.getContentType()).andReturn("application/json").anyTimes();

    replay(xmlViewResolver, xmlView, jsonView);

    View actualView = viewResolver.resolveViewName(viewName, locale);
    assertEquals("Invalid view", RedirectView.class, actualView.getClass());

    verify(xmlViewResolver, xmlView, jsonView);
  }
  @Test
  public void resolveViewNameAcceptHeaderDefaultView() throws Exception {
    request.addHeader("Accept", "application/json");

    ViewResolver viewResolverMock1 = createMock(ViewResolver.class);
    ViewResolver viewResolverMock2 = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

    View viewMock1 = createMock("application_xml", View.class);
    View viewMock2 = createMock("text_html", View.class);
    View viewMock3 = createMock("application_json", View.class);

    List<View> defaultViews = new ArrayList<View>();
    defaultViews.add(viewMock3);
    viewResolver.setDefaultViews(defaultViews);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
    expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
    expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
    expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();
    expect(viewMock3.getContentType()).andReturn("application/json").anyTimes();

    replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock3, result);

    verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);
  }
  @Bean
  public ContentNegotiatingViewResolver viewResolver() {

    ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();

    // Used strategy only for JSON
    FixedContentNegotiationStrategy fixedStrategy =
        new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON);
    Collection<ContentNegotiationStrategy> strategies = new ArrayList<ContentNegotiationStrategy>();
    strategies.add(fixedStrategy);
    resolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));

    // Handle formatting of dates and JSON root elements
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
    mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.registerModule(new JodaModule());

    ArrayList<View> defaultViews = new ArrayList<View>();

    MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
    jsonView.setObjectMapper(mapper);
    jsonView.setExtractValueFromSingleKeyModel(true);

    defaultViews.add(jsonView);
    resolver.setDefaultViews(defaultViews);

    resolver.setOrder(2);

    return resolver;
  }
  @Bean
  public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    final ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
    final List<View> defaultViews = new ArrayList<View>();

    defaultViews.add(jsonView());
    resolver.setDefaultViews(defaultViews);
    resolver.setUseNotAcceptableStatusCode(true);
    resolver.setOrder(1);
    return resolver;
  }
示例#6
0
 @Bean
 public ContentNegotiatingViewResolver contentNegotation() {
   ContentNegotiatingViewResolver contentNegotiator = new ContentNegotiatingViewResolver();
   Map<String, String> mapContentType = new HashMap<String, String>();
   java.util.List<View> lstViews = new ArrayList<View>();
   mapContentType.put("html", "text/html");
   mapContentType.put("json", "application/json");
   contentNegotiator.setMediaTypes(mapContentType);
   lstViews.add(new MappingJacksonJsonView());
   contentNegotiator.setDefaultViews(lstViews);
   return contentNegotiator;
 }
 @Bean
 public ViewResolver contentNegotiatingViewResolver() {
   ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
   viewResolver.setOrder(0);
   Map<String, String> mediaTypes = new HashMap<String, String>();
   mediaTypes.put("json", "application/json");
   viewResolver.setMediaTypes(mediaTypes);
   List<View> defaultViews = new ArrayList<View>();
   defaultViews.add(new MappingJacksonJsonView());
   viewResolver.setDefaultViews(defaultViews);
   return viewResolver;
 }
示例#8
0
 @Bean
 public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
   ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
   Map<String, String> mediaTypes = new HashMap<String, String>();
   mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
   result.setMediaTypes(mediaTypes);
   MappingJacksonJsonView jacksonView = new MappingJacksonJsonView();
   jacksonView.setExtractValueFromSingleKeyModel(true);
   Set<String> modelKeys = new HashSet<String>();
   modelKeys.add("events");
   modelKeys.add("event");
   jacksonView.setModelKeys(modelKeys);
   result.setDefaultViews(Collections.singletonList((View) jacksonView));
   return result;
 }
  @Bean
  public ContentNegotiatingViewResolver contentViewResolver() throws Exception {
    ContentNegotiationManagerFactoryBean contentNegotiationManager =
        new ContentNegotiationManagerFactoryBean();
    contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON);

    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");

    MappingJackson2JsonView defaultView = new MappingJackson2JsonView();
    defaultView.setExtractValueFromSingleKeyModel(true);

    ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver();
    contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject());
    contentViewResolver.setViewResolvers(Arrays.<ViewResolver>asList(viewResolver));
    contentViewResolver.setDefaultViews(Arrays.<View>asList(defaultView));
    return contentViewResolver;
  }
  @Test
  public void resolveViewNameFilenameDefaultView() throws Exception {
    request.setRequestURI("/test.json");

    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", "application/json");
    viewResolver.setMediaTypes(mediaTypes);

    ViewResolver viewResolverMock1 = createMock(ViewResolver.class);
    ViewResolver viewResolverMock2 = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

    View viewMock1 = createMock("application_xml", View.class);
    View viewMock2 = createMock("text_html", View.class);
    View viewMock3 = createMock("application_json", View.class);

    List<View> defaultViews = new ArrayList<View>();
    defaultViews.add(viewMock3);
    viewResolver.setDefaultViews(defaultViews);

    String viewName = "view";
    Locale locale = Locale.ENGLISH;

    expect(viewResolverMock1.resolveViewName(viewName, locale)).andReturn(viewMock1);
    expect(viewResolverMock1.resolveViewName(viewName + ".json", locale)).andReturn(null);
    expect(viewResolverMock2.resolveViewName(viewName, locale)).andReturn(viewMock2);
    expect(viewResolverMock2.resolveViewName(viewName + ".json", locale)).andReturn(null);
    expect(viewMock1.getContentType()).andReturn("application/xml").anyTimes();
    expect(viewMock2.getContentType()).andReturn("text/html;charset=ISO-8859-1").anyTimes();
    expect(viewMock3.getContentType()).andReturn("application/json").anyTimes();

    replay(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);

    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock3, result);

    verify(viewResolverMock1, viewResolverMock2, viewMock1, viewMock2, viewMock3);
  }