Java 基于输入的动态选择方法

Java 基于输入的动态选择方法,java,spring-boot,runtime,Java,Spring Boot,Runtime,我想根据对URI的输入从服务类中选择方法 我有一个获取类别名称的控制器方法: @GetMapping("/{category}/ranking") public Map<String, Integer> getRankingForCategory(@PathVariable String category) { return categorizedBooksRanking.getRankingForCategory(category); } @Get

我想根据对
URI
的输入从服务类中选择方法

我有一个获取类别名称的控制器方法:

@GetMapping("/{category}/ranking")
    public Map<String, Integer> getRankingForCategory(@PathVariable String category) {
        return categorizedBooksRanking.getRankingForCategory(category);
    }
@GetMapping(“/{category}/排名”)
公共映射getRankingForCategory(@PathVariable字符串类别){
返回categorizedBooksRanking.getRankingForCategory(类别);
}
基于此,我想从我的5种方法中选择一种:

package bookstore.scraper.book.scrapingtypes;

imports...

import static bookstore.scraper.JSoupConnector.connect;

@Service
@Slf4j
public class CategorizedBookService {

    private final EmpikFetchingBookService empikBookService;
    private final MerlinFetchingBookService merlinFetchingBookService;

    @Value("${external.library.url.empik.romances}")
    private String romancesCategoryEmpikURL;

    @Value("${external.library.url.empik.biographies}")
    private String biographiesCategoryEmpikURL;

    @Value("${external.library.url.empik.crime}")
    private String crimeCategoryEmpikURL;

    @Value("${external.library.url.empik.guides}")
    private String guidesCategoryEmpikURL;

    @Value("${external.library.url.empik.fantasy}")
    private String fantasyCategoryEmpikURL;

    @Value("${external.library.url.merlin.romances}")
    private String romancesCategoryMerlinURL;

    @Value("${external.library.url.merlin.biographies}")
    private String biographiesCategoryMerlinURL;

    @Value("${external.library.url.merlin.crime}")
    private String crimeCategoryMerlinURL;

    @Value("${external.library.url.merlin.guides}")
    private String guidesCategoryMerlinURL;

    @Value("${external.library.url.merlin.fantasy}")
    private String fantasyCategoryMerlinURL;

    @Autowired
    public CategorizedBookService(EmpikFetchingBookService empikBookService, MerlinFetchingBookService merlinFetchingBookService) {
        this.empikBookService = empikBookService;
        this.merlinFetchingBookService = merlinFetchingBookService;
    }

    public Map<Bookstore, List<Book>> get15BooksFromRomanceCategory() {
        return get15BooksFrom(romancesCategoryEmpikURL, romancesCategoryMerlinURL);
    }

    public Map<Bookstore, List<Book>> get15BooksFromFantasyCategory() {
        return get15BooksFrom(fantasyCategoryEmpikURL, fantasyCategoryMerlinURL);
    }

    public Map<Bookstore, List<Book>> get15BooksFromCrimeCategory() {
        return get15BooksFrom(crimeCategoryEmpikURL, crimeCategoryMerlinURL);
    }

    public Map<Bookstore, List<Book>> get15BooksFromGuidesCategory() {
        return get15BooksFrom(guidesCategoryEmpikURL, guidesCategoryMerlinURL);
    }

    public Map<Bookstore, List<Book>> get15BooksFromBiographiesCategory() {
        return get15BooksFrom(biographiesCategoryEmpikURL, biographiesCategoryMerlinURL);
    }

    private Map<Bookstore, List<Book>> get15BooksFrom(String bookStoreEmpikURL, String bookStoreMerlinURL) {
        Map<Bookstore, List<Book>> bookstoreWith15CategorizedBooks = new HashMap<>();

        bookstoreWith15CategorizedBooks.put(Bookstore.EMPIK, empikBookService
                .get15BooksFromCategory(connect(bookStoreEmpikURL)));
        bookstoreWith15CategorizedBooks.put(Bookstore.MERLIN, merlinFetchingBookService
                .get15BooksFromCategory(connect(bookStoreMerlinURL)));

        return bookstoreWith15CategorizedBooks;
    }
}
package bookstore.scraper.book.scrapingtypes;
进口。。。
导入static bookstore.scraper.JSoupConnector.connect;
@服务
@Slf4j
公共类分类BookService{
私人最终EmpikFetchingBookService empikBookService;
私人最终MerlinFetchingBookService MerlinFetchingBookService;
@值(${external.library.url.empik.romans}”)
私有字符串romancesCategoryEmpikURL;
@值(${external.library.url.empik.biographies}”)
私人字符串传记分类;
@值(${external.library.url.empik.crime}”)
私有字符串crimecategorympikurl;
@值(${external.library.url.empik.guides}”)
私有字符串指南scategoryempikurl;
@值(${external.library.url.empik.fantasy}”)
私有字符串fantasyCategoryEmpikURL;
@值(${external.library.url.merlin.romans}”)
私人字符串Romancescategorymerlineur;
@值(${external.library.url.merlin.biographies}”)
私人字符串传记分类;
@值(${external.library.url.merlin.crime}”)
私有字符串crimeCategoryMerlinURL;
@值(${external.library.url.merlin.guides}”)
私有字符串指南scategorymerlinurl;
@值(${external.library.url.merlin.fantasy}”)
私人字符串幻想分类;
@自动连线
公共分类图书服务(EmpikFetchingBookService empikBookService、MerlinFetchingBookService MerlinFetchingBookService){
this.empikBookService=empikBookService;
this.merlinFetchingBookService=merlinFetchingBookService;
}
公共地图从RomanceCategory()获取15本书{
返回Get15Bookfrom(romancesCategoryEmpikURL,romancesCategoryMerlinURL);
}
公共地图从FantasyCategory()获取15本书{
返回Get15Bookfrom(fantasyCategoryEmpikURL、FantasyCategoryMerlinur);
}
公共地图从CrimeCegory()获取15本书{
返回get15BooksFrom(crimecategorympikurl,crimeCategoryMerlinURL);
}
公共地图从Guidescategory()获取15本书{
返回get15BooksFrom(guidesCategoryEmpikURL、guidesCategoryMerlinURL);
}
公共地图从传记类别()获取15本书{
返回Get15 Books from(传记类别Empikurl,传记类别Empikurl);
}
私有映射get15BooksFrom(字符串bookstorempikurl、字符串bookStoreMerlinURL){
MapBookstorewith15CategorizedBooks=newHashMap();
拥有15个分类图书的书店。put(Bookstore.EMPIK,empikBookService
.get15BooksFromCategory(connect(bookStoreEmpikURL));
拥有15个分类图书的书店。put(Bookstore.MERLIN,merlinFetchingBookService
.get15BooksFromCategory(connect(bookStoreMerlinURL));
归还15本分类图书的书店;
}
}
因此,在另一项服务中:

public Map<String, Integer> getRankingForCategory(String category) {
        ....

        Map<Bookstore, List<Book>> bookstoreWith15CategorizedBooks = categorizedBookService.get15BooksFromBiographiesCategory(); // here should be corresponding the method from CategorizedBookService`
...
}
公共地图getRankingForCategory(字符串类别){
....
Map bookstore with 15CategorizedBooks=CategorizedBooksService.get15BooksFromBiographiesCategory();//这里应该是对应于CategorizedBooksService的方法`
...
}
e、 g如果我把
传记/排名
放在最前面的方法应该出现

如果我把
crime/ranking
放进去,那么方法
get15BooksFromCrimeCategory
应该存在

有什么办法吗?在运行时选择方法