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