SpringBoot扫描注册注解

有个小需求是扫描在SpringBoot 注册的RequestMapping接口地址,可以先扫描所有使用Controller注解的类,然后获取他们的方法,判断使用的RequestMapping注解,再获取value,就能拿到接口地址了。下面为代码片段

    /**
     * 扫描使用注解的类
     * @param packageName 扫描包名
     * @param annotation 注解类
     * @return 扫描到的集合
     */
    public static List<class> scanClass(String packageName, Class annotation) {
        List<class> classList = new ArrayList&lt;>();
        if (ObjectUtils.isEmpty(packageName)) {
            return classList;
        }
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
        TypeFilter includeFilter = (metadataReader, metadataReaderFactory) -> true;
        provider.addIncludeFilter(includeFilter);
        Set<beandefinition> beanDefinitionSet = new HashSet&lt;>();
        // 指定扫描的包名
        Set<beandefinition> candidateComponents = provider.findCandidateComponents(packageName);
        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;
    }

    /**
     * 获取Url
     * @param packageName 扫描包名
     * @param className 指定类,如无指定类,为null即可
     * @return url集合
     */
    public static List<string> getUrls(String packageName,String className){
        List<class> list= scanClass(packageName, Controller.class);
        List<string> urls=new ArrayList&lt;>();
        for (Class aClass : list) {
            if(className!=null&amp;&amp;!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;
    }

闲来无事,吐槽下任职公司

入职这公司也快一年了,今天刚被老板怼了,实在忍不住来吐槽下公司的一些傻吊同事。

关于项目管理,极其混乱。单纯的想到啥就要啥,下完需求一般第二天就要,做完后也不使用,感觉白做。最近终于出了个时间排期表,但神特么的十几个需求在月中就要(虽然也不是做不完)。项目负责人甚至不知道前后端目前进度如何。

关于团队协作,后端猴子交流起来极其操蛋,也不知道他是真不懂业务还是不想麻烦自己,啥都不知道,作为前端的我向后端提出个需求:要求由前端提供参数在后端运算完后返回结果到前端(别问为什么这么做,“为了安全”)。结果后端愣是无法理解这个需求,在拖了几天后给了个“后端直接算出结果返回给前端”的东西,我参数都没给你,你算个鸡儿?

然后是运营那批人,他们老大的核心思想就是“凡是都是我们的产品问题,我们客户的技术绝对没问题”。好家伙,我这边查日志、反编译,都列出是对方哪一行代码出错了,还说是我在甩锅,说不出话.jpg。下需求的时候什么东西也不说清楚,上午下需求,下午就问进度,第二天就问能不能完成,我寻思我也没双头四臂两电脑啊。

总之就是看我每天准点下班,他们心里不平衡,天天去老板那边投诉我,然后老板今儿终于找我谈话,说我“态度不行、技术不行、喜欢甩锅”。我就纳闷了,对接出错我每次都能截图到对方报错的位置咋就技术不行了,态度不行就是看我天天准点下班不舒服?晚上12点微信打电话找我不接?

其实吧,很多细节上也不是做不到的,比如他们的需求按我正常速度,确实可以在不加班的情况下完成,以及很多对接细节我都可以直接插手解决的,但我实在无法认同项目负责人的态度和理想,什么“出了问题都是我们的原因”、乱定KPI目标、要求24小时在线。大兄弟,就我这工资,我加班一分钟都算是愧对自己好吧。我也不是不能加班,但得看公司来,之前某家公司项目确实紧张,老板人也挺好,我不照样加班一整个月。

结论上来说,近期准备提离职,回头在考虑下细节,有点不太想打工了。

花生壳Oray动态域名自动更新IP

前言:由于最近家里公网IP经常变动,自己又有个Oray的域名,所以想弄个动态IP更新上去,而Oray的花生壳服务现在要花钱(还限制端口),所以已经不考虑花生壳了

一、Oray设置

进入域名设置的 其他服务-自主管理,开启自主管理设置

二、关键URL API

1.登录

首先需要获取到登录token

def getToken():
    headers={
        "Authorization":"",
        "Accept":"application/json, text/plain, */*",
        "Content-Type":"application/json",
        "Host":"dm-api.oray.com",
        "Origin":"http://diy.oray.com",
        "Referer":"http://diy.oray.com/domain",
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
    }
    url="http://dm-api.oray.com/authorization" #登录url
    ret=requests.post(url,data='{"rootname": "自主管理域名", "password": "密码"}',headers=headers) #data是raw格式
    _json=json.loads(ret.text)
    return _json['token']

2.获取recordid

def getRecordItem ():
    url="http://dm-api.oray.com/resolves/自助管理根域名"
    headers={
        "Authorization":"Bearer {0}".format(token),
        "Accept":"application/json, text/plain, */*",
        "Content-Type":"application/json",
        "Host":"dm-api.oray.com",
        "Origin":"http://diy.oray.com",
        "Referer":"http://diy.oray.com/domain",
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
    }
    ret=requests.get(url,headers=headers)
    _json=json.loads(ret.text)
    for _item in _json:
        if(_item['name']=='你目标二级域名头'):
            return _item
recordid=getRecordItem()['recordid']

3.更新IP

def updateIp():
    url="http://dm-api.oray.com/resolves/你要修改的二级域名"
    headers={
        "Authorization":"Bearer {0}".format(token),
        "Accept":"application/json, text/plain, */*",
        "Content-Type":"application/json",
        "Host":"dm-api.oray.com",
        "Origin":"http://diy.oray.com",
        "Referer":"http://diy.oray.com/domain",
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
    }
    _json=json.loads('{"recordid":1,"domainname":"你要修改的二级域名","subname":"二级域名头","line":0,"recordtype":"a","value":"","ttl":3600,"priority":0}')
    _json['value']=ip #你要提交的ip
    _json['recordid']=getRecordItem()
    _data=json.dumps(_json)
    requests.post(url,data=_data,headers=headers)

以上就完成了,根据自己使用的不同语言进行http调用修改就行,放一份完整的Python代码仅供参考(假设要修改的二级域名:nas.example.cn)

import json
import requests
ip="127.0.0.1"
def getToken():
    headers={
        "Authorization":"",
        "Accept":"application/json, text/plain, */*",
        "Content-Type":"application/json",
        "Host":"dm-api.oray.com",
        "Origin":"http://diy.oray.com",
        "Referer":"http://diy.oray.com/domain",
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
    }
    url="http://dm-api.oray.com/authorization"
    post=requests.post(url,data='{"rootname": "example.cn", "password": "123456"}',headers=headers)
    _json=json.loads(post.text)
    return _json['token']
token=getToken()
def getRecordItem():
    url="http://dm-api.oray.com/resolves/example.cn"
    headers={
        "Authorization":"Bearer {0}".format(token),
        "Accept":"application/json, text/plain, */*",
        "Content-Type":"application/json",
        "Host":"dm-api.oray.com",
        "Origin":"http://diy.oray.com",
        "Referer":"http://diy.oray.com/domain",
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
    }
    ret=requests.get(url,headers=headers)
    _json=json.loads(ret.text)
    for _item in _json:
        if(_item['name']=='nas'):
            return _item
def updateIp():
    url="http://dm-api.oray.com/resolves/nas.example.cn"
    headers={
        "Authorization":"Bearer {0}".format(token),
        "Accept":"application/json, text/plain, */*",
        "Content-Type":"application/json",
        "Host":"dm-api.oray.com",
        "Origin":"http://diy.oray.com",
        "Referer":"http://diy.oray.com/domain",
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
    }
    _json=json.loads('{"recordid":0,"domainname":"nas.example.cn","subname":"nas","line":0,"recordtype":"a","value":"","ttl":3600,"priority":0}')
    _json['value']=ip
    _json['recordid']=getRecordItem()['recordid']
    _data=json.dumps(_json)
    requests.post(url,data=_data,headers=headers)
updateIp()