Consuming JAX-WS Web Service - best practices? - java

What is the best practice for consuming JAX-WS web service in a Java client? I am inclined to use wsimport to generate stubs. It means using JAXB. Please suggest.
Thanks.

I don't know that it's necessarily a best practice, but I definitely recommend using wsimport/JAXB generated stubs for consuming a SOAP service in the client. You could also use any number of other methods but they all boil down to two options:
1) have stub code generated that does all the XML and HTTP (or other protocol) work for me
2) concoct an XML message in a Java String and use various HTTP (or other protocols) methods to send that XML to the remote service. Then parse the result using some method (regex, custom parser, SAX, DOM, etc..)
In reality option 1 boils down to option 2, but you never see it.

Related

HTTP + XML or SOAP for creating Web Services

I have created a web-service which involves accepting certain parameters in URL, validation the and returning XML response to client using JAXB xml library. Recently I came across JAX-WS framework which uses SOAP over HTTP to transfer request/response to/from client. Am I losing something by using simple servlet + connection pooling and serving XML response over HTTP ? Client will be hitting the URL created via some systems which are not known yet.
Is there any advantage to use REST or SOAP protocol, when simple HTTP + XML or JSON can solve the problem.
Sounds like you do not have any constraints over what you serve which leaves you with loads of options. There are quite a few angles to approach this question from, I just list some of the most obvious practical considerations below and leave out any architectural discussion.
SOAP is generally (but not always) used for enterprise integration - but it has a lot of shortcomings - not least in its verbosity. My advice would be not to use SOAP unless you really have no choice in the matter.
XML or JSON ? would your clients prefer to consume JSON or XML? If you are just publishing the service and not consuming it then I would go with JSON since it is becoming a very popular message format now for web services.
If you are the only one consuming the response then you need to think about what your client technology/framework would likely prefer to parse and how big the message payload is likely to be. If it is rich (loads of nested objects and attributes) then an XML schema might suit, but if the messages are very large then consider that the JSON footprint is likely to be smaller than the XML one.
You are not missing anything in your approach. Go for the simplest option - which depends on what framework/libraries you are using and the skill-set of your developer(s). If a servlet appears to be the most straightforward to you, go with that. If your data is already in XML then that seems like the way to go, but if not, I would consider publishing JSON format first. If you're not sure how to do that, first have a look at Jackson.
Update:
If you are still not sure which way to go then just serve JSON.
Also, the format of the messages you consume/publish should not dictate how you implement your application design - I mean, the question to use a servlet or not does not really factor into what message format to use, unless you intend to use a framework which ties you to a particular approach eg. Play Framework will very easily allow you to serve JSON from a Controller (not Servlet) so if you were using this framework - for example - you would not want to use a servlet and you would use JSON since that is the easiest way to go because of the out-of-the box support the framework already provides.

Consuming generic xml soap webservice in Java without wsimport

I'm searching for a way to consume soap webservices without using wsimport and tools like that. So far, everyting I found requires the wsdl artifacts generation and I want to avoid it because I don't know wich webservices will be used.
The idea is to give the user the possibility to add several WSDL's urls / methods and the program consume them and send it's response to another url.
I'm not looking for out of the box solutions/code or something like that.
What I need is to know if it is possible or not and how to aproach this problem, ideas and things like that.
Thanks!
SOAP mandates a contract between the Client and Server. The contract is that Client will give a request XML is XYZ format and Server will give response in ABC format. Without this, SOAP does not work. This is what is defined in WSDL. So we have to use WSDL.
Tools like wsimport, wsdl4j convert these WSDL files into an object hierarchy and prepares a JAXB wrapper for you, to make life easy!
Now if you do not want to use the tools which automatically convert WSDL to Java Beans or anything of that sort, note SOAP is based on nothing but an XML. If you know how to parse an XML in java, thats pretty much it. You can use a JAX Parser to achieve this and you can read everything that comes in a SOAP request and response.
The problem that you will face is, as the complexity of the SOAP XML grows, it will be difficult for you to parse the XML properly, identify the nodes, values and relations properly. And add to that the problem where in you cannot, without a proper object hierarchy , relate between nodes which are distance apart. Meaning, from line 100 you cannot go back and relate a line 10 node, unless you start maintaining your own objects, relations etc. (which is not what you want, i guess)
Using Java Beans the object hierarchy is maintained and because of that it is easy for you to relate them between different objects at different nodes easily. But JAXB is comparatively slower than JAX.

