Issue
I have a legacy springMvc project and i want to refactor all the controllers to return ResponseEntity object rather then returning Model object which is returned by default.
I am looking for a way to map functions in Map class to ResponseEntity class .
I have seen people recommend to use Regular expression as a solution to refactor all the methods.
I need to know your opinion about implementing Regex as solution in terms of advantages / drawbacks for using regex in this scenario.
In addition it would be helpful if you can suggest other solutions.
please take a look at the attached code for more details.
return Model object to the view
@GetMapping("/getData")
public String getData(Model model) {
model.addAttribute("message", "springMvc");
return "viewPage";
}
return ResponseEntity object as a json format
@GetMapping("/getData")
public ResponseEntity<Map<String,String>> getData() {
Map<String,String> map = new HashMap<>();
map.put("message", "springMvc");
return new ResponseEntity.Ok().body(map);
}
Solution
In case this problem is faced by someone in the future i would like to share that I have found a convenient way to do the refactoring of the code by using AST (Abstract Syntax Tree) which gives you a lot more control over the code comparing to using Regex.
you can check the library repository :
Note that AST is used by IDEs and Static analysis tools such as SonarQube
Answered By - Cheikh Saadbouh