views:

129

answers:

1

I have a legacy Java application that uses class mypackage.Foo. I would like that application to utilize a web service that returns instances of Foo. To connect to this web service, I use wsimport on the WSDL to generate the artifacts, and it gives me another Foo.java file. Ideally, I would like to tell wsimport to generate artifacts that use the original Foo and to not generate any additional Foo files, but I haven't found way to do that. How do I reconcile the two Foo files within the Java application? It seems my options are:

1) put the generated Foo file in another package. But then assigning otherpackage.Foo to a mypackage.Foo variable fails

2) use the generated Foo instead of the original Foo. In some cases, this would seem the best solution, but the original Foo has functionality that is not in the generated Foo. Besides, making an application-wide change is not feasible.

3) use the first option and write a method to manually copy the fields of otherpackage.Foo into mypackage.Foo. If all else fails, this will be what I do, but seems horribly inelegant.

I'm sure I'm missing a simple best practice out there. Appreciate any insights!

A: 

When you are generating Java code from WSDL you can use Apache CXF and it's wsdl2java tool (or by using maven-cxf-codegen-plugin) and specify the following option:

-nexclude <schema-namespace>=<java-package> 

Using this option will:

  • tell the wsdl2java tool to not generate classes from the given XML namespace,
  • tell the tool to put into generated code import from the given java-package.

Of course referenced class must exist in java-package later during compilation (not required during code generation).

In Your example use:

wsdl2java -nexclude <some-schema-namespace-I-dont-know>=mypackage
koppernickus