一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - springmvc @RequestBody String類型參數的使用

springmvc @RequestBody String類型參數的使用

2022-03-05 15:18zoyation Java教程

這篇文章主要介紹了springmvc @RequestBody String類型參數的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springmvc @RequestBody String類型參數

通過如下配置:

 <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
      <property name="supportedMediaTypes">
          <list>
              <value>text/html;charset=UTF-8</value>
              <value>application/json;charset=UTF-8</value>
          </list>
      </property>
  </bean>
  <!-- 啟動SpringMVC的注解功能,完成請求和注解POJO的映射 -->
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
      <property name="messageConverters">
          <list>
              <ref bean="mappingJacksonHttpMessageConverter"/>
              <!-- JSON轉換器 -->
          </list>
      </property>
  </bean> 

在spring mvc的Controller層使用@RequestBody接收Content-Type為application/json的數據時,默認支持Map方式和對象方式參數

@RequestMapping(value = "/[code]/saveUser", method = RequestMethod.POST)
  @ResponseBody
  public JsonResult saveUser(@PathVariable("code") Integer code, @RequestBody Map<String, Object> datas,@RequestBody User user) {
  。。。
  }

如果是一個參數時也需要用個Map或者對象處理,使用String會報解析錯誤,具體看:AbstractJackson2HttpMessageConverter的方法read(Type type, Class

@Override
  public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
          throws IOException, HttpMessageNotReadableException {
      JavaType javaType = getJavaType(type, contextClass);
      return readJavaType(javaType, inputMessage);
  }
  private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
      try {
          return this.objectMapper.readValue(inputMessage.getBody(), javaType);
      }
      catch (IOException ex) {
          throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
      }
  }

為了讓@RequestBody支持String參數(目前只支持接收單個參數)

重寫org.springframework.http.converter.json.MappingJackson2HttpMessageConverter類

package com.test.converter.json
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
/**
* 處理@RequestBody注解為String的情況,只支持接收單個參數的情況
* Created by test
* Date:2017/1/4
* Time:17:33
*/
public class CustomerMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
  @Override
  protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
      Class<?> deseriClazz = getClazz(clazz);
      Object param = super.readInternal(deseriClazz, inputMessage);
      return getTrueObject(clazz, param);
  }
  @Override
  public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
          throws IOException, HttpMessageNotReadableException {
      Type deseriType = getType(type);
      Object param = super.read(deseriType, contextClass, inputMessage);
      return getTrueObject(type, param);
  }
  /**
   * 通過返回參數類型決定是否處理參數,如果是String類型的參數,將解析后的HashMap里的值返回(只支持單個參數)
   *
   * @param type  返回參數類型
   * @param param 參數值
   * @return 實際參數值
   */
  private Object getTrueObject(Type type, Object param) {
      if (type == String.class) {
          Object backParam = null;
          if (param != null && param instanceof LinkedHashMap) {
              LinkedHashMap paramMap = (LinkedHashMap) param;
              if (paramMap.size() == 1) {
                  backParam = paramMap.get(paramMap.keySet().iterator().next());
              }
          }
          param = backParam;
      }
      return param;
  }
  /**
   * 獲取解析參數用的Type
   *
   * @param type 參數類型
   * @return
   */
  private Type getType(Type type) {
      Type deseriClazz;
      if (type == String.class) {
          //jackson不支持String默認用LinkedHashMap
          deseriClazz = LinkedHashMap.class;
      } else {
          deseriClazz = type;
      }
      return deseriClazz;
  }
  /**
   * 獲取解析參數用的Type
   * @param clazz 參數類型
   * @return
   */
  private Class<?> getClazz(Class<?> clazz) {
      Class<?> deseriClazz;
      if (clazz == String.class) {
          //jackson不支持String默認用LinkedHashMap
          deseriClazz = LinkedHashMap.class;
      } else {
          deseriClazz = clazz;
      }
      return deseriClazz;
  }
}

spring mvc xml配置文件修改:

<bean id="mappingJacksonHttpMessageConverter"
        class="com.test.converter.json.CustomerMappingJackson2HttpMessageConverter">
      <property name="supportedMediaTypes">
          <list>
              <value>text/html;charset=UTF-8</value>
              <value>application/json;charset=UTF-8</value>
          </list>
      </property>
  </bean>

