I recently worked on a migration from good-old servlets web-services project to a REST using Spring MVC. The main motivation for the migration was testability. Servlets made very hard to create a good test suite, not good enough for continious integration. On the other hand Spring MVC not only makes testing posible but enjoyable. I’ll briefly layout my strategy and learnings from that project.
Step 1: Create High-level Http tests
This step wil save you a lot of time in the long run. They will serve as a verification step.
For these tests I used RestAssured. It lets you test web-services from the client’s perspective. jsonPath makes it easy to verify responses.
Step 2: Transform Servlets to Controllers
Step 2a: Create a one Cotroller per Servlet
Copy and paste the content of get and post###
In this step pass the HttpSevletRequest and HttpServletResponse as parameters
Step 2c: Create appropriate return types and remove dependency to HttpServletResponse
Split Methods and return the appropriate Object Types
1234567891011121314151617
@RequestMqpping("hello")@ControllerpublicclassHelloWorldController{@RequestStatus(RequestStatus.OK)publicvoiddoGet(@RequestParamStringname,@RequestParamStringaction,HttpServletResponseresponse)throwsException{...PrintWriterwriter=response.getWriter();if("sayhello".equals(action)){writer.write("{message:\"Hello "+name+"\"}");}elseif("list".equals(action)){//write a list to writter or with Gson}...}}