Java Code Examples for org.springframework.web.servlet.view.ContentNegotiatingViewResolver#setDefaultViews()

The following examples show how to use org.springframework.web.servlet.view.ContentNegotiatingViewResolver#setDefaultViews() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DispatcherServletConfiguration.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Bean
public ViewResolver contentNegotiatingViewResolver() {
    log.debug("Configuring the ContentNegotiatingViewResolver");
    ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
    List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();

    UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
    urlBasedViewResolver.setViewClass(JstlView.class);
    urlBasedViewResolver.setPrefix("/WEB-INF/pages/");
    urlBasedViewResolver.setSuffix(".jsp");
    viewResolvers.add(urlBasedViewResolver);

    viewResolver.setViewResolvers(viewResolvers);

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

    return viewResolver;
}
 
Example 2
Source File: DelegatingCatnapWebMvcConfiguration.java    From catnap with Apache License 2.0 6 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    List<View> defaultViews = new ArrayList<>(2);
    defaultViews.add(jsonCatnapSpringView());
    defaultViews.add(jsonpCatnapSpringView());

    List<CatnapWrappingView> catnapViews = new ArrayList<>(2);
    catnapViews.add(jsonCatnapSpringView());
    catnapViews.add(jsonpCatnapSpringView());

    CatnapViewResolver catnapViewResolver = new CatnapViewResolver();
    catnapViewResolver.setViews(catnapViews);

    List<ViewResolver> viewResolvers = new ArrayList<>(1);
    viewResolvers.add(catnapViewResolver);

    ContentNegotiatingViewResolver cnvr = new ContentNegotiatingViewResolver();
    cnvr.setContentNegotiationManager(mvcContentNegotiationManager());
    cnvr.setOrder(1);
    cnvr.setDefaultViews(defaultViews);
    cnvr.setViewResolvers(viewResolvers);

    return cnvr;
}
 
Example 3
Source File: WidgetConfiguration.java    From catnap with Apache License 2.0 6 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    List<View> defaultViews = new ArrayList<>(2);
    defaultViews.add(jsonCatnapSpringView());
    defaultViews.add(jsonpCatnapSpringView());

    List<CatnapWrappingView> catnapViews = new ArrayList<>(2);
    catnapViews.add(jsonCatnapSpringView());
    catnapViews.add(jsonpCatnapSpringView());

    CatnapViewResolver catnapViewResolver = new CatnapViewResolver();
    catnapViewResolver.setViews(catnapViews);

    List<ViewResolver> viewResolvers = new ArrayList<>(1);
    viewResolvers.add(catnapViewResolver);

    ContentNegotiatingViewResolver cnvr = new ContentNegotiatingViewResolver();
    cnvr.setContentNegotiationManager(mvcContentNegotiationManager());
    cnvr.setOrder(1);
    cnvr.setDefaultViews(defaultViews);
    cnvr.setViewResolvers(viewResolvers);

    return cnvr;
}
 
Example 4
Source File: ViewResolutionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testContentNegotiation() throws Exception {

	Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(Person.class);

	List<View> viewList = new ArrayList<View>();
	viewList.add(new MappingJackson2JsonView());
	viewList.add(new MarshallingView(marshaller));

	ContentNegotiationManager manager = new ContentNegotiationManager(
			new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));

	ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
	cnViewResolver.setDefaultViews(viewList);
	cnViewResolver.setContentNegotiationManager(manager);
	cnViewResolver.afterPropertiesSet();

	MockMvc mockMvc =
		standaloneSetup(new PersonController())
			.setViewResolvers(cnViewResolver, new InternalResourceViewResolver())
			.build();

	mockMvc.perform(get("/person/Corea"))
		.andExpect(status().isOk())
		.andExpect(model().size(1))
		.andExpect(model().attributeExists("person"))
		.andExpect(forwardedUrl("person/show"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_JSON))
		.andExpect(jsonPath("$.person.name").value("Corea"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_XML))
		.andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
 
Example 5
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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;
}
 
Example 6
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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;
}
 
Example 7
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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;
}
 
Example 8
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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;
}
 
Example 9
Source File: ViewResolutionTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testContentNegotiation() throws Exception {
	Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(Person.class);

	List<View> viewList = new ArrayList<>();
	viewList.add(new MappingJackson2JsonView());
	viewList.add(new MarshallingView(marshaller));

	ContentNegotiationManager manager = new ContentNegotiationManager(
			new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));

	ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
	cnViewResolver.setDefaultViews(viewList);
	cnViewResolver.setContentNegotiationManager(manager);
	cnViewResolver.afterPropertiesSet();

	MockMvc mockMvc =
		standaloneSetup(new PersonController())
			.setViewResolvers(cnViewResolver, new InternalResourceViewResolver())
			.build();

	mockMvc.perform(get("/person/Corea"))
		.andExpect(status().isOk())
		.andExpect(model().size(1))
		.andExpect(model().attributeExists("person"))
		.andExpect(forwardedUrl("person/show"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_JSON))
		.andExpect(jsonPath("$.person.name").value("Corea"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_XML))
		.andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
 
Example 10
Source File: ViewResolutionTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testContentNegotiation() throws Exception {
	Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(Person.class);

	List<View> viewList = new ArrayList<>();
	viewList.add(new MappingJackson2JsonView());
	viewList.add(new MarshallingView(marshaller));

	ContentNegotiationManager manager = new ContentNegotiationManager(
			new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));

	ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
	cnViewResolver.setDefaultViews(viewList);
	cnViewResolver.setContentNegotiationManager(manager);
	cnViewResolver.afterPropertiesSet();

	MockMvc mockMvc =
		standaloneSetup(new PersonController())
			.setViewResolvers(cnViewResolver, new InternalResourceViewResolver())
			.build();

	mockMvc.perform(get("/person/Corea"))
		.andExpect(status().isOk())
		.andExpect(model().size(1))
		.andExpect(model().attributeExists("person"))
		.andExpect(forwardedUrl("person/show"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_JSON))
		.andExpect(jsonPath("$.person.name").value("Corea"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_XML))
		.andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}