Controller層:

@RequestMapping(value = "/delUser", method = RequestMethod.POST)
  @ResponseBody
  public JsonResult delUser(@RequestBody String id) {
  。。。
  }

 

springmvc用Map接收請求參數分析

第一種情況,什么也不設置,無參數傳遞

注解為 @Controller @RequestMapping

springmvc @RequestBody String類型參數的使用

可以看到傳遞的為SpringMVC的BindingAwareModelMap類型,SpringMVC中的隱含模型就是這個類型,其作用域等價于 request 域,當添加Model、ModelMap參數時,SpringMVC實際傳入的就是這個隱含模型;向這個隱含模型種設置值后,在返回的頁面中就能通過request域取值。

第二種情況,加個參數試試 => .../testmap?test1=2342

結果類型還是一樣,且參數不會被傳入,當然使用request肯定能取出來。

springmvc @RequestBody String類型參數的使用

第三種情況,給Map參數添加@RequestParam注解

1、Get請求 =>http://localhost:8080/ssm/v2/testmap?test1=234234

成功傳入了參數,注意這個Map類型為LinkedHashMap,而不是隱含模型了

springmvc @RequestBody String類型參數的使用

再添加個Model參數看看,隱含模型中依然沒有值

所以添加@RequestParam注解后,SpringMVC會將 Get 請求中封裝進對應的參數中,如果參數是Map就封裝稱LinkedHashMap而不再傳入隱含模型

springmvc @RequestBody String類型參數的使用

2、Post請求, 再測試測試Post請求

與Get的結果一致:參數無@RequestParam注解時,Map接收隱含模型;添加@RequestParam注解時,Map接收LinkedHashMap;隱含模型中無值。

springmvc @RequestBody String類型參數的使用

第四種情況,給Map參數添加@RequestBody注解,且請求方式為Post

出乎意料的也成功傳入了,與@RequestParam注解結果類似,也是LinkedHashMap

springmvc @RequestBody String類型參數的使用

springmvc @RequestBody String類型參數的使用

復雜點的Json數據也能解析接收成功

springmvc @RequestBody String類型參數的使用

springmvc @RequestBody String類型參數的使用

小結一下吧

SpringMVC處理請求用Map類型接收參數時,如果參數無注解,則會傳入BindingAwareModelMap類型,等價于Model、ModelMap參數;

參數添加@RequestParam注解時,會將參數包裝稱LinkedHashMap對象,參數的key為Map的key,參數值為Map的key,支持Get、Post方法(應該支持Put、Delete,沒有測,倆方法與Post類似);

添加@RequestBody注解時,接收Json類型數據,也會包裝成LinkedHashMap對象,該注解不支持Get請求,Get請求沒有請求體不能傳Json。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/yangxingzou/article/details/54092578

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产美女做爰免费视频软件 | gay男男白袜chinese | 男人疯狂进女人下部视频动漫 | 免费国产之a视频 | 亚洲AV久久无码精品九号软件 | 欧美三级一区二区 | 国产成人精品一区 | 91噜噜噜在线观看 | 韩国甜性涩爱在线播放 | 国产玖玖在线 | 千金肉奴隶免费观看 | 四虎成人永久地址 | 99九九成人免费视频精品 | 欧美性理论片在线观看片免费 | 激情婷婷成人亚洲综合 | 鬼吹灯天星术在线高清观看 | 国产一级视频在线观看 | 国产成人精品免费 | 亚洲欧美在线免费观看 | 国产精品日本一区二区三区在线看 | 丝瓜视频黄瓜视频 | 91av最新地址 | h片免费网站 | 亚洲国产精品久久久久 | 免费视频精品一区二区 | 久久99视热频国只有精品 | 好紧好爽再叫浪一点点潘金莲 | 免费精品视频在线 | 调教扩张宫颈女人惨叫 | ai换脸杨颖啪啪免费网站 | 亚洲 欧美 在线观看 | 精品国产乱码久久久久久软件 | ts人妖系列在线专区 | 忘忧草在线社区WWW日本直播 | 天堂欧美 | 图片亚洲va欧美va国产综合 | 亚洲第一在线播放 | 日本免费v片一二三区 | 蛮荒的童话未删减在线观看 | 亚洲精品国产福利片 | 日韩毛片基地一区二区三区 |