users@jersey.java.net

[Jersey] Re: Client response unmarshalling question

From: Arul Dhesiaseelan <aruld_at_acm.org>
Date: Tue, 1 May 2012 16:30:39 -1000

Toss in these two classes.

@XmlRegistry
public class ObjectFactory {

    private final static QName _ArrayOfString_QNAME = new QName("
http://schemas.micrsoft.com/2003/10/Serialization/Arrays", "ArrayOfString");

    public ObjectFactory() {
    }

    public ArrayOfStringType createArrayOfStringType() {
        return new ArrayOfStringType();
    }

    @XmlElementDecl(namespace = "
http://schemas.micrsoft.com/2003/10/Serialization/Arrays", name =
"ArrayOfString")
    public JAXBElement<ArrayOfStringType>
createArrayOfString(ArrayOfStringType value) {
        return new JAXBElement<ArrayOfStringType>(_ArrayOfString_QNAME,
ArrayOfStringType.class, null, value);
    }

}


@XmlRootElement
public class ArrayOfStringType {

    @XmlElement(namespace = "
http://schemas.micrsoft.com/2003/10/Serialization/Arrays")
    protected List<String> string;

    public List<String> getString() {
        if (string == null) {
            string = new ArrayList<String>();
        }
        return this.string;
    }
}

This is all you need, I did this using intellij IDEA, pretty simple to
generate XML <-> XSD <->JAXB.

Then, your client code should be changed to this:

    ClientConfig clientConfig = new DefaultClientConfig();
    Client c = Client.create(clientConfig);
    WebResource r = c.resource(SERVICE_URL);

    GenericType<JAXBElement<ArrayOfStringType>> genericType = new
GenericType<JAXBElement<ArrayOfStringType>>() {
    };
    JAXBElement<ArrayOfStringType> resp =
r.accept(MediaType.APPLICATION_XML_TYPE).get(genericType);
    System.out.println(resp.getValue().getString().size());

This should get what you need.

-Arul

On Mon, Apr 30, 2012 at 11:31 AM, Noah White <emailnbw_at_gmail.com> wrote:

> I've a bit stumped trying to unmarshall a XML response my Jersey client is
> receiving. Here's an example:
>
> <ArrayOfString xmlns="
> http://schemas.micrsoft.com/2003/10/Serialization/Arrays" xmlns:i="
> http://www.w3c.org/2001/XMLSchema-instance">
> <string>ar</string>
> <string>bg</string>
> <string>ca</string>
> </ArrayOfString>
>
> My JAXB class looks like this:
>
> @XmlRootElement(name = "ArrayOfString", namespace="
> http://schemas.micrsoft.com/2003/10/Serialization/Arrays")
> public class LangReponseData {
>
> private List<StringResponseData> supportedLangs;
>
> public List<StringResponseData> getSupportedLangs() {
> return supportedLangs;
> }
>
> public void setSupportedLangs(List<StringResponseData> langList) {
> this.supportedLangs = langList;
> }
>
> public LangResponseData() {
> this.supportedLangs = new ArrayList<>();
> }
> }
>
> @XmlRootElement(name = "string", namespace=""
> http://schemas.micrsoft.com/2003/10/Serialization/")
> public class StringResponseData {
>
> private String _string;
>
> @XmlValue
> public String getString() {
> return _string;
> }
>
> pubic void setString(String aString) {
> _string = aString;
> }
>
> public StringResponseData() {}
>
> }
>
> My client code looks like this:
>
> ClientConfig clientConfig = new DefaultClientConfig();
> Client c = Client.create(clientConfig);
> WebResource r = c.resource(SERVICE_URL);
> LangResponseData resp =
> r.accept(MediaType.APPLICATION_XML_TYPE).get(LangResponseData.class);
>
> When I run this no exceptions are thrown and resp.supportedLangs is always
> size 0. If I change my r.accept to use String.class I can see I am getting
> the XML shown above in my example as the response.
>
> I am using Jersey client 1.11 which comes bundled with Glassfish 3.1.2 on
> JDK 1.7u3 x64.
>
> Any thoughts on this? Thanks,
>
> -Noah
>
>
>
>
>


-- 
http://aruld.info
http://twitter.com/aruld