protected boolean isSwaggerWrapBody(ArgumentsMapperConfig config, List<ProviderParameter> providerNormalParams) { Method swaggerMethod = config.getSwaggerMethod(); if (swaggerMethod.getParameterCount() != 1) { return false; } Type swaggerType = config.getSwaggerMethod().getGenericParameterTypes()[0]; if (!swaggerType.getClass().equals(Class.class)) { return false; } Type firstProviderParam = providerNormalParams.get(0).getType(); if (TypeUtils.isAssignable(firstProviderParam, swaggerType)) { return false; } swaggerType = ((Class<?>) swaggerType).getFields()[0].getGenericType(); Converter converter = converterMgr.findConverter(type, firstProviderParam, swaggerType); if (ConverterCommon.class.isInstance(converter)) { return false; } // 透明rpc的包装场景 return true; }
/** * Returns the requested method from an object instance. * * @param methodName Name of the setter method. * @param instance Object instance to search. * @param valueClass The desired property return type (null if don't care). * @param setter If true, search for setter method signature. If false, getter method signature. * @return The requested method. * @throws NoSuchMethodException If method was not found. */ private static Method findMethod(String methodName, Object instance, Class<?> valueClass, boolean setter) throws NoSuchMethodException { if (methodName == null) { return null; } int paramCount = setter ? 1 : 0; for (Method method : instance.getClass().getMethods()) { if (method.getName().equals(methodName) && method.getParameterTypes().length == paramCount) { Class<?> targetClass = setter ? method.getParameterTypes()[0] : method.getReturnType(); if (valueClass == null || TypeUtils.isAssignable(targetClass, valueClass)) { return method; } } } throw new NoSuchMethodException("Compatible method not found: " + methodName); }
@Override @SuppressWarnings("deprecation") protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType()); JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding); try { JavaType javaType = null; if (jackson26Available && type != null && object != null && TypeUtils.isAssignable(type, object.getClass())) { javaType = getJavaType(type, null); } ObjectWriter objectWriter; objectWriter = this.objectMapper.writer(); if (javaType != null && javaType.isContainerType()) { objectWriter = objectWriter.withType(javaType); } objectWriter.writeValue(generator, object); generator.flush(); } catch (JsonProcessingException ex) { throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex); } }
/** * Following AspectJ semantics, if a returning clause was specified, then the * advice is only invoked if the returned value is an instance of the given * returning type and generic type parameters, if any, match the assignment * rules. If the returning type is Object, the advice is *always* invoked. * @param returnValue the return value of the target method * @return whether to invoke the advice method for the given return value */ private boolean shouldInvokeOnReturnValueOf(Method method, Object returnValue) { Class<?> type = getDiscoveredReturningType(); Type genericType = getDiscoveredReturningGenericType(); // If we aren't dealing with a raw type, check if generic parameters are assignable. return (matchesReturnValue(type, method, returnValue) && (genericType == null || genericType == type || TypeUtils.isAssignable(genericType, method.getGenericReturnType()))); }
@Override public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { returnType.increaseNestingLevel(); Type nestedType = returnType.getNestedGenericParameterType(); returnType.decreaseNestingLevel(); return ResourceSupport.class.isAssignableFrom(returnType.getParameterType()) || TypeUtils.isAssignable(ResourceSupport.class, nestedType); }
/** * Following AspectJ semantics, if a returning clause was specified, then the * advice is only invoked if the returned value is an instance of the given * returning type and generic type parameters, if any, match the assignment * rules. If the returning type is Object, the advice is *always* invoked. * @param returnValue the return value of the target method * @return whether to invoke the advice method for the given return value */ private boolean shouldInvokeOnReturnValueOf(Method method, Object returnValue) { Class type = getDiscoveredReturningType(); Type genericType = getDiscoveredReturningGenericType(); // If we aren't dealing with a raw type, check if generic parameters are assignable. return (ClassUtils.isAssignableValue(type, returnValue) && (genericType == null || genericType == type || TypeUtils.isAssignable(genericType, method.getGenericReturnType()))); }
@Override @SuppressWarnings("deprecation") protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType()); JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding); try { writePrefix(generator, object); Class<?> serializationView = null; FilterProvider filters = null; Object value = object; JavaType javaType = null; if (object instanceof MappingJacksonValue) { MappingJacksonValue container = (MappingJacksonValue) object; value = container.getValue(); serializationView = container.getSerializationView(); filters = container.getFilters(); } if (jackson26Available && type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) { javaType = getJavaType(type, null); } ObjectWriter objectWriter; if (serializationView != null) { objectWriter = this.objectMapper.writerWithView(serializationView); } else if (filters != null) { objectWriter = this.objectMapper.writer(filters); } else { objectWriter = this.objectMapper.writer(); } if (javaType != null && javaType.isContainerType()) { objectWriter = objectWriter.withType(javaType); } objectWriter.writeValue(generator, value); writeSuffix(generator, object); generator.flush(); } catch (JsonProcessingException ex) { throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex); } }