欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

動態(tài)發(fā)布接口

動態(tài)發(fā)布接口

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于網(wǎng)站建設、成都網(wǎng)站制作、哈密網(wǎng)絡推廣、微信小程序開發(fā)、哈密網(wǎng)絡營銷、哈密企業(yè)策劃、哈密品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供哈密建站搭建服務,24小時服務熱線:18982081108,官方網(wǎng)址:aaarwkj.com

    HTTP接口分為REST和SOAP2種方式,文中都涉及到,包含從動態(tài)生成文件到編譯class再到裝載到spring容器和ws.Endpoint中。

    

    REST風格

        方案:

            1.提供java文件模板

            2.讀取文件內容

            3.查庫修改生成java文件

            4.通過JDK中的javax.tools.JavaCompiler動態(tài)編譯成class

            5.通過繼承java.net.URLClassLoader動態(tài)加載class文件到內存

            6.通過獲取spring的ApplicationContext手動把mapping注冊到RequestMappingHandlerMapping中完成動態(tài)發(fā)布

        過程:

            1.模板文件根據(jù)業(yè)務自行配置(涉及公司機密,忽略)

            2.讀取文件內容,生成java文件,編譯class,加載class,發(fā)布接口

        //動態(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("動態(tài)創(chuàng)建接口錯誤,文件已存在:"+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";
			//動態(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("動態(tài)創(chuàng)建接口錯誤",e);
			return false;
		}
		return true;
	}
	
	/**
	* controlCenter(運行時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);
		//設置私有屬性為可見
		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);
	}

         

        結果:

            可以正常發(fā)布spring接口,動態(tài)生成文件注入mapping到spring接口中。

    SOAP風格

        方案:

            1.提供java文件模板

            2.讀取文件內容

            3.查庫修改生成java文件

            4.通過JDK中的javax.tools.JavaCompiler動態(tài)編譯成class

            5.通過繼承java.net.URLClassLoader動態(tài)加載class文件到內存

            6.通過javax.xml.ws.Endpoint的publish動態(tài)發(fā)布接口

        過程:

            1.模板文件根據(jù)業(yè)務自行配置(涉及公司機密,忽略)

            2.讀取文件內容,生成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("動態(tài)創(chuàng)建接口錯誤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("動態(tài)創(chuàng)建接口錯誤ws", e);
			return false;
		}
		return true;
	}
	//動態(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);
	}

        結果:

            可以正常發(fā)布SOAP接口,動態(tài)生成文件發(fā)布SOAP接口。

        后面附件會上傳動態(tài)生成需要的工具類,需要的小伙伴可以下載,記得好評!

                                                 author:賈小仙

                                                 time:2018/9/5 

分享文章:動態(tài)發(fā)布接口
網(wǎng)頁網(wǎng)址:http://aaarwkj.com/article6/pjcdog.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站營銷、關鍵詞優(yōu)化、網(wǎng)站改版、ChatGPT、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿網(wǎng)站建設
久久最新视频中文字幕| 啊啊…嗯嗯…用力免费观看视频| 高清国产国产精品三级国产av| 久久伊人这里都是精品| 青青草成人免费在线公开视频| 天美传媒剧国产在线观看| 亚洲av成人在线不卡| 亚洲青青草原一区二区| 最新日韩av一区二区| 精品亚洲天堂一区二区三区| 麻豆视频在线观看传媒| 国产av综合一区二区| 91中文字幕国产日韩| 国产一级无码免费视频| 最新国产av网址大全| 久久婷婷国产综合色啪| 国产传媒视频在线免费观看| 欧美日韩国产一区在线| 国产欧美高清在线观看视频| 国产日韩精品免费在线| 亚洲午夜av久久乱码| 国产精品一区二区综合亚洲| 日韩在线不卡中文字幕| 国产精品一区欧美精品| 欧美一区二区三区高清在线| 亚洲国产精品视频中文字幕| 欧美精品青青久久久久久| 国产精品欧美久久久久无| 国产一区二区高清不卡| 国产精品一区二区国产激情久久| 国产精品一区二区三区欧美| 神马影院在线观看午夜| 国产精品日韩伦理一区二区| 亚洲一区二区三区色婷婷| 免费在线观看日韩av大片| 亚洲成a人片777777久久| 日韩一区二区免费看视频| 亚洲欧美日韩校园春色| 中文字幕二区三区人妻| 日韩特级黄片在线免费观看| 亚洲精品尤物福利在线一区|