Search in sources :

Example 16 with MappingJackson2JsonView

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

the class ExceptionHandlerHtmlControllerAdvice method handleBadRequest.

// @ExceptionHandler(RedirectionException.class)
// public RedirectView redirection(final RedirectionException exception) {
// final RedirectView loginRedirectView = new RedirectView(exception.getLocation().toString());
// loginRedirectView.setStatusCode(HttpStatus.valueOf(exception.getResponse().getStatus()));
// loginRedirectView.setExposeModelAttributes(false);
// return loginRedirectView;
// }
@ExceptionHandler(BadRequestException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView handleBadRequest(final BadRequestException exception) {
    final MappingJackson2JsonView view = new CustomMappingJackson2JsonView();
    final var modelAndView = new ModelAndView(view);
    modelAndView.addObject(exception.getErrors().getFieldErrors().stream().map(fieldError -> new ValidationError(fieldError.getField(), fieldError.getDefaultMessage())).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 17 with MappingJackson2JsonView

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

the class ExceptionHandlerHtmlControllerAdvice method handleMethodArgumentNotValid.

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
protected ModelAndView handleMethodArgumentNotValid(final MethodArgumentNotValidException exception) {
    final MappingJackson2JsonView view = new CustomMappingJackson2JsonView();
    final var modelAndView = new ModelAndView(view);
    modelAndView.addObject(exception.getBindingResult().getFieldErrors().stream().map(fieldError -> new ValidationError(fieldError.getField(), fieldError.getDefaultMessage())).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 18 with MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView in project irida by phac-nml.

the class IridaRestApiWebConfig method defaultViews.

private List<View> defaultViews() {
    List<View> views = new ArrayList<>();
    MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
    jsonView.setPrettyPrint(true);
    // add support for serializing Path data
    jsonView.getObjectMapper().registerModule(new Jdk7Module());
    views.add(jsonView);
    Jaxb2Marshaller jaxb2marshaller = new Jaxb2Marshaller();
    jaxb2marshaller.setPackagesToScan(new String[] { "ca.corefacility.bioinformatics.irida.web.assembler.resource" });
    MarshallingView marshallingView = new MarshallingView(jaxb2marshaller);
    views.add(marshallingView);
    views.add(new FastaView());
    views.add(new FastqView());
    views.add(new GenbankView());
    views.add(new NewickFileView());
    views.add(new CSVView());
    return views;
}
Also used : GenbankView(ca.corefacility.bioinformatics.irida.web.spring.view.GenbankView) FastqView(ca.corefacility.bioinformatics.irida.web.spring.view.FastqView) CSVView(ca.corefacility.bioinformatics.irida.web.spring.view.CSVView) ArrayList(java.util.ArrayList) Jdk7Module(com.fasterxml.jackson.datatype.jdk7.Jdk7Module) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) NewickFileView(ca.corefacility.bioinformatics.irida.web.spring.view.NewickFileView) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) FastaView(ca.corefacility.bioinformatics.irida.web.spring.view.FastaView) GenbankView(ca.corefacility.bioinformatics.irida.web.spring.view.GenbankView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) CSVView(ca.corefacility.bioinformatics.irida.web.spring.view.CSVView) FastqView(ca.corefacility.bioinformatics.irida.web.spring.view.FastqView) View(org.springframework.web.servlet.View) NewickFileView(ca.corefacility.bioinformatics.irida.web.spring.view.NewickFileView) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) FastaView(ca.corefacility.bioinformatics.irida.web.spring.view.FastaView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView)

Example 19 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();
    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().contentTypeCompatibleWith(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) 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) MockMvc(org.springframework.test.web.servlet.MockMvc) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 20 with MappingJackson2JsonView

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

the class ViewResolverRegistryTests method contentNegotiationAddsDefaultViewRegistrations.

@Test
public void contentNegotiationAddsDefaultViewRegistrations() {
    MappingJackson2JsonView view1 = new MappingJackson2JsonView();
    this.registry.enableContentNegotiation(view1);
    ContentNegotiatingViewResolver resolver1 = checkAndGetResolver(ContentNegotiatingViewResolver.class);
    assertThat(resolver1.getDefaultViews()).isEqualTo(Arrays.asList(view1));
    MarshallingView view2 = new MarshallingView();
    this.registry.enableContentNegotiation(view2);
    ContentNegotiatingViewResolver resolver2 = checkAndGetResolver(ContentNegotiatingViewResolver.class);
    assertThat(resolver2.getDefaultViews()).isEqualTo(Arrays.asList(view1, view2));
    assertThat(resolver2).isSameAs(resolver1);
}
Also used : ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) 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