Search in sources :

Example 6 with ConsumerResponseMapper

use of org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper in project java-chassis by ServiceComb.

the class SwaggerEnvironment method createConsumer.

public SwaggerConsumer createConsumer(Class<?> consumerIntf, Swagger swagger) {
    Map<Class<?>, ContextArgumentMapperFactory> contextFactorys = SPIServiceUtils.getOrLoadSortedService(ConsumerContextArgumentMapperFactory.class).stream().collect(Collectors.toMap(ConsumerContextArgumentMapperFactory::getContextClass, Function.identity()));
    ResponseMapperFactorys<ConsumerResponseMapper> consumerResponseMapperFactorys = new ResponseMapperFactorys<>(ConsumerResponseMapperFactory.class);
    SwaggerOperations swaggerOperations = new SwaggerOperations(swagger);
    SwaggerConsumer consumer = new SwaggerConsumer();
    consumer.setConsumerIntf(consumerIntf);
    for (Method consumerMethod : MethodUtils.findSwaggerMethods(consumerIntf)) {
        String operationId = findOperationId(consumerMethod);
        SwaggerOperation swaggerOperation = swaggerOperations.findOperation(operationId);
        if (swaggerOperation == null) {
            // consumer method set is bigger than contract, it's invalid
            // but we need to support consumer upgrade before producer, so only log and ignore it.
            LOGGER.warn("consumer method {}:{} not exist in contract.", consumerIntf.getName(), consumerMethod.getName());
            continue;
        }
        ConsumerArgumentsMapperCreator creator = new ConsumerArgumentsMapperCreator(Json.mapper().getSerializationConfig(), contextFactorys, consumerIntf, consumerMethod, swaggerOperation);
        ArgumentsMapper argsMapper = creator.createArgumentsMapper();
        ConsumerResponseMapper responseMapper = consumerResponseMapperFactorys.createResponseMapper(consumerMethod.getGenericReturnType());
        SwaggerConsumerOperation op = new SwaggerConsumerOperation();
        op.setConsumerClass(consumerIntf);
        op.setConsumerMethod(consumerMethod);
        op.setSwaggerOperation(swaggerOperation);
        op.setArgumentsMapper(argsMapper);
        op.setResponseMapper(responseMapper);
        consumer.addOperation(op);
    }
    return consumer;
}
Also used : SwaggerOperations(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations) SwaggerOperation(org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation) ResponseMapperFactorys(org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys) Method(java.lang.reflect.Method) ConsumerArgumentsMapperCreator(org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapperCreator) ConsumerContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory) ProducerContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerContextArgumentMapperFactory) ContextArgumentMapperFactory(org.apache.servicecomb.swagger.invocation.arguments.ContextArgumentMapperFactory) ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper) ArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper) ProducerArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper)

Example 7 with ConsumerResponseMapper

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

the class SpringmvcConsumerResponseMapperFactory method createResponseMapper.

@Override
public ConsumerResponseMapper createResponseMapper(ResponseMapperFactorys<ConsumerResponseMapper> factorys, Type swaggerType, Type consumerType) {
    Type realConsumerType = ((ParameterizedType) consumerType).getActualTypeArguments()[0];
    ConsumerResponseMapper realMapper = factorys.createResponseMapper(swaggerType, realConsumerType);
    return new SpringmvcConsumerResponseMapper(realMapper);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper)

Example 8 with ConsumerResponseMapper

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

the class TestSpringmvcConsumerResponseMapperFactory method createResponseMapper.

@Test
public void createResponseMapper() {
    Method responseEntityMethod = ReflectUtils.findMethod(this.getClass(), "responseEntity");
    Method listMethod = ReflectUtils.findMethod(this.getClass(), "list");
    ConsumerResponseMapper mapper = factory.createResponseMapper(factorys, listMethod.getGenericReturnType(), responseEntityMethod.getGenericReturnType());
    Assert.assertThat(mapper, Matchers.instanceOf(SpringmvcConsumerResponseMapper.class));
    Response response = Response.ok(Arrays.asList("a", "b"));
    @SuppressWarnings("unchecked") ResponseEntity<String[]> responseEntity = (ResponseEntity<String[]>) mapper.mapResponse(response);
    Assert.assertThat(responseEntity.getBody(), Matchers.arrayContaining("a", "b"));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ResponseEntity(org.springframework.http.ResponseEntity) ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

ConsumerResponseMapper (org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper)8 Method (java.lang.reflect.Method)4 Test (org.junit.Test)4 Response (org.apache.servicecomb.swagger.invocation.Response)3 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 DefaultConsumerResponseMapper (org.apache.servicecomb.swagger.invocation.response.consumer.DefaultConsumerResponseMapper)2 ResponseEntity (org.springframework.http.ResponseEntity)2 SwaggerOperation (org.apache.servicecomb.swagger.generator.core.model.SwaggerOperation)1 SwaggerOperations (org.apache.servicecomb.swagger.generator.core.model.SwaggerOperations)1 ArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper)1 ContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.ContextArgumentMapperFactory)1 ConsumerArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapper)1 ConsumerArgumentsMapperCreator (org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapperCreator)1 ConsumerContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerContextArgumentMapperFactory)1 ProducerArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper)1 ProducerContextArgumentMapperFactory (org.apache.servicecomb.swagger.invocation.arguments.producer.ProducerContextArgumentMapperFactory)1 Converter (org.apache.servicecomb.swagger.invocation.converter.Converter)1 ResponseMapperFactorys (org.apache.servicecomb.swagger.invocation.response.ResponseMapperFactorys)1