Webservices / dynamic class generation / flow - java

My question probably sounds all wrong since I'm not sure what to ask exactly. I'm very new to web services in general but get a very high level idea. I'm looking at some code where the classes/methods are annotated and exposed for service calls.
The wsimport command can be used to call the http://.....?wsdl which it looks like dynamically generates these the client classes. I do not see a specific WSDL file so i'm guessing the JAX-WS is creating and sending the client source code to the client whcih downloads it and compiles it to the respective classes.
I notice some difference in the client generated class and the original server side class such as the missing toString() method and equals() method. Trying to read about this suggested something about custommized JAXB bindings and stuff. My questions are:
Does anyone know and can share information that gives a better
understanding of the kind of process I'm seeing. i.e. how the
annotated methods are magically appearing on the client side as part
of their respective classes?
Regarding the JAXB binding most of the websites begin from an
existing WSDL. How does this fit into my current problem?
Thanks

Related

Confusion about web services

We have some existing Java code running on a Tomcat server that we want to allow code on another machine to execute, so we are looking into web services. I am new to web services and I think I'm probably doing something wrong here.
I have followed a few online tutorials on how to deploy a JAX-WS web service in Tomcat. I have created a web service class and annotated it with #WebService, written a web.xml and sun-jaxws.xml file, packaged those files (plus the JAX-WS jar files) into a .war file, and deployed it into Tomcat. That appears to be working, as I can load the WSDL file in a browser pointed at Tomcat.
It's the web service client I'm having trouble with. First of all, there are existing classes on the server side that get mapped into a database. We want to allow the client to use those same existing classes and create instances of out of them, invoke the web service, and then have the objects get stored into the database. However, when I ran the wsimport command against the server's WSDL file, it generated a bunch of Java classes, a lot of which are similar to our existing classes. I guess we have to use those instead of our existing classes? So much for code reuse, unless I'm confused, which is very possible.
So now I've written the client using those classes that wsimport generated. But I'm getting compile errors. Some of the fields in our existing Java classes are of type java.net.InetAddress. But for some reason, wsimport generated its own InetAddress class, and this is what it looks like:
package ems.server.webservices;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "inetAddress")
public class InetAddress {
}
Yeah it's just a class with nothing in it! So a lot of the compile errors I'm getting say:
WSClient.java:37: setAddress(ems.server.webservices.InetAddress) in ems.server.webservices.NetworkAddress cannot be applied to (java.net.InetAddress)
networkAddress.setAddress(InetAddress.getByName("1.1.1.1"));
I'm trying to create a java.net.InetAddress, but it wants me to use the empty class that was generated by wsimport. I must be doing something wrong here. Please enlighten me.
We have some existing Java code running on a Tomcat server that we
want to allow code on another machine to execute, so we are looking
into web services
Web services for this? Why?
First of all, there are existing classes on the server side that get
mapped into a database. We want to allow the client to use those same
existing classes....So much for code reuse
Really bad idea. The purpose of Web Services is not code-reusability but interoperability. Trying to reuse business classes that actually carry the semantics of the programing language you are using, is not only a bad practice but can lead you to many issues unless for most trivial web services where you also control the client.
when I ran the wsimport command against the server's WSDL file, it
generated a bunch of Java classes
When you run the wsimport all the necessary artifacts required for the web service client to communicate to the web server are created. This includes all the stub classes that help in transforming your classes during the SOAP marshalling/demarshalling
But I'm getting compile errors. Some of the fields in our existing
Java classes are of type java.net.InetAddress
java.net.InetAddress is a Java specific class. You should not be exposing that in the first place. That is why the code generator creates the empty java.net.InetAddress.
To be honest I can not give you a direct answer for making this work. The only I hint could give you is that if you annotate the InetAddress fields with #XmlTransient they will not be exposed in WSDL and you will not have that problem. Of course, you don't say if this field is needed for you so, perhaps my suggestion is useless to you.
But my recomendation is to switch your approach. You are on the wrong path IMHO.
The only recomendation is to change your approach.

Accessing a remote service in Java through it's web interface using JAX-WS for SOAP

I have a public EJB class that I want accessible online as a web service. I have generated a WSDL and the SOAP mesasging seems to work. I used soapUI to test the connection. What I'm not clear about is how would I then use this exposed web service. I'd like to try another language like Python to then make calls through that interface. I know that the WSDL is supposed to help a potential client build it's client side code but I'm not sure about how to specify the connection and location and login information if I had that. I know I'm asking a large topic but any information would help. Thanks
Edit: so basicaly I'm just wondering do I have to use tools to generate my client code from the WSDL like axis2. Or whatever Python uses. Or can I write the code by hand? What's generally done. is the server reference included in that WSDL and are call methods generated usually?
Take a look at ZSI
But ZSI is too complex and spends more time to generate proxies
I suggest you to use suds. suds generates the proxies On the fly and so fast, i used it in some projects.
another packages are available:
soaplib
SOAPy
pysimplesoap

Retrieving WSDL for a single operation

