您好,登錄后才能下訂單哦!
動(dòng)態(tài)發(fā)布接口
HTTP接口分為REST和SOAP2種方式,文中都涉及到,包含從動(dòng)態(tài)生成文件到編譯class再到裝載到spring容器和ws.Endpoint中。
REST風(fēng)格
方案:
1.提供java文件模板
2.讀取文件內(nèi)容
3.查庫修改生成java文件
4.通過JDK中的javax.tools.JavaCompiler動(dòng)態(tài)編譯成class
5.通過繼承java.net.URLClassLoader動(dòng)態(tài)加載class文件到內(nèi)存
6.通過獲取spring的ApplicationContext手動(dòng)把mapping注冊到RequestMappingHandlerMapping中完成動(dòng)態(tài)發(fā)布
過程:
1.模板文件根據(jù)業(yè)務(wù)自行配置(涉及公司機(jī)密,忽略)
2.讀取文件內(nèi)容,生成java文件,編譯class,加載class,發(fā)布接口
//動(dòng)態(tài)創(chuàng)建接口 @Override public Boolean createGenerate(String serviceName,Long interfaceId,String structrue) { try { //首字母大寫 serviceName = StringUtils.firstCharUpper(serviceName); //目錄路徑 Path directoryPath = Paths.get(outDirectory); // 如果目錄不存在 if (!Files.exists(directoryPath)) { //創(chuàng)建目錄 Files.createDirectories(directoryPath); } String controllerJava = serviceName + "Controller.java"; String autoJavaFile = outDirectory + controllerJava; //文件路徑 Path filePath = Paths.get(autoJavaFile); if (!Files.exists(filePath)) { //創(chuàng)建文件 Files.createFile(filePath); } else { logger.error("動(dòng)態(tài)創(chuàng)建接口錯(cuò)誤,文件已存在:"+autoJavaFile); return false; } // 讀取模板文件流 String javaFile = directory + "RestTemplateController.java"; String content = FileUtils.readFile(javaFile); //替換文件 content = replaceJava(content, serviceName, interfaceId,structrue); //寫入文件 Files.write(filePath, content.getBytes(charsetName)); String fullName = packageName + serviceName + "Controller"; //動(dòng)態(tài)編譯class JavaStringCompiler compiler = new JavaStringCompiler(); Map<String, byte[]> results = compiler.compile(controllerJava, content); //加載class Class<?> clzMul = compiler.loadClass(fullName, results); //獲取spring的applicationContext ApplicationContext applicationContext = SpringContextHelper.getApplicationContext(); //注冊接口到注冊中心 MappingRegulator.controlCenter(clzMul, applicationContext, create); } catch (Exception e) { logger.error("動(dòng)態(tài)創(chuàng)建接口錯(cuò)誤",e); return false; } return true; } /** * controlCenter(運(yùn)行時(shí)RequestMappingHandlerMapping中添加、刪除、修改Mapping接口) * @param Class 希望加載的類Class * @param ApplicationContext spring上下文 * @param type 1新增 2修改 3刪除 * @throws Exception * @throws IllegalAccessException * @Exception 異常對象 * @since CodingExample Ver(編碼范例查看) 1.1 * @author jiaxiaoxian */ public static void controlCenter(Class<?> controllerClass,ApplicationContext Context,Integer type) throws IllegalAccessException, Exception{ //獲取RequestMappingHandlerMapping RequestMappingHandlerMapping requestMappingHandlerMapping=(RequestMappingHandlerMapping) Context.getBean("requestMappingHandlerMapping"); Method getMappingForMethod =ReflectionUtils.findMethod(RequestMappingHandlerMapping.class, "getMappingForMethod",Method.class,Class.class); //設(shè)置私有屬性為可見 getMappingForMethod.setAccessible(true); //獲取類中的方法 Method[] method_arr = controllerClass.getMethods(); for (Method method : method_arr) { //判斷方法上是否有注解RequestMapping if (method.getAnnotation(RequestMapping.class) != null) { //獲取到類的RequestMappingInfo RequestMappingInfo mappingInfo = (RequestMappingInfo) getMappingForMethod.invoke(requestMappingHandlerMapping, method,controllerClass); if(type == 1){ //注冊 registerMapping(requestMappingHandlerMapping, mappingInfo, controllerClass, method); }else if(type == 2){ //取消注冊 unRegisterMapping(requestMappingHandlerMapping, mappingInfo); registerMapping(requestMappingHandlerMapping, mappingInfo, controllerClass, method); }else if(type == 3){ unRegisterMapping(requestMappingHandlerMapping, mappingInfo); } } } } /** * * registerMapping(注冊mapping到spring容器中) * @param requestMappingHandlerMapping * @Exception 異常對象 * @since CodingExample Ver(編碼范例查看) 1.1 * @author jiaxiaoxian */ public static void registerMapping(RequestMappingHandlerMapping requestMappingHandlerMapping,RequestMappingInfo mappingInfo, Class<?> controllerClass, Method method) throws Exception, IllegalAccessException{ requestMappingHandlerMapping.registerMapping(mappingInfo, controllerClass.newInstance(),method); } /** * * unRegisterMapping(spring容器中刪除mapping) * @param requestMappingHandlerMapping * @Exception 異常對象 * @since CodingExample Ver(編碼范例查看) 1.1 * @author jiaxiaoxian */ public static void unRegisterMapping(RequestMappingHandlerMapping requestMappingHandlerMapping,RequestMappingInfo mappingInfo) throws Exception, IllegalAccessException{ requestMappingHandlerMapping.unregisterMapping(mappingInfo); }
結(jié)果:
可以正常發(fā)布spring接口,動(dòng)態(tài)生成文件注入mapping到spring接口中。
SOAP風(fēng)格
方案:
1.提供java文件模板
2.讀取文件內(nèi)容
3.查庫修改生成java文件
4.通過JDK中的javax.tools.JavaCompiler動(dòng)態(tài)編譯成class
5.通過繼承java.net.URLClassLoader動(dòng)態(tài)加載class文件到內(nèi)存
6.通過javax.xml.ws.Endpoint的publish動(dòng)態(tài)發(fā)布接口
過程:
1.模板文件根據(jù)業(yè)務(wù)自行配置(涉及公司機(jī)密,忽略)
2.讀取文件內(nèi)容,生成java文件,編譯class,加載class,通過Endpoint發(fā)布接口
@Override public Boolean createGenerate(String serviceName, Long interfaceId, String structrue) { try { serviceName = StringUtils.firstCharUpper(serviceName); Path directoryPath = Paths.get(outDirectory); // 如果文件不存在 if (!Files.exists(directoryPath)) { Files.createDirectories(directoryPath); } String controllerJava = serviceName + "Controller.java"; String autoJavaFile = outDirectory + controllerJava; Path filePath = Paths.get(autoJavaFile); if (!Files.exists(filePath)) { Files.createFile(filePath); } else { logger.error("動(dòng)態(tài)創(chuàng)建接口錯(cuò)誤ws,文件已存在:" + autoJavaFile); return false; } String wsJavaFile = directory + "JwsTemplateController.java"; String content = FileUtils.readFile(wsJavaFile); content = replaceJava(content, serviceName, interfaceId, structrue); Files.write(filePath, content.getBytes(charsetName)); String fullName = packageName + serviceName + "Controller"; JavaStringCompiler compiler = new JavaStringCompiler(); Map<String, byte[]> results = compiler.compile(controllerJava, content); Class<?> clzMul = compiler.loadClass(fullName, results); publish(clzMul, serviceName); } catch (Exception e) { logger.error("動(dòng)態(tài)創(chuàng)建接口錯(cuò)誤ws", e); return false; } return true; } //動(dòng)態(tài)發(fā)布接口 private void publish(Class<?> clzMul, String serviceName) throws Exception { serviceName = firstCharLower(serviceName); Endpoint endpoint = Endpoint.create(clzMul.newInstance()); endpoint.publish(wsDomain + serviceName); //redisUtil.set(serviceName, endpoint); endpointMap.put(serviceName, endpoint); }
結(jié)果:
可以正常發(fā)布SOAP接口,動(dòng)態(tài)生成文件發(fā)布SOAP接口。
后面附件會(huì)上傳動(dòng)態(tài)生成需要的工具類,需要的小伙伴可以下載,記得好評!
author:賈小仙
time:2018/9/5
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。