Search in sources :

Example 1 with ProducerResponseMapper

use of org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper 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)

Example 2 with ProducerResponseMapper

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

the class SpringmvcProducerResponseMapperFactory method createResponseMapper.

@Override
public ProducerResponseMapper createResponseMapper(ResponseMapperFactorys<ProducerResponseMapper> factorys, Type swaggerType, Type producerType) {
    Type realProducerType = ((ParameterizedType) producerType).getActualTypeArguments()[0];
    ProducerResponseMapper realMapper = factorys.createResponseMapper(swaggerType, realProducerType);
    return new SpringmvcProducerResponseMapper(realMapper);
}
Also used : ProducerResponseMapper(org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 3 with ProducerResponseMapper

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

the class TestSpringmvcProducerResponseMapperFactory method createResponseMapper.

@Test
public void createResponseMapper() {
    Method responseEntityMethod = ReflectUtils.findMethod(this.getClass(), "responseEntity");
    Method listMethod = ReflectUtils.findMethod(this.getClass(), "list");
    ProducerResponseMapper mapper = factory.createResponseMapper(factorys, listMethod.getGenericReturnType(), responseEntityMethod.getGenericReturnType());
    Assert.assertThat(mapper, Matchers.instanceOf(SpringmvcProducerResponseMapper.class));
    ResponseEntity<String[]> responseEntity = new ResponseEntity<String[]>(new String[] { "a", "b" }, HttpStatus.OK);
    Response response = mapper.mapResponse(null, responseEntity);
    Assert.assertThat(response.getResult(), Matchers.contains("a", "b"));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ProducerResponseMapper(org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper) ResponseEntity(org.springframework.http.ResponseEntity) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

ProducerResponseMapper (org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper)3 Method (java.lang.reflect.Method)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Response (org.apache.servicecomb.swagger.invocation.Response)1 ProducerArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper)1 Test (org.junit.Test)1 ResponseEntity (org.springframework.http.ResponseEntity)1