2013-08-30 51 views
1

嗯,我試圖在spring控制器中檢索嵌套的JSON並獲取400(錯誤請求)錯誤。發佈嵌套的JSON到彈簧控制器

JSON

{"AuthenticationInfo": 
    {"loginId":"243324","password":"xyz"} 
} 

控制器

@RequestMapping(value = "/login", method = RequestMethod.POST,headers={"Accept=*/*","content-type=application/json"}) 
    @ResponseBody 
    public MySubscriber getSubscriber(@RequestBody MyAuthentication myAuthentication) { 
     LOGGER.log(Level.INFO, "getSubscriber"); 

     System.out.println("getSubscriber method : "+myAuthentication); 


     MySubscriber mySubscriber = helloWebService.getSubscriber(myAuthentication); 
     LOGGER.log(Level.INFO, "mySubscriber : " + mySubscriber); 
     System.out.println("mySubscriber : " + mySubscriber); 
     return mySubscriber; 
    } 

MyAuthentication

public class MyAuthentication extends AuthenticationInfo { 
    private AuthenticationInfo AuthenticationInfo; 

    public AuthenticationInfo getAuthenticationInfo() { 
     return AuthenticationInfo; 
    } 

    public void setAuthenticationInfo(AuthenticationInfo authenticationInfo) { 
     AuthenticationInfo = authenticationInfo; 
    } 

    @Override 
    public String toString() 
    { 
     return "AuthenticationInfo : "+AuthenticationInfo; 
    } 
} 

AuthenticationInfo

public class AuthenticationInfo { 
     private String loginId; 
     private String password; 
     public String getLoginId() { 
      return loginId; 
     } 
     public void setLoginId(String loginId) { 
      this.loginId = loginId; 
     } 
     public String getPassword() { 
      return password; 
     } 
     public void setPassword(String password) { 
      this.password = password; 
     } 

     @Override 
     public String toString() 
     { 
      return "{ loginId : "+loginId+" || password"+password+"}"; 
     } 
    } 

錯誤是當我發射簡單的Json並相應地檢索它。這裏唯一的問題是JSON的

+1

也發佈你的錯誤。 –

+0

我使用REST客戶端擊中請求,並收到此錯誤 HTTP狀態400 - 型狀態報告 消息 說明通過客戶端發送的請求是語法不正確()。 JBoss Web/7.0.13.Final 控制檯上什麼都沒有 –

+0

怎麼回事調試?你可以做調試,並檢查哪一行導致錯誤。 –

回答

1

現在無論我說什麼都聽起來很愚蠢。我不確定這是否只是傑克遜或任何其他JSON庫的行爲。

這將工作,我已經測試它,你只需要改變在MyAuthentication類中聲明的私有屬性的情況。使用類似如下:

private AuthenticationInfo authenticationInfo; 

現在你還必須改變的情況相匹配的要求,因此使用下面的:

{"authenticationInfo":{"loginId":"abcsdsd","password":"adsfsdfsdbcsdsd"}} 

這工作完全正常。

+0

非常感謝你的工作 –

3

嘗試嵌套結構修改MyAuthentication這樣

public static class MyAuthentication extends AuthenticationInfo { 
    @JsonProperty("AuthenticationInfo") 
    private AuthenticationInfo AuthenticationInfo; 

    public IndexController.AuthenticationInfo getAuthenticationInfo() { 
     return AuthenticationInfo; 
    } 

    public void setAuthenticationInfo(IndexController.AuthenticationInfo authenticationInfo) { 
     AuthenticationInfo = authenticationInfo; 
    } 

    @Override 
    public String toString() { 
     return "AuthenticationInfo : " + AuthenticationInfo; 
    } 
} 

傑克遜的默認jsoin屬性開始用小寫字母。

+0

非常感謝,它的工作。我需要這項工作,因爲我的力量已經控制了我們需要的其他請求 –