Showing posts with label rest service. Show all posts
Showing posts with label rest service. Show all posts

Wednesday, January 4, 2012

CXF Rest service renders JSON with @ for attributes

In my current project I am exposing lots of Rest Services in Java using CXF. All the service should be able to render response in json and xml format. This can very easily be done by just using a annotation(@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })) on the exposed service method. To verify the service is rendering correct response based on passed header type (accept:application/json or accept:application/xml).

I used JsonView firefox plugin. (I like this plugin since you get formatted JSON back vs one line response which you get if you will use Poster firefox plugin).

I noticed that JSON output has extra '@' symbol for attributes. i.e.

......"Programs": {"Channel": {
"
@ID"
: "E",
"TopName": "The Fresh Prince of Bel-Air",
"
BottomName"
: "Bang the Drum Ashley",
"StartTime": {
"
@utc"
: "1325741400000",
"
$"
: "2012-01-05T05:30:00"
},...........


Then after googling I found that CXF default JSON render is Jettison which produced this sort of output. (Why Jettison behaves in such a way is still mystery. I have to it figure out sooner than later.)

If you will replace the default JSON renderer Jettison with Jackson problem will be resolved and it' very simple to provide your custom Provider. I added following lines into my spring.xml

<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper">
<property name="serializationConfig.dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg value="EEE, d MMM yyyy HH:mm:ss z" />
</bean>
</property>
</bean>

<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider">
<property name="mapper" ref="jacksonObjectMapper" />
</bean>


and added the provider into my server declaration i.e
<jaxrs:server id="xService" address="/x">
<jaxrs:serviceBeans>
<ref bean="xServices" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider"/>
</jaxrs:providers>
</jaxrs:server&g
After adding Jackson as my provider. I redeployed my project and JSON was coming without @ sign. :))))

"programList": [{
"isHD": true,
"isClosedCaptioned": false,
"length": 120,
"description": "",
},
"startTime": {
"textValue": "2012-01-05T17:00:00",
"utcStartTime": 1325782800000 ....

-Enjoy
Manisha

Monday, October 17, 2011

Apache Benchmarking Tool Won

I wanted to do the performance test for my rest services and was looking for simplest tool which can send concurrent and sequential requests. Having prior experience with Grinder it become my obvious and first choice. Honestly it did the job but was not very simple, it took me
couple of hours to do the entire setup and then I came across AB (http://httpd.apache.org/docs/2.0/programs/ab.html) Tool.

Finding the URL to download was the toughest thing :). It took me 10 minutes to figure it out (http://www.apache.org/dist/httpd/binaries/win32/).
I know, I am dumb but once the took was installed it was as easy breeze.

I realized the tool is part Lion OS, you don't have to install it separately, so mac users can hit the ground running in no time vs Windows users have to download and set the environment variable and add that to path variable.

Once the Apache benchmark bin directory is in you path, you are ready to rock and roll.

Open the command prompt ( cmd) then type "ab" and hit enter, it will show you all the options. I used -n ( Number of request) and -c (Concurrent request).


Enjoy
Manisha