欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot POST請求接收多個參數(shù)值為null問題

 更新時間:2025年02月12日 09:33:18   作者:老司機張師傅  
SpringBoot接口中POST請求接收JSON數(shù)據(jù)時,使用簡單類型接收會報null,需要封裝成實體類或使用Map(并注明泛型)接收,并且使用@RequestBody注解

問題描述

SpringBoot接口使用Post請求接收前端的JSON數(shù)據(jù)寫了多個參數(shù)

無論怎么寫獲取到的數(shù)據(jù)都是null

      $.ajax({
                            url: "login4",
                            param: JSON.stringify({
                                "name": "name",
                                "age": 10
                            }),
                            type: "post",
                            dataType: "json",
                            success: function (data) {
                            },
                            error: function (data) {
                            }
                        });
    @PostMapping("login4")
    public void login4(String name, Integer age){
        System.out.println("login3");
        System.out.println(name + " -- " + age);
    }

原因分析

POST請求接收JSON數(shù)據(jù)時使用簡單類型(Integer、String)等不能自動填充數(shù)據(jù)

必須要封裝成實體類或者使用Map接收

而且使用Map接收時還要注明泛型

解決方案

類似于以下這種方式

封裝一個實體類(實體類的屬性名要與前端請求參數(shù)對應)或使用Map<String,Object>

前面加上RequestBody注解就能獲取到了

    @RequestMapping("/saveGrAuditLog")
    public void auditUploads(@RequestBody AuditRequestEntity auditRequestEntity, HttpServletRequest request){
        WriteLogParam writeLogParam = new WriteLogParam();
        writeLogParam.setNote(auditRequestEntity.getNote());
        writeLogParam.setOpterationType(auditRequestEntity.getOpterationType());
        writeLogParam.setOptName(auditRequestEntity.getOptName());
        writeLogParam.setResourceId(auditRequestEntity.getResourceId());
        writeLogParam.setResourceName(auditRequestEntity.getResourceName());
        log.info("******params >> "+ JSONUtil.toJsonStr(writeLogParam));
        AuditLogRequestTwo.addAuditNew(request,auditRequestEntity.getParams(),auditRequestEntity.getOptUrl(),auditRequestEntity.getResult(),writeLogParam);
    }

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論