0

I need to work on an enterprise legacy Java application that is developed in servlets & jsp's.Planning to convert this legacy app to a Single Page application using angular js & Spring MVC REST.

In the new development, AngularJS will be submitting the model object's (as JSON ) to Spring REST methods.

In the existing application there is a lot of code in servlets and classes (at least 2000 lines in 30 classes) written to get request parameter's using HttpServletRequest i.e., request.getParameter("name");

Is it possible to be able to inject/convert the model (JSON) object submitted by angularJs to Spring MVC REST methods into HttpServletRequest object, so that I need not change all the legacy code & classes?

Not considering to use the @RequestParam annotation in the method signature as the number of parameters are high.

pp_
  • 3,435
  • 4
  • 19
  • 27
Andy
  • 31
  • 1
  • 1
  • 3
  • does this help maybe? http://stackoverflow.com/questions/1548782/retrieving-json-object-literal-from-httpservletrequest – starcorn Mar 31 '16 at 18:53
  • if I understand correctly it's possible, but if you can provide some example of requested JSON and expected mapping to HttpServletRequest it will be helpful. – Pianov Apr 01 '16 at 06:17
  • @levgen - Mapping will be in similar lines as posted by Viral in the response. Is it possible to convert data from model i.e., "UIData" in example posted by VIral to HttpServletRequest? – Andy Apr 01 '16 at 14:28
  • One option is to retrieve the data from model object and set it into request as attributes : request.setAttribute("AttributeName",value) – Andy Apr 01 '16 at 19:24

1 Answers1

0

Whatever JSON Object you are defining like below and which you trying to pass backend

var dataToPass={
    name:"XYZ",
    id:12,
    selstat:[12,14]
};

For that, you need to create JAVA POJO with similar name like below

class UIData{
 private String name; // Getter and Setter
 private Integer id; // Getter and Setter
 private List<Integer> selstat; // Getter and Setter
}

And in Spring while defining mapping in controller, you need to use annotation @ResuestBody in argument like below

@RequestMapping(value ='/mappingURL', method = RequestMethod.POST)
        public @ResponseBody RsponseObject processData(@RequestBody UIData uiData, HttpServletRequest request) 
            throws MyException {

                // Process you data
            }

This will automatically convert JSON object into POJO instance.

Please go through Spring Documentation for more details.

And make sure content-type in header while passing is application/json along with Jackson library added in your dependency.

Viraj
  • 1,360
  • 3
  • 18
  • 38
  • Thanks Viraj for the response with code sample. I am trying to find a way to be able to convert data from model i.e., "UIData" in above example to HttpServletRequest , so that I need not change all the legacy code that is retrieving data from HttpServletRequest object. – Andy Apr 01 '16 at 13:51