web-services - 使用自定义 jaxb 绑定(bind)获取 xs :datetime to Joda time but getting XMLGregorianCalender

标签 web-services jaxb jodatime wsdl2java xmladapter

Java 1.7
Spring 3.1.1 与 Spring-WS 2.1.1
乔达
休眠 3.6
MySQL 5.0.57
Maven 3
Tomcat 7
eclipse 3.7

已部署 Web 服务和已部署 Web 客户端以及 Web
服务和 Web 客户端相互交谈。

但就像在老式电子游戏中一样,杀死一批龙就会产生
又一批龙。

现在是整个 Joda Datetime xs:dateTime jaxb 绑定(bind)的东西向我喷火。

谷歌搜索出现了很多我试图遵循的解决方案。

代表实体

@Entity
@Table(name="form_templates", catalog="mycomp")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class FormTemplate implements java.io.Serializable
{
    private static final long serialVersionUID = 8533964268513480152L;
    ....

    @Column(name="revision_datetime")
    @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
    @XmlElement(name="revisionTimestamp")
    @XmlJavaTypeAdapter(JodaDateTimeAdapter.class)
    @XmlSchemaType(name="dateTime")
    DateTime revisionTimestamp;
    .... 
}

JodaDateTimeAdapter
@XmlTransient
public class JodaDateTimeAdapter extends XmlAdapter<String, DateTime>
{
    private static final DateTimeFormatter XML_DATE_FORMAT =      ISODateTimeFormat.dateTimeNoMillis();  
    private static final DateTimeFormatter XML_DATE_TIME_FORMAT = ISODateTimeFormat.localDateOptionalTimeParser();  
    private static final DateTimeFormatter DATE_PATTERN =         DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");  

    public DateTime unmarshal(String dateStr) throws Exception  
    {   //parsing from schema to java class  
        DateTime result = DATE_PATTERN.parseDateTime(dateStr);  
        return result;  
    }  

    public String marshal(DateTime dateTime) throws Exception  
    {   //printing from java class to schema  
        String result   = DATE_PATTERN.print(dateTime);  
        return result;  
    }  
}

pom的相关部分
<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-java2ws-plugin</artifactId>
    <version>${cxfVersion}</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxfVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-simple</artifactId>
            <version>${cxfVersion}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>process-classes</id>
            <phase>process-classes</phase>
            <configuration>
                <outputFile>src/main/webapp/WEB-INF/wsdl/FormsService.wsdl</outputFile>
                <className>com.mycomp.forms.web.endpoint.FormsEndpoint</className>
                <genWsdl>true</genWsdl>
                <verbose>true</verbose>
            </configuration>
            <goals>
                <goal>java2ws</goal>
            </goals>
        </execution>
    </executions>
</plugin>

至此,Web 服务编译部署成功。我查看 wsdl 并查看
...
<xs:complexType name="formTemplate">
    <xs:sequence>
      ...
      <xs:element minOccurs="0" name="revisionTimestamp" type="xs:dateTime"/>
      ...

所以看来我正朝着正确的方向前进。部署 Web 服务后,我将注意力转向
到网络客户端

我用这个相关的 pom 片段编译 web 客户端
  ...
  <plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxfVersion}</version>
    <executions>
      <execution>
        <id>generate-sources</id>
        <phase>generate-sources</phase>
        <configuration>
          <sourceRoot>${project.build.directory}/generated_sources</sourceRoot>
          <wsdlOptions>
            <wsdlOption>
              <wsdl>http://localhost:8080/dept_forms_webservice/formsService?wsdl</wsdl>
              <bindingFiles>
                <bindingFile>${basedir}/src/main/resources/jaxb-custom-bindings.xml</bindingFile>
              </bindingFiles>
            </wsdlOption>
          </wsdlOptions>
        </configuration>
        <goals>
          <goal>wsdl2java</goal>
        </goals>
      </execution>
    </executions>
  </plugin>     
  ...

和 jaxb-custom-bindings.xml 位于/src/java/resources
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings wsdlLocation="http://localhost:8080/dept_forms_webservice/formsService?wsdl"
          xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:jxb="http://java.sun.com/xml/ns/jaxb">
  <jaxws:bindings>
    <jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <jxb:javaType name="org.joda.time.DateTime" xmlType="xs:dateTime"
                      parseMethod="com.mycomp.forms.util.JodaDateTimeAdapter.unmarshall"
                      printMethod="com.mycomp.forms.util.JodaDateTimeAdapter.marshall"/>
    </jxb:globalBindings>
  </jaxws:bindings>
