I am designing a Java web application that makes heavy use of AJAX (for user experience – no reloads, etc.). I also have the need to expose a large number of web services because there will be many different clients (not just web browsers) connecting to the Java EE backend.
This got me thinking that it might just make sense for my AJAX XmlHttpRequests to somehow (magically) envelope a SOAP message and talk directly with my existing web servers, so I’m not writing duplicate code. Although I don’t really know how I’ll do that at this point, I know its possible because I’ve already found a few articles on doing just that.
I’m not concerned with whether or not I can have AJAX content post to a Java web service; I’m concerned with whether or not I should be doing this.
Is this a discouraged practice? Are there security vulnerabilities or performance issues I need to be aware of? It makes sense to do this from a reusability and system consistency point of view, but I’m neither an AJAX or Java EE expert to know any better here.
I guess I’m just interested in some educated opinions. And, as always, thanks!
I think that you are definitely on the right track: try to DRY and reuse your code wherever it is sensible. Your approach is – in my opinion – very good, so I just have a few remarks to maybe point you in the direction you would like to go anyway.
In my humble opinion, especially when you are dealing with AJAX, Json would be a much handier format for your data: your AJAX code can directly work with the supplied data structures, you save a lot of data overhead (especially if you are considering using the services a lot) and – but that is very subjective – Json is a much more elegant data format.
I don’t know what your other clients expect from your webservices, but if you are not fixed on the format of the messages being transmitted (although you did mention SOAP and XML), they could consume and produce Json, too.
A very handy framework to produce and consume Json is Jersey, if you are just looking into parsing and creating Json, I really like Gson from Google.
I would suggest you look into RESTful webservices, especially when combined with Java EE. If you are working with a Java EE 6 compliant server, creating RESTful webservices is very easy. They even can produce and consume different kinds of data formats – Json and XML for example.
Good luck! 🙂