동적 발표 인터페이스
HTTP 인 터 페 이 스 는 REST 와 SOAP 2 방식 으로 나 뉘 는데 이 글 은 동적 생 성 파일 부터 컴 파일 클 라 스, spring 용기 와 ws. Endpoint 에 불 러 오 는 것 을 포함한다.
REST 스타일
프로젝트:
1. 자바 파일 템 플 릿 제공
2. 파일 내용 읽 기
3. 라 이브 러 리 수정 자바 파일 생 성
4. JDK 의 javax. tools. JavaCompiler 를 통 해 class 로 동적 컴 파일
5. java. net. URLClassLoader 를 계승 하여 동적 으로 class 파일 을 메모리 에 불 러 옵 니 다.
6. spring 을 가 져 오 는 applicationContext 를 통 해 수 동 으로 mapping 을 Request Mapping Handler Mapping 에 등록 하여 동적 발 표를 완성 합 니 다.
프로 세 스:
1. 템 플 릿 파일 은 업무 에 따라 자체 적 으로 설정 (회사 기밀 관련, 무시)
2. 파일 내용 읽 기, 자바 파일 생 성, class 컴 파일, class 불 러 오기, 발표 인터페이스
//
@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(" , :"+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";
// class
JavaStringCompiler compiler = new JavaStringCompiler();
Map 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(" ",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);
}
결과:
spring 인 터 페 이 스 를 정상적으로 발표 하고 동적 생 성 파일 을 spring 인터페이스 에 주입 할 수 있 습 니 다.
SOAP 스타일
프로젝트:
1. 자바 파일 템 플 릿 제공
2. 파일 내용 읽 기
3. 라 이브 러 리 수정 자바 파일 생 성
4. JDK 의 javax. tools. JavaCompiler 를 통 해 class 로 동적 컴 파일
5. java. net. URLClassLoader 를 계승 하여 동적 으로 class 파일 을 메모리 에 불 러 옵 니 다.
6. javax. xml. ws. Endpoint 의 Publish 동적 발표 인 터 페 이 스 를 통 해
프로 세 스:
1. 템 플 릿 파일 은 업무 에 따라 자체 적 으로 설정 (회사 기밀 관련, 무시)
2. 파일 내용 읽 기, 자바 파일 생 성, class 컴 파일, class 불 러 오기, Endpoint 를 통 해 인터페이스 발표
@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(" 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 results = compiler.compile(controllerJava, content);
Class> clzMul = compiler.loadClass(fullName, results);
publish(clzMul, serviceName);
} catch (Exception e) {
logger.error(" ws", e);
return false;
}
return true;
}
//
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);
}
결과:
SOAP 인 터 페 이 스 를 정상적으로 발표 하고 파일 을 동적 으로 생 성하 여 SOAP 인 터 페 이 스 를 발표 할 수 있 습 니 다.
뒤에 첨부 파일 은 동적 생 성 에 필요 한 도구 류 를 업로드 합 니 다. 필요 한 파트너 는 다운로드 할 수 있 습 니 다. 호평 을 기억 하 세 요!
저자: 가소선
time:2018/9/5
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.