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;
}
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"));
}
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);
}
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);
}
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);
}
Aggregations