Search in sources :

Example 1 with FixedContentNegotiationStrategy

use of org.springframework.web.accept.FixedContentNegotiationStrategy in project spring-framework by spring-projects.

the class ViewResolutionTests method testContentNegotiation.

@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")));
}
Also used : ArrayList(java.util.ArrayList) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) View(org.springframework.web.servlet.View) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MockMvc(org.springframework.test.web.servlet.MockMvc) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.Test)

Example 2 with FixedContentNegotiationStrategy

use of org.springframework.web.accept.FixedContentNegotiationStrategy in project dhis2-core by dhis2.

the class MvcTestConfig method requestMappingHandlerMapping.

@Bean
public CustomRequestMappingHandlerMapping requestMappingHandlerMapping(FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
    CustomPathExtensionContentNegotiationStrategy pathExtensionNegotiationStrategy = new CustomPathExtensionContentNegotiationStrategy(mediaTypeMap);
    pathExtensionNegotiationStrategy.setUseRegisteredExtensionsOnly(true);
    ContentNegotiationManager manager = new ContentNegotiationManager(Arrays.asList(pathExtensionNegotiationStrategy, new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON)));
    CustomRequestMappingHandlerMapping mapping = new CustomRequestMappingHandlerMapping();
    mapping.setOrder(0);
    mapping.setContentNegotiationManager(manager);
    TestInterceptorRegistry registry = new TestInterceptorRegistry();
    addInterceptors(registry);
    registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService));
    registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider));
    mapping.setInterceptors(registry.getInterceptors().toArray());
    return mapping;
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) CustomRequestMappingHandlerMapping(org.hisp.dhis.webapi.mvc.CustomRequestMappingHandlerMapping) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) CustomPathExtensionContentNegotiationStrategy(org.hisp.dhis.webapi.view.CustomPathExtensionContentNegotiationStrategy) Bean(org.springframework.context.annotation.Bean)

Example 3 with FixedContentNegotiationStrategy

use of org.springframework.web.accept.FixedContentNegotiationStrategy in project dhis2-core by dhis2.

the class WebMvcConfig method mvcContentNegotiationManager.

@Primary
@Bean
@Override
public ContentNegotiationManager mvcContentNegotiationManager() {
    CustomPathExtensionContentNegotiationStrategy pathExtensionNegotiationStrategy = new CustomPathExtensionContentNegotiationStrategy(mediaTypeMap);
    pathExtensionNegotiationStrategy.setUseRegisteredExtensionsOnly(true);
    return new ContentNegotiationManager(Arrays.asList(pathExtensionNegotiationStrategy, new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON)));
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) CustomPathExtensionContentNegotiationStrategy(org.hisp.dhis.webapi.view.CustomPathExtensionContentNegotiationStrategy) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 4 with FixedContentNegotiationStrategy

use of org.springframework.web.accept.FixedContentNegotiationStrategy in project spring-framework by spring-projects.

the class ViewResolutionTests method contentNegotiation.

@Test
void contentNegotiation() 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();
    WebTestClient testClient = MockMvcWebTestClient.bindToController(new PersonController()).viewResolvers(cnViewResolver, new InternalResourceViewResolver()).build();
    EntityExchangeResult<Void> result = testClient.get().uri("/person/Corea").exchange().expectStatus().isOk().expectBody().isEmpty();
    // Further assertions on the server response
    MockMvcWebTestClient.resultActionsFor(result).andExpect(model().size(1)).andExpect(model().attributeExists("person")).andExpect(forwardedUrl("person/show"));
    testClient.get().uri("/person/Corea").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk().expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON).expectBody().jsonPath("$.person.name", "Corea");
    testClient.get().uri("/person/Corea").accept(MediaType.APPLICATION_XML).exchange().expectStatus().isOk().expectHeader().contentType(MediaType.APPLICATION_XML).expectBody().xpath("/person/name/text()").isEqualTo("Corea");
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) MockMvcWebTestClient(org.springframework.test.web.servlet.client.MockMvcWebTestClient) ArrayList(java.util.ArrayList) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) View(org.springframework.web.servlet.View) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 5 with FixedContentNegotiationStrategy

use of org.springframework.web.accept.FixedContentNegotiationStrategy in project spring-framework by spring-projects.

the class ProducesRequestConditionTests method matchAndCompare.

// gh-22853
@Test
public void matchAndCompare() {
    ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
    ProducesRequestCondition none = new ProducesRequestCondition(new String[0], null, manager);
    ProducesRequestCondition html = new ProducesRequestCondition(new String[] { "text/html" }, null, manager);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addHeader("Accept", "*/*");
    ProducesRequestCondition noneMatch = none.getMatchingCondition(request);
    ProducesRequestCondition htmlMatch = html.getMatchingCondition(request);
    assertThat(noneMatch.compareTo(htmlMatch, request)).isEqualTo(1);
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) Test(org.junit.jupiter.api.Test)

Aggregations

ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)9 FixedContentNegotiationStrategy (org.springframework.web.accept.FixedContentNegotiationStrategy)9 Test (org.junit.jupiter.api.Test)6 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)6 View (org.springframework.web.servlet.View)5 ArrayList (java.util.ArrayList)3 Jaxb2Marshaller (org.springframework.oxm.jaxb.Jaxb2Marshaller)3 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)3 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)3 MappingJackson2JsonView (org.springframework.web.servlet.view.json.MappingJackson2JsonView)3 MarshallingView (org.springframework.web.servlet.view.xml.MarshallingView)3 Locale (java.util.Locale)2 CustomPathExtensionContentNegotiationStrategy (org.hisp.dhis.webapi.view.CustomPathExtensionContentNegotiationStrategy)2 Bean (org.springframework.context.annotation.Bean)2 MockMvc (org.springframework.test.web.servlet.MockMvc)2 CustomRequestMappingHandlerMapping (org.hisp.dhis.webapi.mvc.CustomRequestMappingHandlerMapping)1 Test (org.junit.Test)1 Primary (org.springframework.context.annotation.Primary)1 MediaType (org.springframework.http.MediaType)1 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)1