Search in sources :

Example 1 with JAXBElement

use of jakarta.xml.bind.JAXBElement in project spring-framework by spring-projects.

the class Jaxb2MarshallerTests method testSupports.

private void testSupports() throws Exception {
    assertThat(marshaller.supports(Flights.class)).as("Jaxb2Marshaller does not support Flights class").isTrue();
    assertThat(marshaller.supports((Type) Flights.class)).as("Jaxb2Marshaller does not support Flights generic type").isTrue();
    assertThat(marshaller.supports(FlightType.class)).as("Jaxb2Marshaller supports FlightType class").isFalse();
    assertThat(marshaller.supports((Type) FlightType.class)).as("Jaxb2Marshaller supports FlightType type").isFalse();
    Method method = ObjectFactory.class.getDeclaredMethod("createFlight", FlightType.class);
    assertThat(marshaller.supports(method.getGenericReturnType())).as("Jaxb2Marshaller does not support JAXBElement<FlightsType>").isTrue();
    marshaller.setSupportJaxbElementClass(true);
    JAXBElement<FlightType> flightTypeJAXBElement = new JAXBElement<>(new QName("https://springframework.org", "flight"), FlightType.class, new FlightType());
    assertThat(marshaller.supports(flightTypeJAXBElement.getClass())).as("Jaxb2Marshaller does not support JAXBElement<FlightsType>").isTrue();
    assertThat(marshaller.supports(DummyRootElement.class)).as("Jaxb2Marshaller supports class not in context path").isFalse();
    assertThat(marshaller.supports((Type) DummyRootElement.class)).as("Jaxb2Marshaller supports type not in context path").isFalse();
    method = getClass().getDeclaredMethod("createDummyRootElement");
    assertThat(marshaller.supports(method.getGenericReturnType())).as("Jaxb2Marshaller supports JAXBElement not in context path").isFalse();
    assertThat(marshaller.supports(DummyType.class)).as("Jaxb2Marshaller supports class not in context path").isFalse();
    assertThat(marshaller.supports((Type) DummyType.class)).as("Jaxb2Marshaller supports type not in context path").isFalse();
    method = getClass().getDeclaredMethod("createDummyType");
    assertThat(marshaller.supports(method.getGenericReturnType())).as("Jaxb2Marshaller supports JAXBElement not in context path").isFalse();
    testSupportsPrimitives();
    testSupportsStandardClasses();
}
Also used : XmlType(jakarta.xml.bind.annotation.XmlType) Type(java.lang.reflect.Type) FlightType(org.springframework.oxm.jaxb.test.FlightType) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Flights(org.springframework.oxm.jaxb.test.Flights) QName(javax.xml.namespace.QName) FlightType(org.springframework.oxm.jaxb.test.FlightType) Method(java.lang.reflect.Method) JAXBElement(jakarta.xml.bind.JAXBElement)

Example 2 with JAXBElement

use of jakarta.xml.bind.JAXBElement in project jaxrs-api by eclipse-ee4j.

the class MBWCheckResource method sendJAXBElement.

@GET
@Path("jaxbelement")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendJAXBElement(@Context SseEventSink sink, @Context Sse sse) {
    try (SseEventSink s = sink) {
        JAXBElement<String> element = new JAXBElement<String>(new QName("name"), String.class, MESSAGE);
        s.send(sse.newEventBuilder().data(element).mediaType(MediaType.APPLICATION_XML_TYPE).build());
    }
}
Also used : QName(javax.xml.namespace.QName) SseEventSink(jakarta.ws.rs.sse.SseEventSink) JAXBElement(jakarta.xml.bind.JAXBElement) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 3 with JAXBElement

use of jakarta.xml.bind.JAXBElement in project jaxrs-api by eclipse-ee4j.

the class MediaTypeResource method sendJAXB.

