简体   繁体   中英

Simplest Jersey Client for xml RESTful issue

I have a webresource that returns this XML:

<?xml version="1.0" encoding="utf-8"?>
<messagebody 
    xmlns="http://api.esendex.com/ns/">
    <bodytext>blablabla</bodytext>
    <characterset>blabla</characterset>
</messagebody>

I've written a simple class for that:

package com.example;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "messagebody")
public class MessageBody {
    private String xmlns;
    private String bodytext;
    private String characterset;

    public String getXmlns() {
        return xmlns;
    }

    @XmlAttribute
    public void setXmlns(String xmlns) {
        this.xmlns = xmlns;
    }

    public String getBodytext() {
        return bodytext;
    }

    @XmlElement
    public void setBodytext(String bodytext) {
        this.bodytext = bodytext;
    }

    public String getCharacterset() {
        return characterset;
    }

    @XmlElement
    public void setCharacterset(String characterset) {
        this.characterset = characterset;
    }
}

And a simple main class:

package com.example;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        try {

            Client client = Client.create();
            client.addFilter(new HTTPBasicAuthFilter("***", "****"));
            WebResource webResource = client
                    .resource("http://myresturl");


            ClientResponse response = webResource.type(MediaType.APPLICATION_XML).get(ClientResponse.class);
            if (response.getStatus() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatus());
            }

            MessageBody body = response.getEntity(MessageBody.class);
            System.out.println(body.getBodytext());


        } catch (Exception e) {

            e.printStackTrace();

        }
    }
}

I pass the authentication and I get the XML right but there's something wrong with the xml unmarshelling. I get the following exception:

javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://api.esendex.com/ns/", local:"messagebody"). Expected elements are <{}messagebody>

To my understanding I've set the annotation correctly.

What am I doing wrong?

JAXB has it's own namespace handling mechanism. I'm guessing it fails (due to lack of proper annotating) before it can see you class wants to handle the namespace.

You can fix it by creating a package-info.java file in the same package as the class then use the @XmlSchema annotation to declare the namespace

@XmlSchema(
        elementFormDefault = XmlNsForm.QUALIFIED,
        namespace = "http://api.esendex.com/ns/"
)
package com.example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

You can see more about namespace handling, here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM