Search in sources :

Example 1 with Jaxb2Marshaller

use of org.springframework.oxm.jaxb.Jaxb2Marshaller in project spring-framework by spring-projects.

the class ViewResolutionTests method testXmlOnly.

@Test
public void testXmlOnly() throws Exception {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(Person.class);
    standaloneSetup(new PersonController()).setSingleView(new MarshallingView(marshaller)).build().perform(get("/person/Corea")).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_XML)).andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
Also used : MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) Test(org.junit.Test)

Example 2 with Jaxb2Marshaller

use of org.springframework.oxm.jaxb.Jaxb2Marshaller 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 3 with Jaxb2Marshaller

use of org.springframework.oxm.jaxb.Jaxb2Marshaller in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method responseBodyArgMismatch.

@Test
public void responseBodyArgMismatch() throws ServletException, IOException {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
            marshaller.setClassesToBeBound(A.class, B.class);
            try {
                marshaller.afterPropertiesSet();
            } catch (Exception ex) {
                throw new BeanCreationException(ex.getMessage(), ex);
            }
            MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller);
            RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
            adapterDef.getPropertyValues().add("messageConverters", messageConverter);
            wac.registerBeanDefinition("handlerAdapter", adapterDef);
        }
    }, RequestBodyArgMismatchController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "<b/>";
    request.setContent(requestBody.getBytes("UTF-8"));
    request.addHeader("Content-Type", "application/xml; charset=utf-8");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(400, response.getStatus());
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MarshallingHttpMessageConverter(org.springframework.http.converter.xml.MarshallingHttpMessageConverter) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with Jaxb2Marshaller

use of org.springframework.oxm.jaxb.Jaxb2Marshaller in project powerauth-restful-integration by lime-company.

the class PowerAuthWebServiceConfiguration method marshaller.

/**
 * Marshaller for PowerAuth SOAP service communication.
 * @return JAXB marshaller with correctly configured context path.
 */
@Bean
public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setContextPath("io.getlime.powerauth.soap");
    return marshaller;
}
Also used : Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) Bean(org.springframework.context.annotation.Bean)

Example 5 with Jaxb2Marshaller

use of org.springframework.oxm.jaxb.Jaxb2Marshaller in project sic by belluccifranco.

the class App method marshaller.

@Bean
public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    // this package must match the package in the <generatePackage> specified in pom.xml
    marshaller.setContextPaths("afip.wsaa.wsdl", "afip.wsfe.wsdl");
    return marshaller;
}
Also used : Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

Jaxb2Marshaller (org.springframework.oxm.jaxb.Jaxb2Marshaller)35 Bean (org.springframework.context.annotation.Bean)13 Test (org.junit.Test)7 MarshallingView (org.springframework.web.servlet.view.xml.MarshallingView)7 Test (org.junit.jupiter.api.Test)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 View (org.springframework.web.servlet.View)4 MappingJackson2JsonView (org.springframework.web.servlet.view.json.MappingJackson2JsonView)4 SimpleXml (com.hack23.cia.service.external.common.impl.test.SimpleXml)3 IOException (java.io.IOException)3 FilterRegistrationBean (org.springframework.boot.web.servlet.FilterRegistrationBean)3 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 FixedContentNegotiationStrategy (org.springframework.web.accept.FixedContentNegotiationStrategy)3 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)3 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)3 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)3 InputStream (java.io.InputStream)2 URISyntaxException (java.net.URISyntaxException)2 lombok.val (lombok.val)2