SOAP Request / Response Comparison Tool

Typically an enterprise application consumes a number of webservices. It is likely that some of these webservices that are being consumed will be upgraded to a new version every month. Before consuming the new webservice , the consumer must do a thorough analysis of changes between old service and new service which will form the basis of impact assessment.
I have come across tools that compare the WSDL , however some of the services that the application consumes could be huge and the WSDL might have more than say 50 operations. The consuming application might be using say 10 operations. By using WSDL comparator , it could be a bit tedious to identify if a modified element is part of a any of the operations consumed. In order to do such an analysis, I usually generate the request and response using SOAP UI for each operation and use text comparison tools to identify the difference.
Is there a tool available for performing of operation-wise comparison when 2 WSDLs are provided as input?
WSDL is eventually an XML document. The XmlUnit can come very handy in this case. Its essentially a utility for unit testing the XML application which make heavy use of XML. In the most trivialized for, it has a Diff class which actually compares the XML as follows:
Diff myDiff = new Diff("</Original>", "</Modified>");
But as I said, this is very trivialized. Please explore this for more details.
Also there is XML diff utility from Oracle.
Not sure if you the operation-wise comparison is possible or not.
Service Registry
It sounds like you are describing service governance. Some of the functionality you are describing can be provided by a registry above the functions of finding and publishing services.
http://wso2.com/products/governance-registry/
http://www-03.ibm.com/software/products/us/en/wsrr/
Maybe you want to generate classes from wsdl files using Eclipse WSDL2Java plugin or Apache CXF instead of generating each operation request and response, in my idea it can facilitate your work. Then compare the new classes with old ones using Devart Code Compare. Am i right?

Recommended Java packages/jars to access and process RESTful web services

I want to access an external RESTFul Web service via Java, and use an open source package that processes the returned XML or Json result and creates object(s) from this data.
I know that there are many solutions out there for this, and I'd like to get your feedback on which one I should use.
For accessing the web services, I know that I can use packages such as apache HttpClient etc. but I'm sure that there are packages that wrap this and also take care of processing the returned data (i.e. creating java objects from the result).
Thanks,
Nina
Spring is great, but this is one case where there are higher-level libraries out there that make it even easier. See for example the clients that come along with JAX-RS implementations like the Jersey client and the CXF client. Some implementations can even provide clients through dynamic proxying if you have a service interface and resource classes available. This way, you hardly have to write any code at all.
Spring Rest Template is your friend.
Spring MVC has something called "RestTemplate" which can be used exactly for this.
http://aruld.info/resttemplate-the-spring-way-of-accessing-restful-services/
http://blog.springsource.com/2009/03/27/rest-in-spring-3-resttemplate/

interface-based Java/Groovy web services

In my experience, most distributed object technologies (RMI, CORBA, etc.) work something like this:
define a service interface
write an implementation of the interface
use a tool (rmic, IDL compiler, etc.) that generates code which enables a client to get a reference to an implementation of the interface given some endpoint (URL).
The important point is that the service interface is a shared contract that both the client and service must adhere to. I've had a look at metro, and it doesn't seem to follow this pattern.
I'm looking for alternative suggestions that do support this kind of interface-based web service development. Unfortunately, I'm required to use SOAP, so libraries that only support RESTful services are no good to me.
Ideally, I would like to follow a code-first, rather than a contract-first appeoach, i.e. I define the (Java) service interface and the WSDL is generated from that, rather than the other way around.
Solutions that support defining or implementing the service using Groovy (instead of Java) are particularly welcome.
Metro allows you to annotate a given method, put a hint or two about the endpoints in the servlet container configuration files, and then have the WSDL generated automatically on request.
This is very nice, and save you all the trouble of having to create a full WSDL for just exposing a method or two.
Metro is good (+1), but Apache CXF's Simple Frontend goes one step further: you don't have to annotate anything. It generates WSDLs, clients and servers from plain Java interfaces.

Categories

Resources