Search in sources :

Example 1 with ConsumerResponseMapper

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

the class SwaggerEnvironment method createConsumer.

public SwaggerConsumer createConsumer(Class<?> consumerIntf, Class<?> swaggerIntf) {
    SwaggerConsumer consumer = new SwaggerConsumer();
    consumer.setConsumerIntf(consumerIntf);
    consumer.setSwaggerIntf(swaggerIntf);
    for (Method consumerMethod : consumerIntf.getMethods()) {
        String swaggerMethodName = findSwaggerMethodName(consumerMethod);
        // consumer参数不一定等于swagger参数
        Method swaggerMethod = ReflectUtils.findMethod(swaggerIntf, swaggerMethodName);
        if (swaggerMethod == null) {
            // consumer大于契约,非法
            String msg = String.format("consumer method %s:%s not exist in swagger.", consumerIntf.getName(), consumerMethod.getName());
            throw new Error(msg);
        }
        ConsumerArgumentsMapper argsMapper = consumerArgumentsFactory.createArgumentsMapper(swaggerMethod, consumerMethod);
        ConsumerResponseMapper responseMapper = consumerResponseMapperFactorys.createResponseMapper(swaggerMethod.getGenericReturnType(), consumerMethod.getGenericReturnType());
        SwaggerConsumerOperation op = new SwaggerConsumerOperation();
        op.setName(consumerMethod.getName());
        op.setConsumerMethod(consumerMethod);
        op.setSwaggerMethod(swaggerMethod);
        op.setArgumentsMapper(argsMapper);
        op.setResponseMapper(responseMapper);
        consumer.addOperation(op);
    }
    return consumer;
}
Also used : ConsumerArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.consumer.ConsumerArgumentsMapper) ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper) Method(java.lang.reflect.Method)

Example 2 with ConsumerResponseMapper

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

the class TestSpringmvcConsumerResponseMapperFactory method asyncResponseEntity.

@Test
public void asyncResponseEntity() {
    Method responseEntityMethod = ReflectUtils.findMethod(this.getClass(), "asyncResponseEntityMethod");
    Method listMethod = ReflectUtils.findMethod(this.getClass(), "list");
    ConsumerResponseMapper mapper = factorys.createResponseMapper(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)

Example 3 with ConsumerResponseMapper

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

the class TestResponseMapperFactorys method createResponseMapper_default.

@Test
public void createResponseMapper_default() {
    ConsumerResponseMapper mapper = consumerResponseMapperFactorys.createResponseMapper(String.class, String.class);
    Assert.assertThat(mapper, Matchers.instanceOf(DefaultConsumerResponseMapper.class));
    Converter converter = Deencapsulation.getField(mapper, "converter");
    Assert.assertSame(ConverterSame.getInstance(), converter);
}
Also used : ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper) DefaultConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.DefaultConsumerResponseMapper) Converter(org.apache.servicecomb.swagger.invocation.converter.Converter) DefaultConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.DefaultConsumerResponseMapper) Test(org.junit.Test)

Example 4 with ConsumerResponseMapper

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

the class TestResponseMapperFactorys method createResponseMapper_cseResponse.

@Test
public void createResponseMapper_cseResponse() {
    ConsumerResponseMapper mapper = consumerResponseMapperFactorys.createResponseMapper(String.class, Response.class);
    Response response = Response.ok(null);
    Object result = mapper.mapResponse(response);
    Assert.assertSame(response, result);
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper) DefaultConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.DefaultConsumerResponseMapper) Test(org.junit.Test)

Example 5 with ConsumerResponseMapper

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

the class SpringmvcConsumerResponseMapperFactory method createResponseMapper.

@Override
public ConsumerResponseMapper createResponseMapper(ResponseMapperFactorys<ConsumerResponseMapper> factorys, Type consumerType) {
    Type realConsumerType = ((ParameterizedType) consumerType).getActualTypeArguments()[0];
    ConsumerResponseMapper realMapper = factorys.createResponseMapper(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)

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