Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wsimport client - customise multiple package names

I am using wsimport to generate client stubs for JAX-WS webservice calls

wsimport has the -p option which allows to customise name of package.

For eg. if the WSDL has namespace of com.abc, then you can subsitute com.abc by com.pqr by calling wsimport with the -p com.pqr command line.

However, this works fine only if there is only one namespace used in the wsdl.
If there are multiple namespaces in the wsdl, is there a way to replace each of them with a different package name.

For eg. if I want namespace com.abc.s1 to be replaced by namespace com.pqr.s1 & namespace com.abc.s2 to be replaced by namespace com.pqr.s2.

If I use wsimport -p com.pqr.s1, it puts all the generated classes into com.pqr.s1

Is there a way to achieve what I want?

like image 812
user93353 Avatar asked Jan 05 '15 12:01

user93353


2 Answers

Generally, you use a jax-b bindings file to customize the unmarshal process for a given XSD or WSDL. The bindings language provides the <package/> directive for the purpose of customizing the generated package of a schema.

Given separate schemata, in separate files, you can have a composite bindings file that'll look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               jaxb:version="2.0">
  <jaxb:bindings schemaLocation="Flight.xsd"  node="/xsd:schema">
    <jaxb:schemaBindings>
      <jaxb:package name="travel.flight"/>
    </jaxb:schemaBindings>
  </jaxb:bindings>
  <jaxb:bindings schemaLocation="Hotel.xsd" node="/xsd:schema">
    <jaxb:schemaBindings>
      <jaxb:package name="travel.hotel"/>
    </jaxb:schemaBindings>
  </jaxb:bindings>
</jaxb:bindings>

Where schemaLocation will refer to the location of individual schema files, node refers to the XML element that the binding declaration is supposed to apply to. <jaxb:package/> will define the name of the output package.

You should then feed the bindings file to wsimport using the -b directive and you should be fine

Reference:

  • JAX-B Reference Guide/Customizing package names
like image 58
kolossus Avatar answered Oct 12 '22 14:10

kolossus


The way i did it, is by doing the following.

First create a schema.xjc file

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               jaxb:version="2.0">
    <jaxb:bindings schemaLocation="YOUR_URL?wsdl#types?schema1">
        <jaxb:schemaBindings>
            <jaxb:package name="your.package.name.schema1"/>
        </jaxb:schemaBindings>
    </jaxb:bindings>
    <jaxb:bindings schemaLocation="YOUR_URL??wsdl#types?schema2">
        <jaxb:schemaBindings>
            <jaxb:package name="your.package.name.schema2"/>
        </jaxb:schemaBindings>
    </jaxb:bindings>
</jaxb:bindings>

The package name can be anything you want it to be, as long as it doesn't contain any reserved keywords in Java

Next you have to create the wsimport.bat script to generate your packaged and code at the preferred location.

cd C:\YOUR\PATH\TO\PLACE\THE\PACKAGES
wsimport -keep -verbose -b "C:\YOUR\PATH\TO\schema.xjb" YOUR_URL?wsdl
pause

If you do not want to use cd, you can put the wsimport.bat in "C:\YOUR\PATH\TO\PLACE\THE\PACKAGES"

If you run it without -keep -verbose it will only generate the packages but not the .java files.

The -b will make sure the schema.xjc is used when generating

like image 22
Glenn Van Schil Avatar answered Oct 12 '22 12:10

Glenn Van Schil