这是我的 OSRMResponseDto.xsd 的摘录:
<xsd:complexType name="OSRMResponseDto">
<xsd:sequence>
<xsd:element name="route_geometry" type="xsd:string"/>
<xsd:element name="route_name" type="xsd:string"/>
<xsd:element name="status" type="xsd:long"/>
<xsd:element name="status_message" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
绑定.xjb:
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
<jaxb:globalBindings underscoreBinding="asCharInWord"/>
</jaxb:bindings>
问题是 maven 生成的类具有以下字段:
@XmlElement(name = "route_geometry", namespace = "http://osrmresponse.generated.core", required = true)
protected String route_Geometry;
@XmlElement(name = "route_name", namespace = "http://osrmresponse.generated.core", required = true)
protected String route_Name;
@XmlElement(namespace = "http://osrmresponse.generated.core")
protected long status;
@XmlElement(name = "status_message", namespace = "http://osrmresponse.generated.core", required = true)
protected String status_Message;
正如您在此处看到的,它将第二个单词大写,这就是 gson 无法将来自 OSRM 服务器的输出响应转换为我的 DTO 类的原因。如何使字段与 .xsd 源中的字段相同?
UPD1:maven插件配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>gpx-xjc</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<bindingDirectory>${basedir}/src/main/xsd/gpx/schema</bindingDirectory>
<clearOutputDir>false</clearOutputDir>
<npa>true</npa>
<schemaDirectory>${basedir}/src/main/xsd/gpx/schema</schemaDirectory>
<target>2.0</target>
<extension>true</extension>
</configuration>
</execution>
<execution>
<id>interfaces-xjc</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<bindingDirectory>${basedir}/src/main/xsd/interfaces</bindingDirectory>
<clearOutputDir>false</clearOutputDir>
<npa>true</npa>
<schemaDirectory>${basedir}/src/main/xsd/interfaces</schemaDirectory>
<target>2.0</target>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
<configuration>
<!--
Package name of the generated sources.
-->
<packageName>core.generated</packageName>
<!--
Copy all source XSDs into the generate artifact.
Place them at the supplied xsdPathWithinArtifact, that is within the given directory.
-->
<xsdPathWithinArtifact>src/main/java/core/xsds</xsdPathWithinArtifact>
</configuration>
</plugin>