JAVA在不確定具體 Annotation 類型時,獲得註解參數

來源:互聯網
上載者:User

標籤:access   comm   new   技術   methods   rest   lte   private   start   

  
package com.lzw.demo;@SpringBootApplicationpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }    @RestController    @RequestMapping(path = "/a")    public static class A {        @GetMapping(path = "/find")        public void add() {            System.out.println("find");        }        @DeleteMapping(path = "/delete")        public void delete() {            System.out.println("delete");        }    }    @RestController    @RequestMapping(path = "/b")    public static class B {        @PutMapping(path = "/update")        public void update() {            System.out.println("update");        }        @PostMapping(path = "/add")        public void add() {            System.out.println("add");        }    }    /**     * Created by laizhenwei on 2018-1-29 22:48:49.     */    @Component    public static class appStartUp implements CommandLineRunner {        @Autowired        private RequestMappingHandlerMapping requestMappingHandlerMapping;        @Override        public void run(String... strings){            Class<Annotation>[] annoClz =                    new Class[]{RequestMapping.class, PostMapping.class,                            GetMapping.class, DeleteMapping.class, PutMapping.class};            Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();            List<Class<?>> beanTypes = map.keySet()                    .stream().filter(info -> map.get(info)                            .getBeanType().getAnnotation(RequestMapping.class) != null)                    .collect(Collectors.toList())                    .stream().map(i -> map.get(i).getBeanType())                    .collect(Collectors.toList());            beanTypes.forEach(t -> Arrays.stream(t.getMethods()).forEach(method ->                    {                        for (int i = 0; i < annoClz.length; i++) {                            Annotation mappingAnno = method.getAnnotation(annoClz[i]);                            if (mappingAnno != null) {                                try {                                    InvocationHandler invocationHandler = Proxy.getInvocationHandler(mappingAnno);                                    Field value = invocationHandler.getClass().getDeclaredField("memberValues");                                    value.setAccessible(true);                                    Map<String, Object> memberValues = (Map<String, Object>) value.get(invocationHandler);                                    System.out.println(Arrays.toString(((String[]) memberValues.get("path"))));                                } catch (Exception ex) {                                    ex.printStackTrace();                                }                            }                        }                    }            ));        }    }}

 

 

 

JAVA在不確定具體 Annotation 類型時,獲得註解參數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.