Im a Newbie in wsdl parsing and my information about WSDL is very limited.
I have a scenario in which there is a wsdl file with multiple operations and i want to get the wsdl for a specific operation from that. Is there any libs in java or JS to accomplish such a task . Or am i missing something.
Please correct me if there is anything wrong in the question ,
Thanks in Advance
Bijesh
WSDLs represent a single service with specific operations. Those operations belong to the service can't be separated from the service itself. In order to simply invoke one method you'll have to bind against the entire service (and all other operations and defined types). If you are publishing the operation, you'll have to bind and publish all operations and types defined in the service. There isn't really a way around that.
Now, in your case if you are using java and if you are acting as a client, you can do what Alfredo O alluded to and use a SOAP framework's tooling to generate all of the java client code for you. From there it's just a matter of using the actual service class and invoking the method on that class that corresponds to the method you want to call. You'll have to use the entire wsdl, but from the perspective of your code you won't have to worry about calling any other methods than the one that interests you.
Popular choices for generating a java client for a SOAP service are:
Apache CXF
Metro
Apache Axis2
You can use Axis 2 to auto-generate the java code needed to invoke those operations. Use the wsdl2java tool provided by Axis 2.

Using Jax-B with Jax-WS in order to return complex types

I'm sure you get plenty of questions on this type of thing on here, but none of the answers I've found seem to deal with the problem I'm having, so I was hoping that I might be able to get some specific answers.
I have a project which consists of a client and a server. The server connects to a database and the client can call the methods in the server through the wsdl file. This works fine, but the server can only return simple types and lists. I looked up how to handle custom types, and I was directed to JaxB, which seems to be exactly what I'm looking for. Unfortunately I'm a little confused as to how I'm supposed to use it. I'm using the NetBeans IDE (6.9.1), which makes it easier to deal with the wsdl files, although I have read that passing complex classes is fairly trivial from the command line, once you've got the wsdl end of things working properly?
What I want to do is to have the server return a class - it'll just contain data - that the client can read and use.
I've done the following, but obviously I'm going wrong somewhere!
I have a class, called Customer on the server side which is constructed and returned when a particular method is called by the client.
The client obviously cannot interpret this class correctly.
I've used schemagen to create an XML schema from the Customer class. I then use the netbeans wizard to bind the schema to a class in the client.
I suppose this is where I'm confused. I want to unmarshall the xml response from the server and use it to create a new object on the client. I'm using the "jaxbu" netbeans shortcut which expands into some code, but I'm unsure of how to put the returning object into the unmarshaller. If that makes any sense?
The code generated by "jaxbu" is similar to below, inside a try block:
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(c.getClass().getPackage().getName());
javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
c = (CustomerInfo) unmarshaller.unmarshal(new java.io.File("File path"));
Any help would be very much appreciated, and if you required more info. please don't hesitate to ask.
Thanks,
Mathew
I am not going to be the best help with the final answer but I asked similar question a while ago and was pointed in useful directions in that question. like Blaise Doughan said you need to use something to move through the xml document and find the pieces you need then marshall them from there.
JAXB unmarshaling Ignoring the SOAP Envelope/Header tags
you shouldn't be doing the jaxb stuff directly. instead, you put all the complex types in the schema associated with your wsdl (assuming you are working from the wsdl). then you use the various jaxws tools (wsgen, wsimport) to generate the stubs and jaxb classes from the wsdl (for both the client and the server).
pretty much all of this is described in the jaxws tutorials (metro is the reference implementation of jaxws).

expose JAXB generated Class as WSDL web service

I have a bunch of very simple functions.
Each function has one input and one output.
OutputType function func(InputType);
The types of input/output are defined in xsd schema and generated into java classes with JAXB/XJC. Now I want to expose those functions as WSDL Web service running on Geronimo.
I just took a look at Axis/WSDL2Java/Java2WSDL; I thought that is pretty in a similar way as my functions are created.
I guess, I can use Java2WSDL to generate WSDL from my function and input/output types.
and then use some tools to generate server/client side binding,
Can anyone give more further suggestions? especially I have defined my input/output of functions in a xsd schema.
thanks very much.
A Summary:
These are what I have now....
Many implemented functions with one input and one outout.
public OutputType functionXXX(InputType in) { ....; return output; }
InputType and OutputType are already defined in a xsd schema (and turned into java classes with Jaxb/xjc).
What I want is....
Build Web services to execute those functions.
Not to touch the code of implemented functions.
And with WSDL,
I found a tutorial using CXF to do what you are looking to here.
That document claims to be using a contract first approach, but it isn't exactly. When discussing SOAP-based services, contract first means creating the descriptors (WSDL, XSD) first. You then generate any code artifacts from those descriptors. You can see the comments in the original blog post for the debate about the original author's choice of words.
That being said, a contract first approach has many benefits depending on what you are trying to accomplish. See the Spring Web Services tutorial for some information about it.
If you have an existing schema, with existing JAXB2 bindings for it, then in my experience Spring WebServices is by far the easiest way of exposing that as a SOAP web service. Its philosophy is "contract first", which is is exactly what you have. You don't need to generate any additional bindings, just wire up the endpoints a la Spring MVC, plug in the marshaller, and of it goes. It will introspect your schema looking for things that look like operations and expose them as WSDL operations automatically (you can tell it how to do that, if the default auto-discovery doesn't quite work).

Categories

Resources