Archive

Archive for August, 2012

Sending objects to your server from jQuery using POST/PUT and accepting using Spring & Jackson

August 24, 2012 Leave a comment

I won’t cover the basics of how to do REST using jQuery and Spring/Jackson in this post, hopefully I’ll find the time to post a whole series about this, so for this post I’ll assume you’ve followed all the tutorials you found online (quite a few helpful ones, shouldn’t be a problem)

So now you have your app server setup, your controller setup, you’ve created a method that’s supposed to get the object, and a javascript that should send it, BUT it just doesn’t work.  I ran into this problem, here’s what I had setup:

I was already using $.ajax instead of $.getJSON so I could send PUT requests, which means I could influence all aspects of the request (getJSON doesn’t let you do that).  I was sending the following request:

$.ajax({
url: ‘/api/users/configuration/’,
type: ‘PUT’,
dataType: ‘json’,
data: {
  configuration: JSON.stringify(userConfiguration)
},
success: function(response) { 
}
});

where  userConfigurationwas an object holding key and value pairs, and looked like this after stringifying it: {“exposure”:6,”publicName”:1}

And I expected the following code to catch it and translate it correctly to an object:

@RequestMapping(value=”/configuration/”, method=RequestMethod.PUT)
public ResponseEntity<String> setConfiguration(
HttpServletRequest request,
@ModelAttribute(“configuration”) Configuration configuration, BindingResult result 
) {
//  … do something with it
}

Several things had to be fixed for this to work out:

first of all, my javascript had to change a bit, I had to add application/json to the request descriptions so that the server knows it needs to parse it as JSON, and I had to stop sending a key-value pair, I only had to send the object itself (so remove the configuration: part)

This made my js code look like this:

    $.ajax({
    url: ‘/api/users/configuration/’,
    type: ‘PUT’,
    dataType: ‘json’,
    contentType: “application/json; charset=UTF-8”,
    data: JSON.stringify(userConfiguration),
    success: function(response) { 
    }
    });

Next, my request handling needed to change:

I was using @ModelAttribute, which may be good for any number of things, BUT it isn’t good for what I was trying to do.  I had to use @RequestBody instead.  Also, I was passing a domain object called Configuration, but didn’t really need the whole object, just the items map part of it, so I ended up just sending that.

so my server side code changed to:

@RequestMapping(value=”/configuration/”, method=RequestMethod.PUT)
public ResponseEntity<String> setConfiguration(
HttpServletRequest request,
@RequestBody Map<String, Integer> items
){
// do something interesting with this input
}

This was tricky to solved, I looked at quite a few tutorials, questions, etc., but it took some messing about to get it all in place.  This post right here is what finally sorted it all out, you might want to go visit that guy’s post as well.

Advertisements

Post 5 – The basics – Software for SAAS development

August 20, 2012 1 comment

The following is a list of programs I’ve played with.  It is by no means a complete list of the software you’ll need, simply describes some of the stuff I’ve looked at.

 

