2017-07-05 51 views
-1

我的JSON對象的JSON體發送到後端時,設置@RequestBody看起來是這樣的:發送包含對象的對象和數組

{ 
    "reportAccess":{ 
     "report":{ 
     "id":70, 
     "reportName":"Test Rapport", 
     "created":"2017-06-01", 
     "description":"Test report", 
     "modifiedDate":"2017-06-01T12:10:00.000+0000", 
     "modifiedBy":"111111" 
     }, 
     "accessGroup":{ 
     "id":1, 
     "accessName":"Administrator" 
     }, 
     "validFromDate":"2017-07-24", 
     "validToDate":"2017-07-30" 
    }, 
    "users":[ 
     { 
     "id":111111, 
     "employeeNumber":"123456", 
     "firstName":"Test1", 
     "lastName":"Test11", 
     "windowsId":"t1234567" 
     }, 
     { 
     "id":222222, 
     "employeeNumber":"7891011", 
     "firstName":"Test2", 
     "lastName":"Test22", 
     "windowsId":"t7891011" 
     } 
    ] 
} 

的前端後撥打電話:

function addAccessForMultipleAgents(access, users) { 
      var holderWrapper = { 
       reportAccess: access, 
       users: users 
      }; 
      return utils.post('/api/v1/reportAccesses/all', holderWrapper); 
     } 

後端,我有一個包裝對象,看起來像這樣:

HolderWrapper.java

public class HolderWrapper { 
    private List<User> users; 
    private ReportAccessCustom reportAccess; 

    public HolderWrapper() { 
    } 

    public HolderWrapper(List<User> users, ReportAccessCustom reportAccess) { 
     this.users = users; 
     this.reportAccess = reportAccess; 
    } 

    public List<User> getUsers() { 
     return users; 
    } 

    public ReportAccessCustom getReportAccess() { 
     return reportAccess; 
    } 

    @Override 
    public String toString() { 
     return "HolderWrapper{" + 
       "users=" + users + 
       ", reportAccess=" + reportAccess + 
       '}'; 
    } 
} 

ReportAccessCustom.java

public class ReportAccessCustom { 
    private Report report; 
    private AccessGroup accessGroup; 
    private Date validFromDate; 
    private Date validToDate; 

    public Report getReport() { 
     return report; 
    } 

    public AccessGroup getAccessGroup() { 
     return accessGroup; 
    } 

    public Date getValidFromDate() { 
     return validFromDate; 
    } 

    public Date getValidToDate() { 
     return validToDate; 
    } 
} 

並處理該POST請求的代碼:

@RequestMapping(value = "", method = RequestMethod.POST) 
    public List<ReportAccess> createMultipleAccesses(@RequestBody Map<String, Object> holderWrapper) { 
     List<ReportAccess> response = new ArrayList<>(); 

     HolderWrapper hw = new HolderWrapper((ArrayList<User>) holderWrapper.get("users"), (ReportAccessCustom) holderWrapper.get("reportAccess")); 
     List<User> users = hw.getUsers(); 
     ReportAccessCustom reportAccessCustom = hw.getReportAccess(); 


     //rest of code.. 
     return response; 
    } 

這一切給我留下的錯誤消息:

​​

任何幫助,爲什麼這失敗woul不勝感激。

+0

你爲什麼不定義參數作爲'HolderWrapper holderWrapper'? – Thomas

+0

@Thomas'@RequestBody HolderWrapper holderWrapper'你的意思是?我試過這樣結束了'NullPointerException'。那麼我應該如何建模對象前端? –

回答

1

您的有效負載中存在拼寫錯誤。它有reportAccess雙{

{ 
    "reportAccess": { 
     {     <--- REMOVE THIS AND ... 
      "report": { 
      "id": 70, 
      ... 
      }, 
      "accessGroup": { 
      "id": 1, 
      ... 
      }, 
      "validFromDate": "2017-07-24", 
      "validToDate": "2017-07-30" 
     }    <--- ... THIS ONE TOO 
    }, 
    "users": [ 
     { 
      "id": 111111, 
      ... 
     }, 
     { 
      "id": 222222, 
      ... 
     } 
    ] 
} 

BTW,你不需要使用您的請求主體的地圖,您可以使用HolderWrapper直接:

@RequestMapping(value = "", method = RequestMethod.POST) 
    public List<ReportAccess> createMultipleAccesses(@RequestBody HolderWrapper holderWrapper) { 
    .... 
} 
+0

我的實際代碼沒有這些額外的大括號。寫這個問題時,這是一個格式錯誤。儘管如此。當我直接使用'HolderWrapper'時,我得到一個'NullPointerException'。但如果我這樣做,我應該如何改變我的對象前端? –

+0

你在哪一點得到NPE?請展示痕跡。你使用什麼春季版本?同時顯示你的配置,請 – alfcope

+0

如果我直接使用'HolderWrapper',當我從HolderWrapper實例中使用'getters'時,會得到'NullPointerException'。但如果直接使用。我從前端發送的對象看起來怎麼樣? Springer版本是'1.5.4.RELEASE' –