Skip to content

Instantly share code, notes, and snippets.

@vicly
Last active August 14, 2021 08:13
Show Gist options
  • Select an option

  • Save vicly/a85ba7583be54c9c025af85f038fe7a3 to your computer and use it in GitHub Desktop.

Select an option

Save vicly/a85ba7583be54c9c025af85f038fe7a3 to your computer and use it in GitHub Desktop.
[WSDL java code gen] #Gradle

Generate java.time.OffsetDateTime for xs:dateTime

JXB file - MyService.jxb (see extraArgs in gradle file)

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">

    <jaxb:globalBindings>
        <jaxb:javaType name="java.time.OffsetDateTime"  xmlType="xs:dateTime" hasNsContext="false"
                       parseMethod="util.JaxbDateAdapter.parseDate"
                       printMethod="util.JaxbDateAdapter.printDate"/>
    </jaxb:globalBindings>

</jaxb:bindings>

Customised Adapter for serialization & deserialization

package util;

public final class JaxbDateAdapter
{
  private JaxbDateAdapter()
  {

  }

  public static OffsetDateTime parseDate(String s)
  {
    return OffsetDateTime.parse(s);
  }

  public static String printDate(OffsetDateTime dt)
  {
    return dt.toString();
  }
}
apply plugin: "com.codeartisans.gradle.wsdl-tasks"
def generatedSourcePath = 'src/main/generated'
sourceSets {
main {
java {
srcDirs = ['src/main/java', generatedSourcePath]
}
}
}
task deleteGenerateWsdlClients(type: Delete) {
delete "$projectDir/" + generatedSourcePath
doFirst {
print("Deleting generated source code from WSDL")
}
}
task generateWsdlClients(type: org.codeartisans.gradle.wsdl.WsdlToJava) {
def output = file("$projectDir/" + generatedSourcePath)
output.deleteOnExit()
outputDirectory = output
wsdls {
xmlDocPublisher {
wsdl = "src/main/resources/wsdl/MyService.wsdl"
extraArgs = "-b src/main/resources/wsdl/MyService.jxb"
}
// Any more Wsdl-to-Java, add here
}
doFirst {
print("Generating source code from WSDL")
}
}
generateWsdlClients.dependsOn deleteGenerateWsdlClients
compileJava.dependsOn generateWsdlClients
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment