@Test
  public void resolveViewNameWithRequestParameter() throws Exception {
    request.addParameter("format", "xls");

    viewResolver.setFavorParameter(true);
    viewResolver.setMediaTypes(Collections.singletonMap("xls", "application/vnd.ms-excel"));

    ViewResolver viewResolverMock = createMock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));

    View viewMock = createMock("application_xls", View.class);

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

    expect(viewResolverMock.resolveViewName(viewName, locale)).andReturn(null);
    expect(viewResolverMock.resolveViewName(viewName + ".xls", locale)).andReturn(viewMock);
    expect(viewMock.getContentType()).andReturn("application/vnd.ms-excel").anyTimes();

    replay(viewResolverMock, viewMock);

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

    verify(viewResolverMock, viewMock);
  }
 @Test
 public void getMediaTypeFromFilenameMediaTypes() {
   viewResolver.setMediaTypes(Collections.singletonMap("HTML", "application/xhtml+xml"));
   assertEquals(
       "Invalid content type",
       new MediaType("application", "xhtml+xml"),
       viewResolver.getMediaTypeFromFilename("test.html"));
 }
 @Test
 public void getMediaTypeParameter() {
   viewResolver.setFavorParameter(true);
   viewResolver.setMediaTypes(Collections.singletonMap("html", "application/xhtml+xml"));
   request.addParameter("format", "html");
   List<MediaType> result = viewResolver.getMediaTypes(request);
   assertEquals(
       "Invalid content type",
       Collections.singletonList(new MediaType("application", "xhtml+xml")),
       result);
 }
示例#4
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;
 }
 @Test
 public void getMediaTypeFilename() {
   request.setRequestURI("/test.html?foo=bar");
   List<MediaType> result = viewResolver.getMediaTypes(request);
   assertEquals(
       "Invalid content type", Collections.singletonList(new MediaType("text", "html")), result);
   viewResolver.setMediaTypes(Collections.singletonMap("html", "application/xhtml+xml"));
   result = viewResolver.getMediaTypes(request);
   assertEquals(
       "Invalid content type",
       Collections.singletonList(new MediaType("application", "xhtml+xml")),
       result);
 }
示例#7
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
  ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    if (null == viewResolver) {
      viewResolver = new ContentNegotiatingViewResolver();

      Map<String, String> mediaTypes =
          new HashMap<String, String>() {
            {
              put("json", "application/json");
              put("urilist", "text/uri-list");
            }
          };
      viewResolver.setMediaTypes(mediaTypes);

      RepositoryRestViewResolver jsonvr = new RepositoryRestViewResolver(jsonView());
      RepositoryRestViewResolver urilistvr = new RepositoryRestViewResolver(urilistView());
      viewResolver.setViewResolvers(Arrays.<ViewResolver>asList(jsonvr, urilistvr));
    }
    return viewResolver;
  }
  @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);
  }