SVN repository: Subversion source control, used to manage your code versions.  Your life can not be complete without having one of those, be it SVN or Git (see a separate post on the topic)
Tortoise SVN client: A very good and free subversion client, don’t look anywhere else.  I know there’s one for windows, don’t remember seeing one for Unix.
Maven 2: THE right way to manage your java dependencies, run your tests, and even manage the whole product packaging life cycle.  Downloading JAR’s yourself takes time, you need to look for them, and upgrading a version is a pain, not to mention managing the different dependencies.  With maven, you simply say what your project needs, and maven takes care of it.  It’s as simple as that.  If you’re building a product from scratch, don’t even think of working any other way.
Maven Artifactory: used to host maven artifacts.  When maven builds your war, you can host it in your artifactory for easy access by developers, production, or other projects using maven which depend on your project.
Tomcat: one of the best application servers out there.  Very light weight, works on all OS’s, old and mature, and constantly under development.
Jetty: Another very good app server, I use it mainly as a maven plugin because of a very nice feature it has of real time resource reloading, which means I can change a JSP or even a class and it will immediately reload it and reflect my changes. 
Apache web server (httpd): One of the best Web servers out there, I’d say it’s the Tomcat equivalent of a web server (some will say it is the other way around, but w/e).  The question of whether or not to use a web server as an app server front end is too deep for this blog post, but if you do decide to use one, Apache is one of the two top options.
ngineX: The other top web server on the market, said to be x50 faster than Apache.  It is still rather new, but quickly making its way up, with usage levels raising.  I haven’t looked at the statistics but if the trend continues it will eventually take over and become #1.
Teamcity: A Continuous Integration server by Jetbrains (if the name sounds familiar it should, it’s the same guys who made Idea).  Considered to be one of the best of its kind, it is free to use for small startups with limited deployment requirements, and will cost about $8k for standard use.
Jenkins: Built from the ruins of Hudson, the open source Continuous Integration project taken over by Oracle after their Sun acquisition, this is considered the best open source CI project, used widely by many.  A while back I had to choose between Jenkins and Teamcity, we did a wide research  on the subject (also looked at Bamboo, from Atlassian, the guys who also make Jira).  We finally went with Teamcity after realizing that support costs for Jenkins will be around the same price that Teamcity costs, and Teamcity is a little bit better in some ways, with a better interface and smoother integration with Jira.  But honestly, if you don’t need the full support package, Jenkins has all you need to get going, and it’s free.
MySQL DB: well, not much to say here, free, small, easy to use and manage using PhpMyAdmin.  Unless you want to spend millions on Oracle, this is a very good choice for SQL based databases.
PhpMyAdmin for MySQL administration: Used for administrating MySQL.
Eclipse: The best open source IDE out there, with an incredible number of plugins and fully featured to the level that it is a fair competitor to Idea, the #1 IDE on the market made by Jetbrain.  See a separate post on this subject.
Idea, Community Edition: For those of us wanting to save some $$$, this version of Idea has almost all you need, the limited functionality which makes it a little bit of a problem to use for development of certain types of applications is server support (can’t officially run it from Idea, you need to do it on your own, although you can run Tomcat as a standard program (not as a “server”) and so if you use maven or ant to deploy your war and configure it correctly you can have it run within Idea, and even debug.  Haven’t found a way to overcome the JSP limitation, so if you need that one you’ve got a problem.   If you want to buy the full version, they have a “personal” license for 200$, which is more affordable than the 500$ they ask for  in their so called “company” license.
I’m a big fan of Idea, can’t say I’ll spend 200$ on it for now though.  Perhaps when I get some funding 🙂
Puppet Enterprise 2.5: For those of us fortunate enough to need deployment to a large number of servers, PuppetLabs has a fine product for automated deployment.  I know there are open source products out there which are pretty good, haven’t done the research on that one yet.
SpringI’m not going to talk too much about Spring here, there are very good resources out there and springsource have a fine web site you can explore.  
Besides, I have a feeling I’ll be talking about it extensively in future posts.  For now I’ll just say that if you’re building something from scratch, taking Spring as your container will make life simpler and force you to build your code in a way that is organized and supportable in the future.
Tiles 2: Tiles (2) is a framework for building web pages from fragments, so for example you can have a header, a body, and a footer each in their own file, and within each you can inject different content based on what you need.  I have a feeling I’ll post something on this subject later, since I’ve done my research trying to decide between Tiles, Spring MVC, and Struts.  I eventually picked Tiles 2, more on that later.
jQuery: A very popular Javascript library built to make life easier when dealing with the different browsers on the market.  Writing browser compatible javascript code is difficult, and requires extensive testing, if you can limit yourself to a 3rd party library which promises full browser compatibility, you’re set.  It also has a very wide range of plugins to do really cool stuff.
MongoDB: The trend nowadays is moving away from SQL databases into the so called “No-SQL” DB’s.  Mongo is one of those light-weight No-SQL databases which lets you store your data as a “document”, actually in a JSON format.  Compared to its size, this database has really strong capabilities.
Logback: Once upon a time there was log4j, a very popular logging framework for java.  Then the creator of this framework came up with slf4j, an interface for using logging engine in a standard way.  And finally there was Logback, the latest from this very capable developer, and what you should now use to do your logging in java.

Post 4 – The basics – Version control:Git vs. SVN

August 19, 2012 Leave a comment

I don’t want to open a comment war here, so I won’t say which is best, I’ll only say that I’ve used Subversion for the past 9 years, and it is a very good solution, but I’m using Git for my current adventure, and I’ll tell you why.

SVN is a service based solution, you need to install and maintain a central service if you want to store your code, and versions are stored on the hosting server.  So if want to be able to check diffs or check-in your code, you need to have access to the hosting service.

Git works differently, Git stores all your history locally in every repository, and every repository is a full clone of all data available (current and past code).  This means you can do checkins and diffs locally without access to a centralized service, meaning you can work offline, and if you want to do diffs it’s as fast as it can be since it’s all local disk access.

I’m no expert on neither solutions, but in my humble opinion Git suits smaller teams who need to work in separate locations, and want to be able to do many quick checkins per day, whereas SVN suits a centralized team where the server is hosted in a LAN and history searches and compare can benefit from LAN speed.  I know there’s also a difference in source control strategy between the two but I haven’t researched it enough to comment on it.

As far as online repositories are concerned, there’s Github if you want a centralized Git repository which everyone can sync to, and I’m sure you can find a good repository for Subversion or do it yourself in EC2.

Installing Git is as simple as a download, and SVN (service) is a little harder but manageable.  SVN client (tortoisesvn) is VERY good, there are good git clients out there from what I’ve heard, but I get all I need within Eclipse so I haven’t even tried to find one.

Bottom line: I chose Git because it fits my current needs, moving to SVN later is a feasible option if I ever feel the need.

Post 3 – The basics – Choosing an OS

August 19, 2012 Leave a comment

I’ve been a Windows consumer my whole life, I own three genuin versions of Win7, and I can safely say that my OS of choice would definitely NOT be Windows neither for Dev, nor for Production.  If you’re developing anything but .NET, Linux offers all that you need, and OSX (which is based on Free-BSD) is a great environment to develop in (although you can do just fine with Ubunto).  CentOS would currently be my OS of choice for production environment, but I’m no expert in Linux so I’ll leave that discussion to someone else for now.

The reason I’m saying all that, is that most open source projects fit well into Unix based systems, and while most have Win equivalents, it’s not quite the same. I can’t give you specifics off the top of my head, it’s more of a feeling I got while installing a long list of programs for playing around with (different DB’s, different CI environments, different App servers, etc.).  You can see that list in another post.

One thing I did not like in OSX is the lack of current Java version support.  OSX comes with Java built in, usually an older version, and you have to do some work in order to move up to a higher version (not the same as windows, where you simply click “install” and boom – you get a new version.

On the other hand, with OSX, you get a very comfortable Apple grade UI, and a Unix OS so you don’t have to install VM software to run your basic stuff on Linux.

As for production, in my previous job I’ve lead an effort to move our SAAS product from Windows to Unix, and our tests showed very good advantage of Unix over Windows, but it probably depends on what your product does.

Categories: R&D

Post 2 – The basics – Online hosting

August 19, 2012 Leave a comment

Well, I’m not a real expert at hosting solutions, but here’s what I’ve learned:

There are 4 possibilities when hosting online:

1. Buy cage space at any number of hosting spots.  They give you a cage, with electricity, network ,and air conditioning, and you have to do the rest (fill in hardware, cables, etc.).  You also get a service of a person who will go in and flip a power switch if you need it, so you don’t have to have someone on site).

I’ve been told that the costs start to balance out at around $1m, anything below that you should use the other options.  This is the cheapest solution if you have enough traffic and requirements to fill in the marginal cost, but is not for the average startup with $100k in the bank.

2. Use hosting solutions.  There are many, of all kinds, they vary in reliability and options, and are mid-range as far as costs are concerned.  A few months ago I would have said: use that if you want to get going as quick as possible, but now I know there are better options (see #4).  I guess the only reason to use that solution is if you are too big for option #3 (coming up), and too small for option #1.

3. Use PAAS such as EC2.  This is a very nice option, gives you endless scaling capabilities and easy deployment of (almost) any kind of environment, and best of all: you pay only for what you use, for as long as you use it.  You need 1000 Linux computers for the next 30 minutes?  setup one, duplicate 1000 times, and there you go.  Will cost you a couple of hundred bucks, but that’s it.  Amazing!  By the way, Amazon has free hosting for small environments so if you only want to play around, you don’t pay anything.  Haven’t played around with Azure, but I assume it works the same.

As far as I know this is the most expensive option, if you need an online solution which is constantly available and has high traffic and data requirements, I think you’ll balance out with option #2 in no time, and at some point will obviously get to #1 as well.

4. PAAS Level 2 (Cloudbees, Google apps engine, and the likes).  This is the best solution possible for startups at their early stages in my opinion.  I’ve been using Cloudbees for the past 2 months now, COMPLETELY FREE, and their solution rocks.  You get code hosting (I’m using GIT but you can also use SVN), as soon as you checkin your code and push it upstream to their repository (built into Eclipse if you use it), your code is copied over to Jenkins (I’ll get to CI in another post), and if all tests pass, your application is deployed in the cloud (on their app servers, I think it’s Tomcat).  You get access to a couple of DB’s (I’ve used MySQL and MongoDB) so you can save data, they have different monitoring solutions to complement, so in the end, you get a complete Continuous Deployment solution, with minimal effort and knowledge.

Just to be clear, I don’t work at Cloudbees, nor do I have any stocks, I just think what they did is absolutely phenomenal.  You can look at a youtube video which shows from zero to CD in 30 minutes using Cloudbees right here.  Check it out, it’s well worth your time.

Post 1 – The basics – Eclipse vs. Idea

August 19, 2012 1 comment

Having worked with Idea (by Intellij) for a long long time I have to admit that I’m a big fan.  They’ve managed to conjure up every single useful feature you could ask for, and excluding v. 7 & 8 their IDE is very well behaved, works fast and responsive even in big  projects (the one I worked on had over 1m lines of code).

However, their personal license costs 200$, and community version has some limitations (I’ve looked at v.11) especially with running external servers and with debugging JSP’s, which made Eclipse a better choice for me at this stage (early startup stages, no extra money to spend).

I was a bit fearful of using Eclipse and the IDE did not feel intuitive at first, but having played around with it for the past couple of months now I know what to expect and can say that, while not as comfortable as Idea, it is still a valid choice.

Bottom line: choose Eclipse if you can’t afford the 200$, you’ll get used to it eventually and won’t miss Idea’s extra features that much (aside for ALT-F7, the ever so useful find-usages, which has only a slower alternative in Eclipse called Show-Hierarchy)

Categories: R&D Tags: , ,

Hello world!

August 15, 2012 Leave a comment

This site will be used to share the knowledge I find around the web, and insights I get while developing my startup.

As there’s a lot of knowledge floating around, and much time is spent on picking the good from bad, relevant from old, good practice from bad, I thought I’d save you folks some time and share what I learn.

Hope you like this site, and if you like what you get and appreciate it, start your own site and share your knowledge freely!

Amir

Categories: Random stuff