吴学文
2019-03-19 732f76a904e6f202b6d953257500e3a8aaf3dfdb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.java110.web.controller;
 
import com.java110.common.factory.ApplicationContextFactory;
import com.java110.common.util.Assert;
import com.java110.core.base.controller.BaseController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
 
 
@RestController
public class CallComponentController extends BaseController {
 
    /**
     * 调用组件方法
     * @return
     */
 
    @RequestMapping(path="/callComponent/{componentCode}/{componentMethod}",
            method = RequestMethod.POST)
    public ResponseEntity<String> callComponent(
            @PathVariable String componentCode,
            @PathVariable String componentMethod,
            @RequestBody String info,
            HttpServletRequest request){
        ResponseEntity<String> responseEntity = null;
        try{
            Assert.hasLength(componentCode,"参数错误,未传入组件编码");
            Assert.hasLength(componentMethod,"参数错误,未传入调用组件方法");
 
            Object componentInstance = ApplicationContextFactory.getBean(componentCode);
 
            Assert.notNull(componentInstance,"未找到组件对应的处理类,请确认 "+componentCode);
 
            Method cMethod = componentInstance.getClass().getDeclaredMethod(componentMethod,String.class);
 
            Assert.notNull(cMethod,"未找到组件对应处理类的方法,请确认 "+componentCode+"方法:"+componentMethod);
 
             responseEntity = (ResponseEntity<String>)cMethod.invoke(componentInstance,info);
 
        }catch (Exception e){
            responseEntity = new ResponseEntity<>("调用组件失败"+e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }finally {
            return responseEntity;
        }
    }
 
 
}