3

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);
}

1 Answer 1

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's a good idea. I didn't know that I can add multiple values ​​to the RequestMapping

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.