@GET
@Path("jaxb")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void sendJAXB(@Context SseEventSink sink, @Context Sse sse) {
    try (SseEventSink s = sink) {
        JAXBElement<String> element = new JAXBElement<String>(new QName("name"), String.class, SSEMessage.MESSAGE);
        s.send(sse.newEventBuilder().data(element).mediaType(mediaType).build());
    }
}
Also used : QName(javax.xml.namespace.QName) SseEventSink(jakarta.ws.rs.sse.SseEventSink) JAXBElement(jakarta.xml.bind.JAXBElement) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 4 with JAXBElement

use of jakarta.xml.bind.JAXBElement in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method getJaxbToken.

private static String getJaxbToken() {
    JAXBElement<String> element = new JAXBElement<String>(new QName("content"), String.class, plaincontent);
    try {
        JAXBContext context = JAXBContext.newInstance(String.class);
        java.io.StringWriter writer = new java.io.StringWriter();
        context.createMarshaller().marshal(element, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}
Also used : QName(javax.xml.namespace.QName) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) JAXBElement(jakarta.xml.bind.JAXBElement)

Example 5 with JAXBElement

use of jakarta.xml.bind.JAXBElement in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method jaxbWriterClientInterceptorTest.

/*
   * @testName: jaxbWriterClientInterceptorTest
   * 
   * @assertion_ids: JAXRS:SPEC:84;
   * 
   * @test_Strategy: JAX-RS implementations are REQUIRED to call registered
   * interceptors when mapping representations to Java types and vice versa.
   */
@Test
@Tag("xml_binding")
public void jaxbWriterClientInterceptorTest() throws Fault {
    JAXBElement<String> element = new JAXBElement<String>(new QName("element"), String.class, content);
    GenericEntity<JAXBElement<String>> entity = new GenericEntity<JAXBElement<String>>(element) {
    };
    addProvider(EntityWriterInterceptor.class);
    addInterceptors(EntityWriterInterceptor.class);
    setRequestContentEntity(entity);
    setProperty(Property.REQUEST, buildRequest(Request.POST, "poststring"));
    setProperty(Property.SEARCH_STRING, EntityWriterInterceptor.class.getName());
    setProperty(Property.REQUEST_HEADERS, buildContentType(MediaType.TEXT_XML_TYPE));
    setProperty(Property.UNEXPECTED_RESPONSE_MATCH, Resource.DIRECTION);
    invoke();
    logMsg("JAXRS called registered writer interceptor for JAXBElement provider");
}
Also used : QName(javax.xml.namespace.QName) GenericEntity(jakarta.ws.rs.core.GenericEntity) JAXBElement(jakarta.xml.bind.JAXBElement) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Aggregations

JAXBElement (jakarta.xml.bind.JAXBElement)16 QName (javax.xml.namespace.QName)11 Test (org.junit.jupiter.api.Test)6 GET (jakarta.ws.rs.GET)3 Path (jakarta.ws.rs.Path)3 StringReader (java.io.StringReader)3 StreamSource (javax.xml.transform.stream.StreamSource)3 Tag (org.junit.jupiter.api.Tag)3 FileDataSource (jakarta.activation.FileDataSource)2 Produces (jakarta.ws.rs.Produces)2 GenericEntity (jakarta.ws.rs.core.GenericEntity)2 SseEventSink (jakarta.ws.rs.sse.SseEventSink)2 JAXBContext (jakarta.xml.bind.JAXBContext)2 JAXBException (jakarta.xml.bind.JAXBException)2 Unmarshaller (jakarta.xml.bind.Unmarshaller)2 Source (javax.xml.transform.Source)2 FlightType (org.springframework.oxm.jaxb.test.FlightType)2 XmlAnyType (xades4j.xml.bind.xades.XmlAnyType)2 XmlSigPolicyQualifiersListType (xades4j.xml.bind.xades.XmlSigPolicyQualifiersListType)2 DataHandler (jakarta.activation.DataHandler)1