expose JAXB generated Class as WSDL web service - java

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).

Related

Webservices / dynamic class generation / flow

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

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.

How to generate a wsdl without a default request / response suffix in Spring?

The definitions for the methods of a web service have been provided to me by a 3rd party, i've created the java classes and maven schemagen plugin generates the xsd brilliantly from the classes. I then set up a spring-ws-servlet etc to publish the wsdl, only the WSDL11Definition classes require a request and responsesuffix.... I don't have a suffix. the method names are things like getAttribute, setAttribute, refreshDetails, manageAttribute etc. Does anyone know of a way around this? are there other wsdl definition classes that can be referenced to do this, or perhaps other providers (instead of the suffixProviders that the defaultWsdl11Definition uses)?
Any help would be appreciated!
Spring-WS is designed around "schema first" design, rather than "code first":
When creating Web services, there are
two development styles: Contract Last
and Contract First. When using a
contract-last approach, you start with
the Java code, and let the Web service
contract (WSDL, see sidebar) be
generated from that. When using
contract-first, you start with the
WSDL contract, and use Java to
implement said contract.
Spring-WS only supports the
contract-first development style, and
this section explains why.
You can make it work with "code first", but you're going to have a fight on your hands.
If you want to stick with Spring-WS, then I strongly suggest that you use the web service spec to write an XML Schema describing the operations, and then either generate java from that schema, or write the java by hand.

Can I create server-side Java classes, OR mapping, and client-side AS classes from a single model description?

I have a Java server application and a Flex client application. For my data model, I have typical objects, like User etc., that are basically the same in the database, the server-side Java code and the client-side AS code. I. e., the public fields of my AS and Java classes are named identically, and have the same types and values as the respective fields in the database table which has the same name as those classes.
For the Java classes and the DB tables to match, I can use any ORM. Are there any tools to have the AS classes (and, ideally, the matching Remoting boilerplate code) autogenerated from the Java ones as well? I had a short look at BlazeDS, but it didn't seem to have tools like that, at least I couldn't find anything about it.
Of course, in a perfect world, I'd describe my data items once and have the DDL, Java code and AS code all autogenerated. Does such a tool exist, and is it usable enough?
Oh yeah, and it would be great if the source format could be protobuf files...
Note: This seems to be the same question as this one. The answers there seem to indicate that at least there's no common tool that "everybody uses" or that has a reputation for high quality.
You can start here : http://www.graniteds.org/confluence/display/DOC/2.+Gas3+Code+Generator
The only tools that I know of are for generating AS classes from server code are the wizards built into Flex builder for autogenerating AS remote classes from ColdFusion CFC's for use with a ColdFusion based AMF service.
What may be useful to remember though is that once the serialized object has been received by Flex and an instance of mx.utils.ObjectProxy has been created for it (this relates to the flex RemoteObject class and AMF based services, but I imagine that a Protocol Buffer extension would follow the same pattern), the process of casting the proxy to an Actionscript class isn't all that complicated. As long as your Actionscript class has properties or accessors with names that match the properties on the serialized object, and an Remote Object alias that matches the type of the serialized object, then you can cast the object proxy to the remote class without having to do any extra work.
Like I said, I'm not aware of a tool that does what you want, but It wouldn't be too difficult to create a tool to read the type and public properties from your Java classes, or extract that same metadata from your protocol buffer files, and write some Actionscript code off that Metadata. If you can't find a solution that fits your needs, you might consider writing an Ant task or a script or something to do it for you. Thats what I would do anyway.

What is the best way to expose a WCF service so that it can be easily consumed from Java/CXF?

We've written a WCF service to be used by a Java shop, who is using CXF to generate the adapters. We're not that familiar with Java, but have exposed the service using basicHttpBinding, SSL, and basic authentication. Integration tests show that a .NET client can consume the service just fine. However, the Java shop is having trouble consuming the service. Specifically, they getthe following JAXB error: Two declarations cause a collision in the ObjectFactory class. This is usually caused if 2 operations have the same name and namespace when CXF attempts to create adapter classes.
We can't find any type or operation names that should cause any sort of collision. We have made sure that all custom types specify a namespace, and tempuri.org is not specified anywhere in the WSDL. The Java shop suspects the error is because the generated WSDL contains <xsd:import elements.
So, my questions:
Is there any better way than CXF for the Java shop consume the WCF service? Project Tango looks interesting, but I don't know enough to tell them to consider using it. Is CXF a defacto standard in Java?
BasicHttpBinding/SSL/Basic Auth are MS recommended for interop scenarios, but the client still seems to have interop problems. Should we consider other bindings or settings to make this easier to consume?
Is there a way to configure WCF to always output a single WDSL with no schema imports?
The "Two declarations cause a collision in the ObjectFactory class" error message usually has nothing to do with imports. That's a JAXB error message that is usually caused by having multiple elements or similar that cause the generated field names to be the same. For example, if you have elements like:
<element name="Foo" .../>
and
<element name="foo" .../>
That can cause that error. Another is using thing like hyphens and underscores and such that are usually eliminated+capped:
<element name="doFoo" .../>
and
<element name="do_foo" .../>
With 2.1.4, you can TRY running the wsdl2java with the -autoNameResolution flag. That SOMETIMES helps with this, but not always. Unfortunately, the information that JAXB gives in these cases is nearly worthless and lots of times it's just trial and error to find the conflicting types. :-(
I am deep into Java & WCF interoperability. As someone else said you need to flatten your WSDL if you are working with file based WSDL. However I use Netbeans 6.5 and if you point to a real url like http://myservice/?wsdl , Netbeans can cope easily with the default wsdl generated by WCF.
In real life other things you need to consider is service versioning, optional datamembers (doesn't go well in java, so I suggest to make all datamembers IsRequired=true), order etc.
The real tough thing to get going was security. I had to make mutual certificate authentication working and it still has some issues.
The only way for your java client to talk to a WCF component will be one of the HTTP methods - basicHttpBinding, ws*, etc just as MS recommends. Java can't talk to WCF over TCP or namedPipes or MSMQ, etc.
I'd start with a super simple WCF component - something with a single method that spits out a string. Get that working with Java and then work your way up. Make sure that everything you're exposing is working with base types or well defined [DataContract] objects.
I've developped WCF with Axis2 clients. The authentication methods I've sucessfully uses is BasicHttpBinding/SSL/Basic (Transport) and WS-Security with Username (and MTOM).
The Metro implementation is used by SUN and Microsoft to test the interop :
http://weblogs.java.net/blog/haroldcarr/archive/2007/11/metro_web_servi.html
Sorry no clue about the import generated by WCF for the schema definition.
The problem with xsd:import is very common. Some toolkits or runtimes cannot cope with that. To address this, you can flatten the WSDL that is generated by WCF.
Check this post.
Regarding whether CXF is the right Java stack - I have never heard of it? I have used AXIS successfully, as well as JAX-WS. Both have been pretty straightforward.
This is a Jaxb issue. I ran into the same issue but used the xmlbeans option instead in wsdl2java client generation. To be honest I seem to prefer the xmlbeans objects more over jaxb as far a consumer to this webservice.

Categories

Resources