Search in sources :

Example 1 with ProducerArgumentsMapper

use of org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper in project incubator-servicecomb-java-chassis by apache.

the class SwaggerEnvironment method createProducer.

public SwaggerProducer createProducer(Object producerInstance, Swagger swagger) {
    Class<?> producerCls = BeanUtils.getImplClassFromBean(producerInstance);
    Map<String, Method> visibleProducerMethods = retrieveVisibleMethods(producerCls);
    Class<?> swaggerIntf = ClassUtils.getOrCreateInterface(swagger, null, null);
    SwaggerProducer producer = new SwaggerProducer();
    producer.setProducerCls(producerCls);
    producer.setSwaggerIntf(swaggerIntf);
    for (Method swaggerMethod : swaggerIntf.getMethods()) {
        String methodName = swaggerMethod.getName();
        // producer参数不一定等于swagger参数
        Method producerMethod = visibleProducerMethods.getOrDefault(methodName, null);
        if (producerMethod == null) {
            // producer未实现契约,非法
            String msg = String.format("swagger method %s:%s not exist in producer.", swaggerIntf.getClass().getName(), methodName);
            throw new Error(msg);
        }
        ProducerArgumentsMapper argsMapper = producerArgumentsFactory.createArgumentsMapper(swaggerMethod, producerMethod);
        ProducerResponseMapper responseMapper = producerResponseMapperFactorys.createResponseMapper(swaggerMethod.getGenericReturnType(), producerMethod.getGenericReturnType());
        SwaggerProducerOperation op = new SwaggerProducerOperation();
        op.setName(methodName);
        op.setProducerClass(producerCls);
        op.setProducerInstance(producerInstance);
        op.setProducerMethod(producerMethod);
        op.setSwaggerMethod(swaggerMethod);
        op.setArgumentsMapper(argsMapper);
        op.setResponseMapper(responseMapper);
        producer.addOperation(op);
    }
    return producer;
}
Also used : ProducerResponseMapper(org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper) ProducerArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)1 ProducerArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper)1 ProducerResponseMapper (org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper)1