0

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);
    }
7
  • why do you want to do that ? Commented Nov 5, 2020 at 20:52
  • Are there any HTML files in this legacy Spring MVC project ? The return "viewPage"; indicates that a view (HTML-template) is filled with the previously prepared model (that's "MVC"). What is your plan to change / refactor? Commented Nov 5, 2020 at 20:54
  • There are HTML files but the frontend will be rebuild using angular. I just need to refactor my controller to return json instead of model views. Commented Nov 5, 2020 at 20:57
  • @ggr because i want the controller to return only responseEntity (json object) so it can be consumed by angular in the frontend. Commented Nov 5, 2020 at 21:13
  • just return your object, a bean, DTO Commented Nov 5, 2020 at 21:17

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Regex cannot be used in your context. There are certainly too many special cases to change, it is not just a question of changing a string. JavaParser is a good candidate.

1 Comment

That what i have found thnks for confirming that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.