dharmendrakshl1 / rest-assured

Automatically exported from code.google.com/p/rest-assured
0 stars 0 forks source link

XML with JAXB: broken invocation of javax.xml.bind.Unmarshaller #361

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

just tried to use XML as data transfer format instead of JSON, but got a 
MissingMethodException.

What steps will reproduce the problem?
1. set request.contentType(ContentType.XML);
2. set request.header(new Header("Accept", "application/xml"));
3. call a GET method, which returns XML-formatted body

What is the expected output? What do you see instead?

groovy.lang.MissingMethodException: No signature of method: 
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal() 
is applicable for argument types: (java.io.StringReader, java.lang.Class)

What version of the product are you using? On what operating system?
2.3.4, Windows 7 64-bit JDK-1.7 and JDK-1.8

Please provide any additional information below.

XmlPathJaxbObjectDeserializer.groovy, line 47:
JAXBElement jaxbElement = unmarshaller.unmarshal(reader, cls)

Does not compile when converted to Java, since there is no such method in the 
JDK.
A potential fix might be to wrap the StringReader in a StreamSource like this:

JAXBElement jaxbElement = unmarshaller.unmarshal(new 
javax.xml.transform.stream.StreamSource(reader), cls)

PS: I don't know if this error is present on marshalling (object to XML)

Original issue reported on code.google.com by rfili...@gmail.com on 13 Oct 2014 at 6:01

Attachments:

GoogleCodeExporter commented 9 years ago
Could you please try out the latest snaphot, 2.3.5-SNAPSHOT after having 
depended on the following repo:

<repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots />
        </repository>
</repositories>

to see if it works better?

Original comment by johan.ha...@gmail.com on 14 Oct 2014 at 1:10

GoogleCodeExporter commented 9 years ago
Hi, just tried the fix, exception is no more thrown and the XML payload is 
converted into an object of the declared type, however no fields in the object 
are set, all having null value after deserialization. 

I guess this is an issue on my site, because I am using a generated .jar module 
with DTOs, which are generated by the enuncicate tool. They annotated with 
@JsonFoo annotations from Jackson, and perhaps this is not the way to go. 

Now I will try a second generated .jar module (again from enunciate), where the 
DTOs are annotated with @XmlFoo annotations from JAXB. The goal is to be able 
to use both XML and JSON as a transfer format, with the same set of DTOs 
(either with @JsonFoo or @XmlFoo annotations). Am I guessing right, that the 
JAXB annotations are the right way to go for this scenario?

Original comment by rfili...@gmail.com on 16 Oct 2014 at 9:23

GoogleCodeExporter commented 9 years ago
I was able to serialize both JSON and XML using JAXB-annotated classes in 
combination with the Jackson 1.9.3 serializer. The trick is to register another 
AnnotationIntrospector, which is capable of inspecting JAXB annotations. 

    ObjectMapperConfig objectMapperConfig = ObjectMapperConfig.objectMapperConfig()                                                              .jackson1ObjectMapperFactory(new JaxbObjectMapperFactory());
    RestAssured.config = RestAssured.config()
                                    .objectMapperConfig(objectMapperConfig);

public class JaxbObjectMapperFactory implements Jackson1ObjectMapperFactory {

  @Override
  public ObjectMapper create(Class cls, String charset) {
    ObjectMapper mapper = new ObjectMapper();
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    mapper.setDeserializationConfig(mapper.getDeserializationConfig()
                                          .withAnnotationIntrospector(introspector));
    mapper.setSerializationConfig(mapper.getSerializationConfig().withAnnotationIntrospector(introspector));
    return mapper;
  }
}

More information can be found on the Jackson website 
http://wiki.fasterxml.com/JacksonJAXBAnnotations

I will try out more complex scenarios in the next days and give feedback if any 
issues arise.

Original comment by rfili...@gmail.com on 16 Oct 2014 at 1:20

GoogleCodeExporter commented 9 years ago
Thanks a lot for helping out and investigating. Tell me if there's anything we 
should do on the REST Assured side to make it simpler..

Original comment by johan.ha...@gmail.com on 16 Oct 2014 at 2:11

GoogleCodeExporter commented 9 years ago
we are still using 2.3.4. Is there a work around until we get a latest version?

Original comment by c.k.chai...@gmail.com on 10 Nov 2014 at 9:05

GoogleCodeExporter commented 9 years ago
rfilipov just mentioned the workaround. No changes have been made in REST 
Assured.

Original comment by johan.ha...@gmail.com on 11 Nov 2014 at 6:10