Latest Post

Heap sort code (but still bubble sort is my favorite)

It has been long since my college days that I used to hear that bubble sort is the worst sorting method and you should not use it. Theoretically its complexity was same as few others like insertion sort and selection sort that is O(n2) . But when you run it on machines and compute time … Continue reading

  • I am writing this just to help people if they are working on rest services and there are some issues which they will encounter and may spend a good amount of time figuring out the solution.And once you get the solution , you would not find it something very tricky, they are very simple stuff but still on the web they are not specified very well in a single place. So you search and serach and then combined different information to solve the puzzle.So Couple of issues which might help some REST guy somewhere in some corner of world looking for this solution 1.GZIP data in REST SERVICESI had to automate one of the Rest Services and the post method would accept the request data in zipped(Gzip) format.I was using Apache Httpclient. At first I thought it would be nothing and I would just call some method to set that data needs to be zipped before sending. But it was not the same way.I had to explicitly do the zipping stuff and make the entity as ByteArrayEntity. I am copying the code so that it can help. Here requestString is the body of the request.It will be converted into byte array and then returned. After that ByteArrayEntity is created from the byte Array and it is set into the httpPost like this I faced a lot of confusion as the service which was jersey based was throwing error as it was not recognizing the data. So had to set header Content-Type in the http request.I was using json format so I set it to application/json . Other header which I had to set was Content-Encoding to gzip. Also in case your service also return the data in the zipped format and you want to parse it , then you can use the following code 2.Alternative for sending body with the delete method call Next a very small thing that you may encounter is that somehow the delete may contain the body. In my case the service was recording the deleter information. But most the of the clients supports no entity attachement with DELETE call.So the alternate is to use X-HTTP-Method-Override header. Basically we need to send POST request and set the header with the method we would like to actually call. 3.Date Conversion Jersey was used for the REST service implementation. And jersey was using Jackson parser for request processing. The request contained of the date fields.One issue we faced was the automatic date conversion in the server's timezone by Jackson. So even if we sent 01-10-2010 it was converted into 31-09-20 10 since the server wasin PST timezone.We did not want that behavior.Our requirement was just to keep the data as it is in the request. Looked for the Jackson Parser documentation and found that a deserializer needs to be implemented.So for every date field in the request this class will take care of it.Code below: And then mark your field with this Deserializer Now we can handle the data in according to our requirement.Also you can write a Serializer like this also if you want to convert data in response to some specific format. Hope this post might prove helpful to somebody. Gaurav....