Home Spring Refactor common controller redirection codes
Reply: 1

Spring Refactor common controller redirection codes

Borgy Manotoy
1#
Borgy Manotoy Published in 2018-01-09 06:04:28Z

I have some redirection codes in my Controller classes that I want to refactor. These codes just check if an object is null, and if it is... it would redirect it to a particular page.

Below are parts of the code:

@RequestMapping(value = "/user-details", method = RequestMethod.GET)
public ModelAndView userDetails(@RequestParam("id") int id) {

    ...

    User user = userService.getUserById(id);
    if (matter == null) {
        return new ModelAndView("redirect:/users-list.html");
    }

    ...
}

@RequestMapping(value = "/user-application-details", method = RequestMethod.GET)
public ModelAndView userApplicationDetails(@RequestParam("applicationId") int applicationId) {

    ...

    UserApplication application = userApplicationService.getApplicationById(applicationId);
    if (application == null) {
        return new ModelAndView("redirect:/users-list.html");
    }

    ...
}

@RequestMapping(value = "/user-activity-details", method = RequestMethod.GET)
public ModelAndView userActivityDetails(@RequestParam("activityId") int activityId) {

    ...

    UserActivity activity = userActivityService.getActivityById(activityId);
    if (activity == null) {
        return new ModelAndView("redirect:/users-list.html");
    }

    ...
}

I would like to refactor the null checking and redirection to a separate class, but I am not sure how I could call it inside the controller and would do the redirection to the specified page.

Synch
2#
Synch Reply to 2018-01-11 14:49:14Z

Checking whether a variable is present inside a controller is fine. You can improve on the syntax by using the Java 8 Optional API.

What you can do is move the exception handling to a separate class. Spring has a special component annotation exactly for this use case: @ControllerAdvice accompanied with @ExceptionHandler methods. This does require you to throw exceptions.

For an example, see:

@Controller
public class SampleController {

    @RequestMapping(value = "/user-details", method = RequestMethod.GET)
    public ModelAndView userDetails(@RequestParam("id") int id) {

        ...

        Optional<User> userOptional = userService.getUserById(id);
        User user = userOptional.orElseThrow(() -> new UserNotFoundException("message here"));

        ...
    }
}

@ControllerAdvice
public class SampleControllerAdvice {

    private static final String REDIRECT_USER_LIST = "redirect:/users-list.html";

    @ExceptionHandler
    public String handleUserNotFound(UserNotFoundException e, RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("errorMessage", e.getMessage());
        return REDIRECT_USER_LIST;
    }

    @ExceptionHandler
    public String handleUserActivityNotFound(UserActivityNotFoundException e, RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("errorMessage", e.getMessage());
        return REDIRECT_USER_LIST;
    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318262 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO