我想添加将Google电子表格中的行更新到我的网络应用的功能。看起来Google Sheets API是可行的方式。它是一个RESTful Web服务,主要是XML驱动的。我想将XML请求和响应建模为Java对象(通过JAXB解组/编组)。但是,我无法找到任何描述它们的WADL或XSD文件。它们存在吗?是否有一种聪明的方式来发现它们?
如果它们不存在,那么生成它们的好方法是什么?通过xml-to-xsd工具运行示例xml响应,然后让JAXB创建Java类?
注意:Google提供了一个Java客户端库,但我想避免在项目中引入更多依赖项。我的用例似乎很简单,不需要它。
答案 0 :(得分:1)
经过深入研究后,我无法找到任何描述服务,请求和响应的WADL / XSD文件。最接近的发现是发现Google表格API基于Atom Feed格式(http://www.w3.org/2005/Atom)。但是通过JAXB运行它的XSD产生了太多额外的东西,我放弃了这条路线。
我编写了一个简单的XSD来满足我的Java XML模型要求。这些模型足以在工作表中读取和添加/更新行。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.w3.org/2005/Atom"
xmlns="http://www.w3.org/2005/Atom"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1">
<xs:element name="feed">
<xs:annotation>
<xs:appinfo>
<jaxb:class name="GoogleSheetFeed" />
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:string" />
<xs:element name="title" type="xs:string" />
<xs:element maxOccurs="unbounded" name="entry" type="GoogleSheetFeedEntry" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="entry" type="GoogleSheetFeedEntry" />
<xs:complexType name="GoogleSheetFeedEntry">
<xs:sequence>
<xs:element name="id" type="xs:string" />
<xs:element name="title" type="xs:string" />
<xs:element maxOccurs="unbounded" name="link">
<xs:complexType>
<xs:attribute name="href" type="xs:string" />
<xs:attribute name="rel" type="xs:string" />
<xs:attribute name="type" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
</xs:sequence>
</xs:complexType>
</xs:schema>