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

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

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

服務器之家 - 編程語言 - Java教程 - SpringMVC @RequestBody 為null問題的排查及解決

SpringMVC @RequestBody 為null問題的排查及解決

2022-02-25 00:42瘋狂哈丘 Java教程

這篇文章主要介紹了SpringMVC @RequestBody 為null問題的排查及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SpringMVC @RequestBody為null

今天寫一個springmvc接口,希望入參為json,然后自動轉成自己定義的封裝對象,于是有了下面的代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@PostMapping("/update")
@ApiOperation("更新用戶信息")
public CumResponseBody update(@RequestBody UserInfoParam param) {
     int userId = getUserId();
     userService.updateUserInfo(userId, param);
     return ResponseFactory.createSuccessResponse("ok");
}
//UserInfoParam.java
public class UserInfoParam {
    private String tel;
    private String email;
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

程序正常啟動后,使用swaggerUI發起測試

?
1
2
3
4
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
   "email": "12%40mail.com", \
   "tel": "13677682911" \
 }' 'http://127.0.0.1:9998/api/user/update'

最后程序報錯

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.pingguiyuan.shop.common.response.CumResponseBody com.pingguiyuan.shop.weixinapi.controller.UserController.update(com.pingguiyuan.shop.common.param.weixin.UserInfoParam)

對了幾遍,接口的編寫和請求內容都確定沒有問題,但是請求的json就是沒注入進來轉成param對象。查了一圈資料也沒找到滿意的答案,就只能給springMVC的源碼打斷點跟一遍,看一下具體是哪里出了問題。

由于本篇不是介紹springMVC實現原理的,就不具體介紹springMVC的源碼。

最后斷點發現springMVC從request的inputstream沒取出內容來(inputstream.read()出來的直接是-1)。由于有在一個攔截器輸出請求的參數內容—>【當請求時get時,通過request.getParameterMap();獲取參數,當請求時post時,則是直接輸出request的inpustream里面的內容】。所以請求的body里面是肯定有內容的,也就是說request.getInputstream()的流是有內容的,那為什么到springMVC這read出來的就是-1呢。

稍微理了下思路,發現是自己給自己挖了個坑。答案是:request的inputstream只能讀一次,博主在攔截器中把inputstream的內容都輸出來了,到springMVC這,就沒有內容可以讀了。

關于inputsteam的一些理解

servlet request的inpustream是面向流的,這意味著讀取該inputstream時是一個字節一個字節讀的,直到整個流的字節全部讀回來,這期間沒有對這些數據做任何緩存。因此,整個流一旦被讀完,是無法再繼續讀的。

這和nio的處理方式就完全不同,如果是nio的話,數據是先被讀取到一塊緩存中,然后程序去讀取這塊緩存的內容,這時候就允許程序重復讀取緩存的內容,比如mark()然后reset()或者直接clear()重新讀。

特意去看了下InputStream的源碼,發現其實是有mark()和reset()方法的,但是默認的實現表示這是不能用的,源碼如下

?
1
2
3
4
5
6
7
public boolean markSupported() {
    return false;
}
public synchronized void reset() throws IOException {
        throw new IOException("mark/reset not supported");
}
public synchronized void mark(int readlimit) {}

其中mark是一個空函數,reset函數直接拋出異常。同時,inputstream還提供了markSupported()方法,默認是返回false,表示不支持mark,也就是標記(用于重新讀)。

但是并不是所有的Inputstream實現都不允許重復讀,比如BufferedInputStream就是允許重復讀的,從類名來看,就知道這個類其實就是將讀出來的數據進行緩存,來達到可以重復讀的效果。下面是BufferedInputStream重寫的3個方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public synchronized void mark(int readlimit) {
    marklimit = readlimit;
    markpos = pos;
}
public synchronized void reset() throws IOException {
    getBufIfOpen(); // Cause exception if closed
    if (markpos < 0)
        throw new IOException("Resetting to invalid mark");
    pos = markpos;
}
public boolean markSupported() {
    return true;
}

