Search in sources :

Example 1 with MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView 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 MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView in project cas by apereo.

the class OidcCallbackAuthorizeViewResolver method resolve.

@Override
public ModelAndView resolve(final J2EContext ctx, final ProfileManager manager, final String url) {
    final Set<String> prompt = authorizationRequestSupport.getOidcPromptFromAuthorizationRequest(url);
    if (prompt.contains(OidcConstants.PROMPT_NONE)) {
        if (manager.get(true) != null) {
            return new ModelAndView(url);
        }
        final Map<String, String> model = new HashMap<>();
        model.put(OAuth20Constants.ERROR, OidcConstants.LOGIN_REQUIRED);
        return new ModelAndView(new MappingJackson2JsonView(), model);
    }
    return new ModelAndView(new RedirectView(url));
}
Also used : HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView)

Example 3 with MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView in project arch-playground by BeneStem.

the class ExceptionHandlerHtmlControllerAdvice method handleConstraintViolation.

@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView handleConstraintViolation(final ConstraintViolationException exception) {
    final MappingJackson2JsonView view = new CustomMappingJackson2JsonView();
    final var modelAndView = new ModelAndView(view);
    modelAndView.addObject(exception.getConstraintViolations().stream().map(constraintViolation -> new ValidationError(((PathImpl) constraintViolation.getPropertyPath()).getLeafNode().getName(), constraintViolation.getMessage())).collect(toList()));
    return modelAndView;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ValidationError(com.breuninger.arch.playground.common.domain.ValidationError) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 4 with MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView in project spring-framework by spring-projects.

the class ViewResolutionTests method jsonOnly.

@Test
void jsonOnly() {
    WebTestClient testClient = MockMvcWebTestClient.bindToController(new PersonController()).singleView(new MappingJackson2JsonView()).build();
    testClient.get().uri("/person/Corea").exchange().expectStatus().isOk().expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON).expectBody().jsonPath("$.person.name", "Corea");
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) MockMvcWebTestClient(org.springframework.test.web.servlet.client.MockMvcWebTestClient) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) Test(org.junit.jupiter.api.Test)

Example 5 with MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView 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)

Aggregations

MappingJackson2JsonView (org.springframework.web.servlet.view.json.MappingJackson2JsonView)22 ModelAndView (org.springframework.web.servlet.ModelAndView)11 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)7 lombok.val (lombok.val)5 Test (org.junit.jupiter.api.Test)5 MarshallingView (org.springframework.web.servlet.view.xml.MarshallingView)5 ArrayList (java.util.ArrayList)4 Jaxb2Marshaller (org.springframework.oxm.jaxb.Jaxb2Marshaller)4 View (org.springframework.web.servlet.View)4 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)4 ValidationError (com.breuninger.arch.playground.common.domain.ValidationError)3 HashMap (java.util.HashMap)3 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 FixedContentNegotiationStrategy (org.springframework.web.accept.FixedContentNegotiationStrategy)3 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)3 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 LinkedHashMap (java.util.LinkedHashMap)2 Bean (org.springframework.context.annotation.Bean)2 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)2