From 326bcfbf9e47b74fc83153ff95cd48cc69285986 Mon Sep 17 00:00:00 2001 From: yutou Date: Wed, 23 Jun 2021 16:08:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86=E6=89=AB=E6=8F=8Fur?= =?UTF-8?q?l=E7=9A=84=E5=B7=A5=E5=85=B7=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nas/Controllers/MusicController.java | 23 ++++--- .../Controllers/MusicFavoritesController.java | 13 ++-- .../java/com/yutou/nas/Controllers/QQBot.java | 7 +-- .../nas/bangumi/AnimationController.java | 17 +++-- .../java/com/yutou/nas/other/TestMain.java | 15 +++++ src/main/java/com/yutou/nas/other/tools.java | 24 ++++++- .../java/com/yutou/nas/utils/AppTools.java | 62 ++++++++++++++++++- 7 files changed, 127 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/yutou/nas/other/TestMain.java diff --git a/src/main/java/com/yutou/nas/Controllers/MusicController.java b/src/main/java/com/yutou/nas/Controllers/MusicController.java index e15d3f5..8391660 100644 --- a/src/main/java/com/yutou/nas/Controllers/MusicController.java +++ b/src/main/java/com/yutou/nas/Controllers/MusicController.java @@ -25,13 +25,12 @@ import java.util.List; import static com.yutou.nas.Datas.AppData.defaultMusicPath; @Controller -@RequestMapping("/nas/music/") public class MusicController { @Resource MusicToolsServiceImpl musicTools; - @RequestMapping("all.do") + @RequestMapping("/nas/music/all.do") @ResponseBody public String getAllMusicList() { JSONObject json = new JSONObject(); @@ -42,7 +41,7 @@ public class MusicController { return json.toJSONString(); } - @RequestMapping("list.do") + @RequestMapping("/nas/music/list.do") @ResponseBody public String getMusicListOfPath(@RequestBody JSONObject body) { String path = body.getString("path"); @@ -65,7 +64,7 @@ public class MusicController { } @ResponseBody - @RequestMapping("getAlbum.do") + @RequestMapping("/nas/music/getAlbum.do") public String getAlbum(@RequestBody JSONObject body) { String album = body.getString("album"); JSONObject json = new JSONObject(); @@ -79,7 +78,7 @@ public class MusicController { } @ResponseBody - @RequestMapping("getArtist.do") + @RequestMapping("/nas/music/getArtist.do") public String getArtist(@RequestBody JSONObject body) { String artist = body.getString("artist"); JSONObject json = new JSONObject(); @@ -92,7 +91,7 @@ public class MusicController { return json.toJSONString(); } - @RequestMapping("reload.do") + @RequestMapping("/nas/music/reload.do") @ResponseBody public String reload() { JSONObject json = new JSONObject(); @@ -102,7 +101,7 @@ public class MusicController { return json.toJSONString(); } - @RequestMapping("find/file.do") + @RequestMapping("/nas/music/find/file.do") @ResponseBody public String findFile(@RequestBody JSONObject body) { String path = body.getString("path"); @@ -120,7 +119,7 @@ public class MusicController { return json.toJSONString(); } - @RequestMapping("getlocalhost.do") + @RequestMapping("/nas/music/getlocalhost.do") @ResponseBody public String getLocalHost() { JSONObject json = new JSONObject(); @@ -133,7 +132,7 @@ public class MusicController { return json.toJSONString(); } - @RequestMapping(value = "web/image.do", produces = MediaType.IMAGE_JPEG_VALUE) + @RequestMapping(value = "/nas/music/web/image.do", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public byte[] getImage(String fileName, String type) { JSONObject json = new JSONObject(); @@ -146,7 +145,7 @@ public class MusicController { return getImage(json); } - @RequestMapping(value = "image.do", produces = MediaType.IMAGE_JPEG_VALUE) + @RequestMapping(value = "/nas/music/image.do", produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public byte[] getImage(@RequestBody JSONObject body) { String fileName = body.getString("fileName"); @@ -172,7 +171,7 @@ public class MusicController { return null; } - @RequestMapping("random.do") + @RequestMapping("/nas/music/random.do") @ResponseBody public String random() { List list = musicTools.getMusicList(); @@ -192,7 +191,7 @@ public class MusicController { } - @RequestMapping("play.do") + @RequestMapping("/nas/music/play.do") public ResponseEntity play(String filePath, String random) { String _filePath; boolean _random; diff --git a/src/main/java/com/yutou/nas/Controllers/MusicFavoritesController.java b/src/main/java/com/yutou/nas/Controllers/MusicFavoritesController.java index 9a1242f..0112fa8 100644 --- a/src/main/java/com/yutou/nas/Controllers/MusicFavoritesController.java +++ b/src/main/java/com/yutou/nas/Controllers/MusicFavoritesController.java @@ -21,7 +21,6 @@ import java.util.List; * 收藏夹相关 */ @Controller -@RequestMapping("/nas/music/favorite/") public class MusicFavoritesController { @Resource MusicFavoritesDao favoritesDao; @@ -30,7 +29,7 @@ public class MusicFavoritesController { @Resource MusicDataDao musicDataDao; - @RequestMapping("dir/add.do") + @RequestMapping("/nas/music/favorite/dir/add.do") @ResponseBody public String addFavoriteDir(@RequestBody JSONObject body) { String favorite = body.getString("favorite"); @@ -50,7 +49,7 @@ public class MusicFavoritesController { return json.toJSONString(); } - @RequestMapping("dir/list.do") + @RequestMapping("/nas/music/favorite/dir/list.do") @ResponseBody public String getAllFavoriteDir() { JSONObject json = new JSONObject(); @@ -70,7 +69,7 @@ public class MusicFavoritesController { return json.toJSONString(); } - @RequestMapping("dir/rename.do") + @RequestMapping("/nas/music/favorite/dir/rename.do") @ResponseBody public String renameFavoriteDir(@RequestBody JSONObject body) { int id = body.getInteger("id"); @@ -89,7 +88,7 @@ public class MusicFavoritesController { return json.toJSONString(); } - @RequestMapping("add.do") + @RequestMapping("/nas/music/favorite/add.do") @ResponseBody public String addFavorite(@RequestBody JSONObject body) { JSONObject json = new JSONObject(); @@ -116,7 +115,7 @@ public class MusicFavoritesController { return json.toJSONString(); } - @RequestMapping("remove.do") + @RequestMapping("/nas/music/favorite/remove.do") @ResponseBody public String removeFavorite(@RequestBody JSONObject body) { JSONArray md5s = body.getJSONArray("data"); @@ -133,7 +132,7 @@ public class MusicFavoritesController { return json.toJSONString(); } - @RequestMapping("get.do") + @RequestMapping("/nas/music/favorite/get.do") @ResponseBody public String getFavorite(@RequestBody JSONObject body) { int fid = body.getInteger("fid"); diff --git a/src/main/java/com/yutou/nas/Controllers/QQBot.java b/src/main/java/com/yutou/nas/Controllers/QQBot.java index be1682f..2c56b71 100644 --- a/src/main/java/com/yutou/nas/Controllers/QQBot.java +++ b/src/main/java/com/yutou/nas/Controllers/QQBot.java @@ -14,15 +14,14 @@ import org.springframework.web.multipart.MultipartFile; import java.io.File; @Controller -@RequestMapping("/qq/bot") public class QQBot { - @RequestMapping("test.do") + @RequestMapping("/qq/bot/test.do") @ResponseBody public String test(){ return "123"; } - @RequestMapping("send.do") + @RequestMapping("/qq/bot/send.do") @ResponseBody public String send(String msg,String imgUrl){ JSONObject json=new JSONObject(); @@ -55,7 +54,7 @@ public class QQBot { } return json.toJSONString(); } - @RequestMapping("sendAndImg.do") + @RequestMapping("/qq/bot/sendAndImg.do") @ResponseBody public String botMsg(String msg,@RequestParam("file") MultipartFile file){ JSONObject json=new JSONObject(); diff --git a/src/main/java/com/yutou/nas/bangumi/AnimationController.java b/src/main/java/com/yutou/nas/bangumi/AnimationController.java index ecfa2b3..44b9388 100644 --- a/src/main/java/com/yutou/nas/bangumi/AnimationController.java +++ b/src/main/java/com/yutou/nas/bangumi/AnimationController.java @@ -18,7 +18,6 @@ import javax.annotation.Resource; import java.util.List; @Controller -@RequestMapping("anim/") public class AnimationController { @Resource BangumiListDao listDao; @@ -26,7 +25,7 @@ public class AnimationController { BangumiItemDao itemDao; @ResponseBody - @RequestMapping(value = "rss/data.do", method = RequestMethod.GET) + @RequestMapping(value = "/anim/rss/data.do", method = RequestMethod.GET) public String getAnimList(String key,String type,String team, int page) { boolean isNull = key.length() == 0; if(!StringUtils.isEmpty(type)&&type.length()>2) @@ -50,7 +49,7 @@ public class AnimationController { } @ResponseBody - @RequestMapping(value = "type/add.do", method = RequestMethod.POST) + @RequestMapping(value = "/anim/type/add.do", method = RequestMethod.POST) public String addRssType(String title) { BangumiList bangumiList = new BangumiList(); bangumiList.setTitle(title); @@ -63,7 +62,7 @@ public class AnimationController { } @ResponseBody - @RequestMapping(value = "type/list.do", method = RequestMethod.GET) + @RequestMapping(value = "/anim/type/list.do", method = RequestMethod.GET) public String getRssTypeList() { JSONObject json = new JSONObject(); json.put("code", 0); @@ -72,7 +71,7 @@ public class AnimationController { return json.toJSONString(); } @ResponseBody - @RequestMapping(value = "type/del.do", method = RequestMethod.POST) + @RequestMapping(value = "/anim/type/del.do", method = RequestMethod.POST) public String delType(int id){ BangumiItemExample example=new BangumiItemExample(); example.createCriteria().andBidEqualTo(id+""); @@ -85,7 +84,7 @@ public class AnimationController { } @ResponseBody - @RequestMapping(value = "rss/add.do", method = RequestMethod.POST) + @RequestMapping(value = "/anim/rss/add.do", method = RequestMethod.POST) public String addAnimationRss(String title, String author, String categories, String titleKey, String bid) { BangumiItem item = new BangumiItem(); item.setTitle(title); @@ -101,7 +100,7 @@ public class AnimationController { } @ResponseBody - @RequestMapping(value = "rss/list.do", method = RequestMethod.GET) + @RequestMapping(value = "/anim/rss/list.do", method = RequestMethod.GET) public String getAnimationRssList(String type) { JSONObject json = new JSONObject(); BangumiItemExample example=new BangumiItemExample(); @@ -112,7 +111,7 @@ public class AnimationController { return json.toJSONString(); } @ResponseBody - @RequestMapping(value = "rss/del.do", method = RequestMethod.POST) + @RequestMapping(value = "/anim/rss/del.do", method = RequestMethod.POST) public String delAnimation(int id){ int i=itemDao.deleteByPrimaryKey(id); JSONObject json=new JSONObject(); @@ -121,7 +120,7 @@ public class AnimationController { return json.toJSONString(); } @ResponseBody - @RequestMapping(value = "client/get.do", method = RequestMethod.GET) + @RequestMapping(value = "/anim/client/get.do", method = RequestMethod.GET) public String clientApi(){ AnimationData animationData=new AnimationData(); List list=itemDao.selectByExample(new BangumiItemExample()); diff --git a/src/main/java/com/yutou/nas/other/TestMain.java b/src/main/java/com/yutou/nas/other/TestMain.java new file mode 100644 index 0000000..4c8a608 --- /dev/null +++ b/src/main/java/com/yutou/nas/other/TestMain.java @@ -0,0 +1,15 @@ +package com.yutou.nas.other; + +import com.yutou.nas.utils.AppTools; + +import java.util.List; + +public class TestMain { + public static void main(String[] args) { + List urls=AppTools.getUrls("com.yutou.nas",null); + + for (String url : urls) { + System.out.println(url); + } + } +} diff --git a/src/main/java/com/yutou/nas/other/tools.java b/src/main/java/com/yutou/nas/other/tools.java index 8b90a60..adcdd4c 100644 --- a/src/main/java/com/yutou/nas/other/tools.java +++ b/src/main/java/com/yutou/nas/other/tools.java @@ -3,10 +3,19 @@ package com.yutou.nas.other; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.yutou.nas.NasApplication; +import com.yutou.nas.utils.AppTools; import com.yutou.nas.utils.DepotManager; import com.yutou.nas.utils.RedisTools; import com.yutou.nas.utils.Tools; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.GenericBeanDefinition; +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.filter.TypeFilter; import org.springframework.stereotype.Controller; +import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -16,7 +25,9 @@ import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; -import java.util.Enumeration; +import java.io.IOException; +import java.lang.annotation.Annotation; +import java.util.*; @Controller public class tools { @@ -124,5 +135,16 @@ public class tools { DepotManager.scan(); return "ok"; } + @ResponseBody + @RequestMapping("/public/test.do") + public String test(){ + List list= AppTools.scanClass("com.yutou.nas",Controller.class); + System.out.println("list size = "+list.size()); + for (Class aClass : list) { + System.out.println(aClass.getName()); + System.out.println(aClass.getAnnotation(RequestMapping.class)); + } + return "ok"; + } } diff --git a/src/main/java/com/yutou/nas/utils/AppTools.java b/src/main/java/com/yutou/nas/utils/AppTools.java index ab783c8..dce7359 100644 --- a/src/main/java/com/yutou/nas/utils/AppTools.java +++ b/src/main/java/com/yutou/nas/utils/AppTools.java @@ -1,7 +1,17 @@ package com.yutou.nas.utils; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.type.filter.TypeFilter; +import org.springframework.stereotype.Controller; +import org.springframework.util.ObjectUtils; +import org.springframework.web.bind.annotation.RequestMapping; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; import java.text.SimpleDateFormat; -import java.util.Date; +import java.util.*; import static com.yutou.nas.utils.RedisTools.processOut; @@ -39,4 +49,54 @@ public class AppTools { public static boolean isRuntimeSystemOfWindow() { return System.getProperty("os.name").contains("Windows"); } + + public static List scanClass(String classPath, Class annotation) { + List classList = new ArrayList<>(); + if (ObjectUtils.isEmpty(classPath)) { + return classList; + } + ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); + TypeFilter includeFilter = (metadataReader, metadataReaderFactory) -> true; + provider.addIncludeFilter(includeFilter); + Set beanDefinitionSet = new HashSet<>(); + // 指定扫描的包名 + Set candidateComponents = provider.findCandidateComponents(classPath); + beanDefinitionSet.addAll(candidateComponents); + + beanDefinitionSet.forEach(beanDefinition -> { + try { + Class clazz = Class.forName(beanDefinition.getBeanClassName()); + + if (!ObjectUtils.isEmpty(annotation)) { + if (!ObjectUtils.isEmpty(AnnotationUtils.getAnnotation(clazz, annotation))) { + classList.add(clazz); + } + } else { + classList.add(clazz); + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } +// System.out.println(definition.getBeanClassName()); + }); + return classList; + } + + public static List getUrls(String packageName,String className){ + List list= AppTools.scanClass(packageName, Controller.class); + List urls=new ArrayList<>(); + for (Class aClass : list) { + if(className!=null&&!aClass.getSimpleName().equals(className)){ + continue; + } + Method[] methods= aClass.getDeclaredMethods(); + for (Method method : methods) { + RequestMapping ls=method.getAnnotation(RequestMapping.class); + if(ls!=null) { + urls.add(ls.value()[0]); + } + } + } + return urls; + } }