Posts Tagged ‘jQuery’

Online service development in java – a beginner’s guide

November 4, 2012 1 comment

In the coming series of posts, I will share my development experience (code included) with creating an online service from scratch.  If you read through all posts, you should reach a point where you are capable of writing code and deploying it to a production like environment which is available online, using some of the latest (and greatest) open source libraries available in java.  You will definitely not be at the end of the journey, but will get a pretty good head start.

I’ve created a sample project, which basically does nothing (well, almost nothing), but it does so in a (semi) secure way, using several top libraries.  Basic service, is a service which lets you register, login, and… do nothing.  But, it does so using:

Spring, Maven, ESAPI (+AppSensor) for input validation, JSP’s (JSTL+Tiles 2), jQuery (+UI), Mongo DB, Logback, Jackson, Mockito (+PowerMock) for unit testing, and it does it in a RESTful way, using complete Data and Presentation separation (all JSP’s get their data through REST requests, meaning you can add easily add mobile support without almost any change to the  interface), and is fully internationalized (currently supports hebrew and english, meaning we have RTL languages covered).  The complete source code for Basic service is available on google code, it is a fully functional (and GPL open source) example of combining all of these technologies , which means that you can concentrate on the next steps rather than spend time building the foundation from scratch.  It took me about 3 months to go through all the baby steps in each of those technologies (after spending some time picking the right ones of course), I hope this series will save you at least some of that time.

Please keep in mind that I do not claim this to be a complete and bullet proof piece of software, there is still much to do in order to make it “production ready”.

Basic service is deployed on Cloudbees, using Continuous Deployment,  you’ll get some insights on how to do that as well.

One final note: the title says “a beginner’s guide”, but should be read with two different interpretations: one is (possibly) you, who is just getting started with creating an online service, and the other is me, sharing my experience with jumping into these deep water a couple of months ago.  If you like what you read, you’re welcome to comment, if you see me making horrible mistakes, constructive criticism is more than welcome.

Let’s move on.  The next post will cover the basics of setting up a functional development environment, one which will allow you to develop and instantly deploy your code to production so it is available to the rest of the world

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:

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:

    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.

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.