以下是我的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>
binding.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>