JAX-WS, Eclipse, JBoss

 

Ok another Java post, they are few and far between, but I’ve already polluted this blog with objective-c, javascript and other non .net languages so why not.

So I was lying in bed last night my wife was hogging the windows machine watching some film or other, so I’d a choice between reading 50 shades of grey or firing up my mac book air, no contest there…

 
I recently interviewed a guy that had moved from Apache Axis to JAX-WS, the way he described it sounded a lot like WCF (windows communication foundation) so I wanted to see for myself.

 

  • Install Jboss 7.1.1 for an application server
  • Install Eclipse juno IDE for Java
  • Install Mono Develop (not necessary but i had this already for iPhone dev so thought what the heck I’ll use it for the client)

So what is JAX-WS? The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services. It is part of the Java EE platform from Sun Microsystems. Like the other Java EE APIs, JAX-WS uses annotations. Here’s how I created a sample one.

Ensure JBoss can run

Start the standalone shell script and check you can see http://localhost:8080 page below in your browser

clip_image001

Choose JavaEE perspective in Eclipse

clip_image002

Create a new project in Eclipse  (dynamic web)

clip_image003

Add the following webservice class

Complete with annotations

clip_image004

Modify web.xml

Add the highlighted section

clip_image005

Configure the Local JBoss server in eclipse

Right click on the server you added and choose Add/Remove

clip_image006

Add your deployment

clip_image007

Add your current deployment
clip_image008

Start Application Server

Click on the Play button in the server tab toolbar, you should be automatically switched to the Console pane in Eclipse. Take note that your DynamicTest war file is deployed.

clip_image009

Review the JBoss Admin Console

Specifically the Webservice Endpoints, You should see your webservice deployed here.

clip_image010

You can also browse to the wsdl

clip_image011

Create your client

I used C# with the Mono Develop IDE to create a simple Console Application

clip_image012

Just add a Webservice the way you would in Visual Studio (I went for .net 2.0 WS because the WCF version didn’t create an app.config for me (visual studio you spoil me)).

Run

clip_image013

And that’s it your first JAX-WS! (and not a windows machine in sight.. I feel dirty but I like it :-) )

 

 

=== UPDATE ===

Ok after reading a lot of blogs and a few weeks later i've found a nicer way of doing it.

Instead of editing the xml you can choose to add a new webservice and select your webservice class (note screens below are not for the same project but are functionaly the same

 

 

 

Customization of WebApi

 

If you’ve used Asp MVC Web Api then you are most likely familiar with the notion of content negotiation, this is the process where the content returned in the response is dictated by the accepts header in the request. In sort if you request xml you get xml back, if you request json you get json back.

This is done by what we call Formatters. You can of course add your own formatters, e.g. let’s say you have an application that returns human resource details; you may request back a profile picture by hitting the same route with a different request header.

 

OOB Formatters

Lets take a working example, of what we get out of the box (OOB).

Create a new Web Api project

Add the following class

image

Modify the ValuesController as follows

image

We now should be able to run the project and see the following in the browser

image

So by default we get back an xml formatted response, of course we could request json, but what if we just don’t want xml?

Tweaking the config

 

Add the following line to your Global.asax.cs

image

Now add this new GlobalConfig static class as follows

image

Run your application again

image

Now we get json by default, yah!

 

But wait a second, what if I know that I have some javascript developers that want to use this content, wouldn’t be nicer to offer camel casing to these guys?

image 

Run project again

image

e.g some simple jQuery

image