Spring mvc3.2 requestbody json display principle
1,public interface HandlerMethodReturnValueHandler {/** * Whether the given {@linkplain MethodParameter method return type} is * supported by this handler. * * @param returnType the method return type to check * @return {@code true} if this handler supports the supplied return type; * {@code false} otherwise */boolean supportsReturnType(MethodParameter returnType);/** * Handle the given return value by adding attributes to the model and * setting a view or setting the * {@link ModelAndViewContainer#setRequestHandled} flag to {@code true} * to indicate the response has been handled directly. * * @param returnValue the value returned from the handler method * @param returnType the type of the return value. This type must have * previously been passed to * {@link #supportsReturnType(org.springframework.core.MethodParameter)} * and it must have returned {@code true} * @param mavContainer the ModelAndViewContainer for the current request * @param webRequest the current request * @throws Exception if the return value handling results in an error */void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception;}2,public class RequestResponseBodyMethodProcessor extends AbstractMessageConverterMethodProcessor {public RequestResponseBodyMethodProcessor(List
> messageConverters) {super(messageConverters);}public boolean supportsParameter(MethodParameter parameter) {return parameter.hasParameterAnnotation(RequestBody.class);}public boolean supportsReturnType(MethodParameter returnType) {return returnType.getMethodAnnotation(ResponseBody.class) != null;}public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {Object arg = readWithMessageConverters(webRequest, parameter, parameter.getParameterType());Annotation[] annotations = parameter.getParameterAnnotations();for (Annotation annot : annotations) {if (annot.annotationType().getSimpleName().startsWith("Valid")) {String name = Conventions.getVariableNameForParameter(parameter);WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);Object hints = AnnotationUtils.getValue(annot);binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});BindingResult bindingResult = binder.getBindingResult();if (bindingResult.hasErrors()) {throw new MethodArgumentNotValidException(parameter, bindingResult);}}}return arg;}public void handleReturnValue(Object returnValue, MethodParameter returnType,ModelAndViewContainer mavContainer, NativeWebRequest webRequest)throws IOException, HttpMediaTypeNotAcceptableException {mavContainer.setRequestHandled(true);if (returnValue != null) {writeWithMessageConverters(returnValue, returnType, webRequest);}}}3,AbstractMessageConverterMethodProcessorprotected
void writeWithMessageConverters(T returnValue, MethodParameter returnType, NativeWebRequest webRequest)throws IOException, HttpMediaTypeNotAcceptableException {ServletServerHttpRequest inputMessage = createInputMessage(webRequest);ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);writeWithMessageConverters(returnValue, returnType, inputMessage, outputMessage);}4,AbstractMessageConverterMethodProcessorpublic final void write(T t, MediaType contentType, HttpOutputMessage outputMessage)throws IOException, HttpMessageNotWritableException {HttpHeaders headers = outputMessage.getHeaders();if (headers.getContentType() == null) {if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {contentType = getDefaultContentType(t);}if (contentType != null) {headers.setContentType(contentType);}}if (headers.getContentLength() == -1) {Long contentLength = getContentLength(t, headers.getContentType());if (contentLength != null) {headers.setContentLength(contentLength);}}writeInternal(t, outputMessage);outputMessage.getBody().flush();}5,MappingJacksonHttpMessageConverterprotected void writeInternal(Object object, HttpOutputMessage outputMessage)throws IOException, HttpMessageNotWritableException {JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());JsonGenerator jsonGenerator =this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);// A workaround for JsonGenerators not applying serialization features// https://github.com/FasterXML/jackson-databind/issues/12if (this.objectMapper.getSerializationConfig().isEnabled(SerializationConfig.Feature.INDENT_OUTPUT)) {jsonGenerator.useDefaultPrettyPrinter();}try {if (this.prefixJson) {jsonGenerator.writeRaw("{} && ");}this.objectMapper.writeValue(jsonGenerator, object);}catch (JsonProcessingException ex) {throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);}}