可以看到BufferedInputStream的markSupported()方法返回的是true,說明它應該是支持重復讀的。我們可以通過mark()和reset()來實現重復讀的效果。

@RequestBody 自動映射原理的簡單介紹

springMVC在處理請求時,先找到對應controller處理該請求的方法,然后遍歷整個方法的所有參數,進行封裝。在處理參數的過程中,會調用AbstractMessageConverterMethodArgumentResolver#readWithMessageConverters()類的方法進行進行一些轉換操作,源碼如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter,
            Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {
        MediaType contentType;
        boolean noContentType = false;
        try {
            contentType = inputMessage.getHeaders().getContentType();
        }
        catch (InvalidMediaTypeException ex) {
            throw new HttpMediaTypeNotSupportedException(ex.getMessage());
        }
        if (contentType == null) {
            noContentType = true;
            contentType = MediaType.APPLICATION_OCTET_STREAM;
        }
        Class<?> contextClass = parameter.getContainingClass();
        Class<T> targetClass = (targetType instanceof Class ? (Class<T>) targetType : null);
        if (targetClass == null) {
            ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
            targetClass = (Class<T>) resolvableType.resolve();
        }
        HttpMethod httpMethod = (inputMessage instanceof HttpRequest ? ((HttpRequest) inputMessage).getMethod() : null);
        Object body = NO_VALUE;
        EmptyBodyCheckingHttpInputMessage message;
        try {
            message = new EmptyBodyCheckingHttpInputMessage(inputMessage);
            for (HttpMessageConverter<?> converter : this.messageConverters) {
                Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter.getClass();
                GenericHttpMessageConverter<?> genericConverter =
                        (converter instanceof GenericHttpMessageConverter ? (GenericHttpMessageConverter<?>) converter : null);
                if (genericConverter != null ? genericConverter.canRead(targetType, contextClass, contentType) :
                        (targetClass != null && converter.canRead(targetClass, contentType))) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
                    }
                    if (message.hasBody()) {
                        HttpInputMessage msgToUse =
                                getAdvice().beforeBodyRead(message, parameter, targetType, converterType);
                        body = (genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) :
                                ((HttpMessageConverter<T>) converter).read(targetClass, msgToUse));
                        body = getAdvice().afterBodyRead(body, msgToUse, parameter, targetType, converterType);
                    }
                    else {
                        body = getAdvice().handleEmptyBody(null, message, parameter, targetType, converterType);
                    }
                    break;
                }
            }
        }
        catch (IOException ex) {
            throw new HttpMessageNotReadableException("I/O error while reading input message", ex);
        }
        if (body == NO_VALUE) {
            if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) ||
                    (noContentType && !message.hasBody())) {
                return null;
            }
            throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
        }
        return body;
    }

上面這段代碼主要做的事情大概就是獲取請求的contentType,然后遍歷配置的HttpMessageConverter—>this.messageConverters,如果該HttpMessageConverter可以用于解析這種contentType(genericConverter.canRead方法),就用這種HttpMessageConverter解析請求的請求體內容,最后返回具體的對象。

在spring5.0.7版本中,messageConverters默認似乎配置了8種convert。分別是

  • ByteArrayMessageConverter
  • StringHttpMessageConverter
  • ResourceHttpMessageConverter
  • ResourceRegionHttpMessageConverter
  • SourceHttpMessageConverter
  • AllEncompassingFormHttpMessageConverter
  • MappingJackson2HttpMessageConverter
  • Jaxb2RootElementHttpMessageConverter

具體的convert是哪些contentType并怎么解析的,這里不多做介紹,感興趣的朋友可以自行查看源碼。

比如我們請求的header中的contentType是application/json,那么在遍歷messageConverters的時候,其他genericConverter.canRead()都會返回false,說明沒有適配上。

