spring mvc3.2 requestbody json顯示原理

來源:互聯網
上載者:User

spring mvc3.2 requestbody json顯示原理

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.