This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

customizing prefix namespace with xjb

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to customize the namespace prefix of my external xsd with a xjb file, because the generated SOAP message contains ns2: prefix, and I need abc, but I can't get it to work.
There's not much documentation available.
It should be enough to use the schemaBindings element.
Any suggestions?


 
Saloon Keeper
Posts: 11462
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehh, why do you need this? The namespace prefix is absolutely inconsequential, as long as the namespace itself is correct.

Anyway, if you want to do this anyway, I think you need a separate namespace prefix plugin for xjc. How are you running xjc, manually or through a build tool?
 
Kenji Watanabe
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan, I am using the jaxb2-maven-plugin.

The result of my XJC execution is a rootelement without prefix but with the namespace:

To use this as a Soap request in SoapUI I need to add the prefix to the rootelement and remove its namespace,
and add the Soap envelope where  I change the (by WSDL) automatically generated prefix csw: to ns2:

I get a meaningful response in SoapUI instead of a fault.
So I'd like to have these customization made through XJB.
 
Stephan van Hulst
Saloon Keeper
Posts: 11462
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result of your XJC execution is not a root element. It is a Java class.

Where you put the prefix declaration doesn't matter, as long as all the nodes that use the prefix are scoped to the element containing the declaration. So you can leave the ns2 prefix declaration on the LalalaRequest element, you just need to prefix LalalalaRequest with ns2:.

I suppose you got a WSDL from whomever maintains the service you are trying to access. When generating a service client from the WSDL, why can't you just use it as is without modifying the bindings?
 
Kenji Watanabe
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome, just adding the prefix to the LalalalaRequest element made it work in SoapUI, even when the envelope still uses the csw prefix.
Then I need to find a way to add the prefix with XJB, thanks!
 
Stephan van Hulst
Saloon Keeper
Posts: 11462
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "the envelope still uses the csw prefix"?

I don't understand why you would have to add a prefix to anything at all. Is the WSDL you got faulty?
 
Kenji Watanabe
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output of my XJC execution is

Since it has no envelope I tried testing in SoapUI by generating a default request from the imported WSDL, and pasting the XJC result into the body, after addition of the ns2 prefix to the rootelement:

Notice the difference in namespace of the envelope

and the rootelement

This still results in a valid Soap response message, thanks to the addition of the ns2 prefix to the rootelement type.
 
Stephan van Hulst
Saloon Keeper
Posts: 11462
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kenji Watanabe wrote:The output of my XJC execution is


No, it's not. The xjc goal of the jaxb2-maven-plugin generates Java source code. Not XML. Please show us your Maven POM, and what commands you are executing to get the XML.

Since it has no envelope I tried testing in SoapUI by generating a default request from the imported WSDL, and pasting the XJC result into the body, after addition of the ns2 prefix to the rootelement


This is an extremely brittle workflow. It also doesn't make sense that it should work at all, the way you have manipulated the namespaces, because the semantics of the SOAP request are now different.

Have you already tried generating an example request in SoapUI from the WSDL using the option to generate examples for optional elements and lists?
 
Kenji Watanabe
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I unmarshall the XSDs to Java objects by XJC.
The maven-jaxb2-plugin is straight through:


Then I use them to create a request object to send to an external webservice by Jersey.
I have a full working example from a previous employee for another application (but I do not have its WSDL).

I generated a mockservice from my WSDL in SoapUI, http://localhost:9080/mockHttpBinding, with an example request and response and they work manually.

Since the external service is not yet available I sent the request to the mockservice URI but I got a back


Then I outputted the created request object as XML by marshalling it through an OutputStream

resulting in

To test its validity I replaced the body of the Soap example request and got

and I noticed the ns2 prefix on all but the outer element.
To check on the syntax I first put the output in the example request body, changing ns2 prefixes to csw, and it worked.
Then I tried the same by only setting only the outer element LalalaRequest to ns2, which also worked.

What I really want though is getting a valid response from the SoapUI mock service.

 
Kenji Watanabe
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, after registering a LoggingFeature (org.glassfish.jersey.logging) to my client I see now the request from there

and the response

So the ns2 prefix is no issue but apparently the default with JAXB marshalling.

The example application has the same type of output btw.

Then the only question remaining is how I can get the request in a Soap envelope to test it with SoapUI.
 
Stephan van Hulst
Saloon Keeper
Posts: 11462
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just generate a service client from the WSDL, and call the service from Java code? If you want to see the raw SOAP request, you can use a network monitoring tool like Fiddler or Wireshark.
 
Kenji Watanabe
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan,

I don't know how to generate a service client, but I think I misunderstood the Jersey client.
In short, the request is fired by

where the requestElement is the object that is the result from the XJC compilation, set with values (is that called a proxy object?)
The LoggingFeature registered on the Client instance is only showing plain XML, but not a full SoapMessage,

so I do not understand how the example application gets a valid response if it is supposed to be a Soap request.

I found a tutorial to send the request by WebServiceTemplate, which uses the SaajSoapMessageFactory under water.
That does generate the expected SoapEnvelope, so I am going to use that to talk to the SoapUI mocking service.

Thanks for your help!
 
Stephan van Hulst
Saloon Keeper
Posts: 11462
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.ws.rs isn't used for SOAP. It's used for REST.

You can simply generate a SOAP client using the jaxws-maven-plugin:

Now all that you need to do is change the <packageName> to whatever package you want to generate the SOAP client in, and put your WSDL in the src/wsdl folder. When you build the application, source files will be generated in a separate source folder and you can access the service from your application like a regular Java object:

For reference, this is the WSDL I generated the service from. Everything works just fine:
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!