然后遍歷到MappingJackson2HttpMessageConverter時genericConverter.canRead()返回true,接著就去獲取請求的請求體,并通過json解析成我們@RequestBody定義的對象。

因此,如果我們的請求的contentType和數據協議都是自定義的,我們完全可以自己實現一個HttpMessageConverter,然后解析特定的contentType。

最后記得將這個實現放入messageConverters中,這樣springMVC就會自動幫我們把請求內容解析成對象了。

關于@requestBody的一些說明

1、@requestBody注解

常用來處理content-type不是默認的application/x-www-form-urlcoded編碼的內容,比如說:application/json或者是application/xml等。一般情況下來說常用其來處理application/json類型。

2、通過@requestBody

可以將請求體中的JSON字符串綁定到相應的bean上,當然也可以將其分別綁定到對應的字符串上。

例如說以下情況:

?
1
2
3
4
5
6
7
8
9
10
$.ajax({       
         url:"/login",       
         type:"POST",        
          data:'{"userName":"admin","pwd","admin123"}',      
        content-type:"application/json charset=utf-8",        
          success:function(data)
             {          
                alert("request success ! ");       
            }    
});
?
1
2
3
4
@requestMapping("/login")    
  public void login(@requestBody String userName,@requestBody String pwd){      
  System.out.println(userName+" :"+pwd);    
}

這種情況是將JSON字符串中的兩個變量的值分別賦予了兩個字符串,但是呢假如我有一個User類,擁有如下字段:

?
1
2
String userName;
String pwd;

那么上述參數可以改為以下形式:@requestBody User user 這種形式會將JSON字符串中的值賦予user中對應的屬性上

需要注意的是,JSON字符串中的key必須對應user中的屬性名,否則是請求不過去的。

3、在一些特殊情況

@requestBody也可以用來處理content-type類型為application/x-www-form-urlcoded的內容,只不過這種方式不是很常用,在處理這類請求的時候,@requestBody會將處理結果放到一個MultiValueMap<String,String>中,這種情況一般在特殊情況下才會使用,例如jQuery easyUI的datagrid請求數據的時候需要使用到這種方式、小型項目只創建一個POJO類的話也可以使用這種接受方式。

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

原文鏈接:https://blog.csdn.net/u013332124/article/details/82630741

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91精品国产高清久久久久 | 窝窝影院午夜色在线视频 | 加勒比成人 | 麻豆网站在线观看 | 99久久综合九九亚洲 | 天天爱天天操天天射 | 男人操女生 | 日韩亚洲国产激情在线观看 | 亚洲伦理一区 | 久久亚洲精品AV成人无码 | 国产实拍会所女技师在线 | 洗濯屋H纯肉动漫在线观看 武侠艳妇屈辱的张开双腿 午夜在线观看免费观看 视频 | 亚洲国产精品日韩高清秒播 | 摸咪网在线影院在线观看 | 成人国产第一区在线观看 | 19+韩国女主播激情vip视频在线 | 国产日韩一区二区三区在线播放 | 美女bbxx美女bbb | 日日综合 | 免费成人在线观看视频 | 美女光屁股网站 | 国产香蕉一区二区在线网站 | 国产福利在线免费观看 | 国产成人精品777 | 天天插在线视频 | 日本一区二区不卡久久入口 | 乌克兰18sex性hd | 日韩免费观看 | 欧美爽妇 | 人人爱天天做夜夜爽88 | 我的家教老师在线观看 | 日本不卡1卡2卡三卡网站二百 | 女人爽到喷水的视频免费看 | 亚洲精品短视频 | 欧美日韩专区国产精品 | 久久精品无码人妻无码AV蜜臀 | 免费观看在线永久免费xx视频 | 办公室出轨秘书高h | 九九热在线视频观看这里只有精品 | 深夜福利一区 | 国内精品久久久久影院男同志 |