</jaxws:bindings>

一切都编译了,生成了源代码,Web客户端成功部署



查看生成的 FormTemplate 实体我看到
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.XMLGregorianCalendar;  // <---- should be joda datetime here

public class FormTemplate
{
    ...
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar revisionTimestamp;   // <-- should be joda datetime here
...

所以我显然错过了一些微观的配置细节,而且会永远如此
如果有人能指出什么是错误的以及如何纠正它,我将不胜感激。

TIA,

仍在学习的史蒂夫

最佳答案

我使用了这个配置并且工作正常。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:annox="http://annox.dev.java.net"
  xmlns:tns="http://esb.tsf.ab.com/enterprise/message"
  xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
  jaxb:extensionBindingPrefixes="xjc annox"
  version="2.1">

  <jaxb:globalBindings>
        <xjc:serializable uid="12343" />
        <jaxb:javaType name="java.util.Date" xmlType="xs:date" printMethod="adapter.DateAdapter.printDate" parseMethod="adapter.DateAdapter.parseDate" />
        <jaxb:javaType name="java.util.Date" xmlType="xs:dateTime" printMethod="adapter.DateTimeAdapter.printDateTime" parseMethod="adapter.DateTimeAdapter.parseDateTime" />
        <jaxb:javaType name="java.util.Date" xmlType="xs:time" printMethod="adapter.TimeAdapter.printTime" parseMethod="adapter.TimeAdapter.parseTime" />
  </jaxb:globalBindings>

</jaxb:bindings>

我用这个插件来生成bean
<plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-Xannotate</arg>
                        <arg>-nv</arg>
                        <arg>-Xnamespace-prefix</arg>
                    </args>
                    <extension>true</extension>
                    <schemaDirectory>src/main/resources/schema/xsd</schemaDirectory>
                    <schemaIncludes>
                        <schemaInclude>my.xsd</schemaInclude>
                    </schemaIncludes>
                    <bindingDirectory>src/main/resources/schema/xjb</bindingDirectory>
                    <bindingIncludes>
                        <include>*.xjb</include>
                    </bindingIncludes>
                    <debug>true</debug>
                    <verbose>true</verbose>
                    <episode>true</episode>
                    <forceRegenerate>true</forceRegenerate>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.0</version>
                        </plugin>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics-annotate</artifactId>
                            <version>0.6.0</version>
                        </plugin>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-namespace-prefix</artifactId>
                            <version>1.1</version>
                        </plugin>
                    </plugins>
                </configuration>
            </plugin>

并将此参数用于 wsdl2java
<wsdlOption>
                                    <wsdl>${basedir}/src/main/resources/schema/integration/wsdl/CostPriceMaintenance.wsdl</wsdl>
                                    <extraargs>

                                        <extraarg>-nexclude</extraarg>
                                        <extraarg>http://namespace</extraarg>

                                    </extraargs>
                                </wsdlOption>

尝试使用这个配置,你有一个额外的。
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:jxb="http://java.sun.com/xml/ns/jaxb">

    <jxb:globalBindings>
        <jxb:javaType name="org.joda.time.DateTime" xmlType="xs:dateTime"
                      parseMethod="com.mycomp.forms.util.JodaDateTimeAdapter.unmarshall"
                      printMethod="com.mycomp.forms.util.JodaDateTimeAdapter.marshall"/>
    </jxb:globalBindings>

</jxb:bindings>

关于web-services - 使用自定义 jaxb 绑定(bind)获取 xs :datetime to Joda time but getting XMLGregorianCalender,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25755508/

相关文章:

c# - 连接的服务组件 Microsoft WCF Web 服务引用提供程序失败。(HRESULT : 0x80131500) the project format is incorrect

java - 乔治亚州亚特兰大的 Java 时区名称是什么?或任何地点?

java - 两个日期之间的星期 Java + Joda Time

javascript - 在 ajax 成功括号内显示对话框时出现问题

java - Java REST Web 服务中的线程

mysql - 尝试获取带有在线数据库的 Web 应用程序?真的很挣扎

java - 使用 jaxb eclipselink2.2 和 cdata 出现意外结果

java - JAXB 属性绑定(bind) - 为什么需要它

java - @XmlElementWrapper 和@XmlElement 无法使用 Jackson 进行反序列化

java - Joda DateTimeZone.getName(long instant) 返回无效名称