Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC architecture

I have three .jsp pages on my app (index, user and admin). And I have three controllers for everything page but in my administration and user controllers use duplicate methods. For example, I have a method "getGenres" (For shows all genres from DB) in admin controller and user controller. How can I combine these methods if "@RequestMapping" is different in controllers?

@Controller
@EnableWebMvc
@RequestMapping("admin")
public class AdminController {

@Autowired
private GenreTableService genreService;

@RequestMapping(value = "genres")
public ResponseEntity<Map<String, List<Genre>>> getGenres() throws ServiceException {

   Map<String, List<Genre>> genres = new HashMap<>(1);
   genres.put("genres", genreService.getAll());

   return new ResponseEntity<>(genres, HttpStatus.OK);
}
like image 538
cxFaust Avatar asked May 03 '26 19:05

cxFaust


1 Answers

You want to combine methods. this is sample code.

enter link description here

enter link description here

like this :

@Controller
@RequestMapping("/common")
public class AdminController {

     @Autowired
     private GenreTableService genreService;

     @RequestMapping(value = "/genres")
     public ResponseEntity<Map<String, List<Genre>>> getGenres() throws ServiceException {

         Map<String, List<Genre>> genres = new HashMap<>(1);
         genres.put("genres", genreService.getAll());

         return new ResponseEntity<>(genres, HttpStatus.OK);
     }
}

or

@Controller
public class AdminController {

    @Autowired
    private GenreTableService genreService;

    @RequestMapping(value = {"/admin/genres", "/user/genres"})
    public ResponseEntity<Map<String, List<Genre>>> getGenres() throws ServiceException {

       Map<String, List<Genre>> genres = new HashMap<>(1);
       genres.put("genres", genreService.getAll());

       return new ResponseEntity<>(genres, HttpStatus.OK);
    }
}
like image 131
0gam Avatar answered May 06 '26 12:05

0gam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!