SSM框架之@ResponseBody和@RequestBody的區別及HttpMessageConverter的作用
阿新 • • 發佈:2018-12-18
一、@ResponseBody
@ResponseBody作用於方法上,將返回結果直接以Json等格式返回,直接封裝到response物件的Body中,不經過SpringMVC的檢視解析器。一般用於前後端分離的專案,或者Ajax等非同步請求。
@ResponseBody public Student getStudent(Integer id){ Student student = sstudentService.getStudent(id); return student; } //前端得到的Json資料: '{"id":"201541030102","name":"劉聰","sex":"男"}'
二、@RequestBody
@RequestBody作用方法的形參,將前端傳來的Json等格式的資料封裝到javabean中。它會直接讀取讀取Request請求的body部分資料,再使用HttpMessageConverter進行解析。
//前端傳來的的Json資料: '{"id":"201541030102","name":"劉聰","sex":"男"}' @ResponseBody public Integer setStudent(@ResponseBody Student student){ Integer total = sstudentService.setStudent(stedent); return total; }
三、HttpMessageConverter
HTTP的請求報文就是一種字串,在java中會被封裝在ServletInputStream流裡,我們在從流中獲得資料,HTTP的響應報文則是通過ServletOutputStream流進行向外輸出的。
獲得請求報文的資訊實際就是HttpMessageConverter介面通過相應的實現類來解析流中的資訊,並轉化為Spring中可以使用的Javabean的過程,響應過程相反。
HttpMessageConverter介面原始碼;
/* * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.http.converter; import java.io.IOException; import java.util.List; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; /** * Strategy interface that specifies a converter that can convert from and to HTTP requests and responses. * * @author Arjen Poutsma * @author Juergen Hoeller * @since 3.0 */ public interface HttpMessageConverter<T> { /** * Indicates whether the given class can be read by this converter. * @param clazz the class to test for readability * @param mediaType the media type to read, can be {@code null} if not specified. * Typically the value of a {@code Content-Type} header. * @return {@code true} if readable; {@code false} otherwise */ boolean canRead(Class<?> clazz, MediaType mediaType); /** * Indicates whether the given class can be written by this converter. * @param clazz the class to test for writability * @param mediaType the media type to write, can be {@code null} if not specified. * Typically the value of an {@code Accept} header. * @return {@code true} if writable; {@code false} otherwise */ boolean canWrite(Class<?> clazz, MediaType mediaType); /** * Return the list of {@link MediaType} objects supported by this converter. * @return the list of supported media types */ List<MediaType> getSupportedMediaTypes(); /** * Read an object of the given type form the given input message, and returns it. * @param clazz the type of object to return. This type must have previously been passed to the * {@link #canRead canRead} method of this interface, which must have returned {@code true}. * @param inputMessage the HTTP input message to read from * @return the converted object * @throws IOException in case of I/O errors * @throws HttpMessageNotReadableException in case of conversion errors */ T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException; /** * Write an given object to the given output message. * @param t the object to write to the output message. The type of this object must have previously been * passed to the {@link #canWrite canWrite} method of this interface, which must have returned {@code true}. * @param contentType the content type to use when writing. May be {@code null} to indicate that the * default content type of the converter must be used. If not {@code null}, this media type must have * previously been passed to the {@link #canWrite canWrite} method of this interface, which must have * returned {@code true}. * @param outputMessage the message to write to * @throws IOException in case of I/O errors * @throws HttpMessageNotWritableException in case of conversion errors */ void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException; }
一些相關的實現類