Archive for the ‘Spring’ Category.
May 6, 2010, 8:34 am
A quick setup to upload files using SpringMVC.
Dependencies
I’m using Maven so the very first step is to add some extra dependencies: commons-io and commons-fileupload:
Application Context
Second step is to setup the application context to make usage of multipart feature:
Controller
Third step is to create your controller. Note that there is a new type here: MultipartFile as parameter.
View
And the last step: your JSP page.
January 2, 2010, 10:00 am

Recently I needed to work on a project where all the communication between fron and backend was done usin HTTP requests. At the beginning we considered to use Servlet with some helpers wich acted as a clue mapping the URL’s parameters to a Map object. Internally this Map was “injected” in all resource classes.
It was a solution to be considered but how it was the very first idea that the team had, we decided to discard it.
As required by the client we had to use Maven and Spring. However all others libraries we could decide and bear with it.
So, the second idea was to use RESTful simply to facilitate the job of mapping the parameters coming from the URL and the internal code.
We researched the first lib Jersey since it is the standard implementation of the JSR-311 we also researched the Restlet API.
We decided to go again the ideology to refuse he first option at this time. We studied both of them.
Both frameworks had unique aspects and a vast documentation. Each one with their singularities, for instance on Restlet it is necessary to configure the “routes” in a subclass of Application.
But the main reasons that made us to decide to use Jersey were:
- Be the standard implementation of the specification
- Less bureaucracy on the implementation of the resources
- Almost natural integration with Spring for classes and resources(Request, Response, ServletContext) injection.
This “walk through” has the objective to exemplify how to create a simple application using: Spring, Maven and Jersey made simple.
Setup
- Include the entry for the jersey-spring repository in the pom.xml file.
- Include the libs as dependencies: jersey spring
- Configure the web.xml file
It’s time do code!
- Configuring the Spring (applicationContext.xml)
Create applicationContext.xml file on META-INF directory of your app. This code tells to Spring engine to that com.mng.jerseydemo package is available for dependency injection. Follow a model of this file:
- Create the first resource.
At this snipped there are some annotations between then:
- @Path
This is the URL pattern that will be handled by the class (resource). It can be informed variables following the model: {valiableName}. For instance: @Path(”/user/{username}/{password}”).
- @Autowired
Injects an object from the variable type.
- @GET
Response request made via GET. There are also: @POST, @PUT and @DELETE that can be used at the same way then @GET.
- @Produces
Inform the return type of a method. This annotation can be informed for a method as well as for a class together with @Path.
If specified at the class all methods inside this one without a specific @Produces will follow the @Produces of the class.
- @PathParam
Translate mapped variables from a URL to an object. This will be mapped to a variable informed at the signature of the method.
Getting access
To get access to a resource just created you can use your own browser (Chrome or Firefox) with then is easy to test the “verb” GET.
However, if you are in the “X” world (Linux, Unix or even Mac OS) relax, take a cup of tea and use cUrl.
In a fast and simple way this is how you can use to test all the verbs available (GET, PUT, POST and DELETE) through the parameter: “-X” informing the method to be used.
And with the “-F” parameter you can specify the key value parameters for the POST method (@QueryParam).