wsimport client - customise multiple package names
Asked Answered
C

2

8

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?

Crymotherapy answered 5/1, 2015 at 12:37 Comment(4)
It's possible with a custom jaxb binding. Post your wsdl (or something like it here). The ideal scenario would be for your various schema to be in separate xsds, but I presume you don't have thatMolasses
@Molasses - they are in separate schemas. How does that help?Crymotherapy
Having them in separate files means you don't have to deal with a tangle of Xpath or spilling a bunch of jax-b binding directives in your WSDLMolasses
Did you ever get around to trying this?Molasses
M
7

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:

Molasses answered 6/1, 2015 at 16:44 Comment(1)
New link: javaee.github.io/jaxb-v2/doc/user-guide/… Full JAX-B guide: javaee.github.io/jaxb-v2/doc/user-guideMiguelmiguela
S
4

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

Strung answered 22/9, 2015 at 9:14 Comment(1)
Note that this example gives "[ERROR] The package name 'your.package.name.schema2' used for this schema is not a valid package name." The reason is that "package" is a restricted word in java. For example with 'your_package_name.schema2' the example runs fine.Footloose

© 2022 - 2024 — McMap. All rights reserved.