错误:XMLStreamException:使用Odata V2 Java API

时间:2019-03-29 10:21:19

标签: java xml odata

我是Odata的新手,我的意图是使用odata V2 API。按照链接https://olingo.apache.org/doc/odata2/tutorials/OlingoV2BasicClientSample.html并在执行下面的代码行时收到以下错误。

EntityProvider.readFeed(APPLICATION_ATOM_XML, entityContainer.getEntitySet(entitySetName), content,
                EntityProviderReadProperties.init().build());

错误:

org.apache.olingo.odata2.api.ep.EntityProviderException: An exception of type 'XMLStreamException' occurred.
    at org.apache.olingo.odata2.core.ep.consumer.XmlFeedConsumer.readFeed(XmlFeedConsumer.java:83)
    at org.apache.olingo.odata2.core.ep.consumer.XmlEntityConsumer.readFeed(XmlEntityConsumer.java:61)
    at org.apache.olingo.odata2.core.ep.AtomEntityProvider.readDeltaFeed(AtomEntityProvider.java:371)
    at org.apache.olingo.odata2.core.ep.AtomEntityProvider.readFeed(AtomEntityProvider.java:364)
    at org.apache.olingo.odata2.core.ep.ProviderFacadeImpl.readFeed(ProviderFacadeImpl.java:166)
    at org.apache.olingo.odata2.api.ep.EntityProvider.readFeed(EntityProvider.java:660)
    at test.it.data.ODataClient.readFeed(ODataClient.java:81)
    at test.it.data.Main.main(Main.java:16)
Caused by: javax.xml.stream.XMLStreamException: LocalName feed specified did not match with current local name
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.require(XMLStreamReaderImpl.java:1101)
    at org.apache.olingo.odata2.core.ep.consumer.XmlFeedConsumer.readFeed(XmlFeedConsumer.java:73)
    ... 7 more

客户端的源代码如下。

package test.it.data;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
import org.apache.olingo.odata2.api.edm.Edm;
import org.apache.olingo.odata2.api.edm.EdmEntityContainer;
import org.apache.olingo.odata2.api.edm.EdmEntitySetInfo;
import org.apache.olingo.odata2.api.ep.EntityProvider;
import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties;
import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
import org.apache.olingo.odata2.api.exception.ODataException;

public class ODataClient {

    public static final String APPLICATION_XML = "application/xml";
    public static final String APPLICATION_ATOM_XML = "application/atom+xml";
    public static final String APPLICATION_JSON = "application/json";

    public static final String METADATA = "$metadata";

    private String serviceUrl;
    private String username;
    private String password;
    private String entitySetName;

    private Edm edm;

    public ODataClient(String serviceUrl, String username, String password, String entitySetName)
            throws IOException, ODataException, HttpException {

        this.serviceUrl = serviceUrl;
        this.username = username;
        this.password = password;
        this.entitySetName = entitySetName;

        edm = getEdmInternal();
    }

    private Edm getEdmInternal() throws IOException, ODataException, HttpException {

        HttpURLConnection connection = connect(serviceUrl, entitySetName + "/" + METADATA, null, "GET", APPLICATION_XML);
        edm = EntityProvider.readMetadata((InputStream) connection.getContent(), false);
        return edm;
    }

    private void checkStatus(HttpURLConnection connection) throws IOException, HttpException {

        if (400 <= connection.getResponseCode() && connection.getResponseCode() <= 599) {
            HttpStatusCodes httpStatusCode = HttpStatusCodes.fromStatusCode(connection.getResponseCode());
            throw new HttpException(httpStatusCode, httpStatusCode.getStatusCode() + " " + httpStatusCode.toString());
        }
    }

    public Edm getEdm() {
        return edm;
    }

    public List<EdmEntitySetInfo> getEntitySets() throws ODataException {
        return edm.getServiceMetadata().getEntitySetInfos();
    }

    public ODataFeed readFeed(String entityContainerName, String entitySetName, String query)
            throws IOException, ODataException, HttpException {

        EdmEntityContainer entityContainer = edm.getEntityContainer(entityContainerName);
        String relativeUri;
        if (entityContainer.isDefaultEntityContainer()) {
            relativeUri = entitySetName;
        } else {
            relativeUri = entityContainerName + "." + entitySetName;
        }

        InputStream content = connect(serviceUrl, relativeUri, query, "GET", APPLICATION_ATOM_XML).getInputStream();
        return EntityProvider.readFeed(APPLICATION_ATOM_XML, entityContainer.getEntitySet(entitySetName), content,
                EntityProviderReadProperties.init().build());
    }

    public ODataEntry readEntry(String entityContainerName, String entitySetName, String query)
            throws IOException, ODataException, HttpException {

        EdmEntityContainer entityContainer = edm.getEntityContainer(entityContainerName);
        String relativeUri;
        if (entityContainer.isDefaultEntityContainer()) {
            relativeUri = entitySetName;
        } else {
            relativeUri = entityContainerName + "." + entitySetName;
        }

        InputStream content = (InputStream) connect(serviceUrl, relativeUri, query + "?$format=json", "GET", APPLICATION_JSON).getContent();
        return EntityProvider.readEntry(APPLICATION_JSON, entityContainer.getEntitySet(entitySetName), content,
                EntityProviderReadProperties.init().build());
    }

    private HttpURLConnection connect(String serviceUrl, String entitySetName, String query, String httpMethod, String contentType)
            throws IOException, HttpException {

        String urlStr = createUri(serviceUrl, entitySetName, query);
        URL url = new URL(urlStr);
        HttpURLConnection connection;
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(httpMethod);
        connection.setRequestProperty("Accept", contentType);
        connection.setRequestProperty("Accept-Language", "UTF-8");

        String authorization = "Basic ";
        authorization += new String(Base64.encodeBase64((this.username + ":" + this.password).getBytes()));
        connection.setRequestProperty("Authorization", authorization);

        connection.connect();

        checkStatus(connection);

        return connection;
    }

    private String createUri(String serviceUri, String entitySetName, String query) {

        final StringBuilder absolutUri = new StringBuilder(serviceUri);

        if (entitySetName != null) {
            absolutUri.append("/").append(entitySetName);
        }
        if (query != null) {
            absolutUri.append(query);
        }
        return absolutUri.toString();
    }
}

任何有用的指针都将受到高度赞赏。

0 个答案:

没有答案