Archive for Java

RESTful

Anyone who’s worked on a REST API for any length of time will hear people talk about being RESTful. RESTful is simply trying to abide by REST standards. People will often argue about whether to use PUT or POST in a given situation. Usually there’s a right answer but not always.

GET it?

One grey area I’ve come across is GET requests with a lot of query parameters. If we’re trying to be RESTful then a GET request shouldn’t have a body. Also, if we need to pass some criteria to a GET request then it’s usually done via query parameters right? (e.g. http://www.mysite.com/?myParam=myvalue). Now, what happens if we have lots of criteria, say a list of things that need to be included in a DB search for that particular GET to return the right info. Depending on backend implementation this might be fine, however certain frameworks will vomit because they have a maximum URI size. If you tried the above with a Java service using Spring REST you will likely get this error:org.springframework.web.client.HttpClientErrorException: 414 Request-URI Too Long

OK my code breaks, now what?!

There’s debate about this online however I like this answer on Stack Oveflow. TLDR; send a POST. The commenter goes into more detail but essentially if we think of the query as a resource that’s being created then we still please the REST gods while essentially performing a glorified GET.

If Sherlock Holmes was alive today (and non fictional) I'd wager he would be a great developer. The plan for this blog is to provide succinct and useful investigations into software topics, aimed at mainly backend developers, hobbyist data scientists and anyone with a curiousity for the technical.