Search in sources :

Example 21 with Jaxb2Marshaller

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

the class ServletAnnotationControllerHandlerMethodTests method responseBodyArgMismatch.

@PathPatternsParameterizedTest
void responseBodyArgMismatch(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(RequestBodyArgMismatchController.class, usePathPatterns, 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);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "<b/>";
    request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
    request.addHeader("Content-Type", "application/xml; charset=utf-8");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(400);
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MarshallingHttpMessageConverter(org.springframework.http.converter.xml.MarshallingHttpMessageConverter) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) ServletException(jakarta.servlet.ServletException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 22 with Jaxb2Marshaller

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

the class MarshallingMessageConverterTests method createMarshaller.

@BeforeEach
public void createMarshaller() throws Exception {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(MyBean.class);
    marshaller.afterPropertiesSet();
    this.converter = new MarshallingMessageConverter(marshaller);
}
Also used : Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 23 with Jaxb2Marshaller

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

the class OxmNamespaceHandlerTests method jaxb2ClassesToBeBoundMarshaller.

@Test
public void jaxb2ClassesToBeBoundMarshaller() {
    Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ClassesMarshaller", Jaxb2Marshaller.class);
    assertThat(jaxb2Marshaller).isNotNull();
}
Also used : Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) Test(org.junit.jupiter.api.Test)

Example 24 with Jaxb2Marshaller

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

the class OxmNamespaceHandlerTests method jaxb2ContextPathMarshaller.

@Test
public void jaxb2ContextPathMarshaller() {
    Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ContextPathMarshaller", Jaxb2Marshaller.class);
    assertThat(jaxb2Marshaller).isNotNull();
}
Also used : Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) Test(org.junit.jupiter.api.Test)

Example 25 with Jaxb2Marshaller

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

the class ViewResolutionTests method xmlOnly.

@Test
void xmlOnly() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(Person.class);
    WebTestClient testClient = MockMvcWebTestClient.bindToController(new PersonController()).singleView(new MarshallingView(marshaller)).build();
    testClient.get().uri("/person/Corea").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) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) Test(org.junit.jupiter.api.Test)

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