I have the following requirements and thinking about how to best get java objects from a WSDL.
XML data comes from a public SOAP Webservice
I have to use JAXB
I want to automatically unmarshall the retrieved data to Java objects
Ideally I'd like to have java objects using JAXB Annotations. Are there any tools that I could combine to autogenerate these?
Sure, there are lots of ways to use JAX-WS (which uses JAXB for its XML binding) to generate a web service client.
You can execute it from the command-line:
http://www.mkyong.com/webservices/jax-ws/jax-ws-wsimport-tool-example/
As part of your Maven build:
https://jax-ws-commons.java.net/jaxws-maven-plugin/
Or from within your Eclipse environment:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jst.ws.cxf.doc.user%2Ftasks%2Fcreate_client.html
Yes, there are. With every jdk, there is an executable file called wsimport that does exactly what you want.
Here is an answer i gave to a similar question.
Related
I'm looking for an easy way to acces wsdl file from java.
In python with suds I can do somethin like:
self.wsdlClient = Client(WSDL_URL)
wsdlClient.methodName()
Is there a way to connect to wsdl through an URL?
Python is a dynamic language so you can create methods on an object on the fly, like for example a method for each operation of your web service as discovered by reading the WSDL. But Java is not a dynamic language so the Python approach can't be used.
You either have to generate the code at design time (i.e. create a stub) by feeding the WSDL to some tool like wsimport.exe (or other tools) then use the generated code in your application or ...
... you have to parse the WSDL at runtime and create the XML payload based on the information you acquired from the WSDL, in which case you might want to look at the following tool: soap-ws.
The easiest way is to generate a stub.
Just a little background:
I have a wsdl and schema files with a lot of hierarchy, meaning there are a lot of import/include tags in the schema. I have a netbeans project and used wsimport to generate the client code. I’m successfully calling the web service operations and getting data.
What I need:
I'm looking to get access to the model the xjc compiler uses to generate the java code from the schema. I would like to do this without writing my own plugin if possible. I want to use this model to generate my own code with codemodel.
The question is:
Is there a way to get access (preferably from my client project described above) to the model or 'outline' without writing a xjc plugin?
I’m new to java and jaxb so any direction and detailed instructions are much appreciated.
You can invoke the xjc compiler directly by using Ant or Maven. Just point it at the schemas referenced. If you need some control over the code generated, you could look at custom binding in JAXB.
What exactly are you trying to accomplish?
I have an xml file format and using that xml I want to create skeleton classes needed to serialize and de-serialize that xml. I am using Java and XStream for this.
There is tool in .net world which creates classes using xml. Is there anything similar in Java world?
I have not used XStream myself, so this solution may not 100% work for you. However, the simplest approach in Java is to use Java's Architectural Binding for XML (JAXB) API and tools. JAXB was included as part of the JDK with the release of Java 6. To generate Java code from an XML schema you would use the xjc command that comes with the JDK. Here is an example:
> xjc schemas\my-schema.xsd -d src\java -p com.company.model
This code generation method will create Java Objects that include JAXB-specific annotations that are used by the Marshaller to map the Java object to its XML format and vice versa. It will also contain number of warnings stating that the code was auto-generated and should not be modified. As long as you are not trying to automatically keep the code in synch with your XML you could ignore these messages.
Now, as I mentioned, this technique does generate JAXB annotated classes, however, the generated code may still be compatible with XStream as I believe XStream uses simple attribute name -> xml node name conversion logic.
I am using axis2 to create a web service from a java class I created. Everything works well, but I'd like to be able to customize the wsdl and I cannot figure out how to do it. I tried using some JAXB annotations with my objects (using axis1) but it had no effect on the wsdl generation.
I'd like to be able to specify nillable=true for some elements and make others required. I'd also like to be able to change element names and other things. Shouldn't axis2 look at the JAXB2.0 annotations if there's no WSDL included in the META-INF folder?
I have been trying to google how to map out the schema in a bottom-up approach but I have not had luck. The axis2 website only shows basic steps for creating a web service or a client, but nothing about customizing the schema.
To use JAXB annotation with AXIS2, you should have a look at this article :
Java Web services: JAXB and JAX-WS in Axis2
In Eclipse.org you find (Eclipse Helios) Eclipse IDE for Java EE Developers which is useful to create a web service easily and you can look at the documentation once for use of it effectively.
I need to create connection to web service with axis2. I would like to know how can I convert recieved xmls to Java objects. Is there any good tutorials to learn how to do this?
Axis2 is pretty well documented. Make sure to check the axis2 user guide.
On the other hand be aware of that using web services and axis2 is not a piece
of cake sort of think, so read the docs exhausitvly. ;)
Either the XML is part of the SOAP request. In that case, Axis2 will convert it to Java for you.
If XML is part of a "data blob", then you need to do this yourself. There are several options:
You can have a look at the generators which Axis2 uses. Try the docs.
Use an XML OO mapper like Castor.
You can write your own mapper (not really recommended).
If you can influence the sender side, you can try to create XML that follows the rules for XMLEncoder. That would allow you to use the standard Java Serialization API to build objects.
use axis wsdl2java tool and put generated classes to your source files. Then first create a service stub with YourServiceStub(Service_Adress) and use that instance to invoke necessary methods.
for wsdl2java i commonly use:
wsdl2java -uri wsdlLocation -ss -sd -uw -g -o outputLocation