2017-03-04 91 views
0

我創建了一個路線內容類型不支持java春天

@RequestMapping(value = "login" , method = RequestMethod.POST , consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 
public void greet(@RequestBody Login l){ 
    System.out.println(l.getName()); 
} 

class Login{ 
    public void setName(String name) { 
     this.name = name; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    private String name; 
    private String password; 

    public String getName() { 
     return name; 
    } 

    public String getPassword() { 
     return password; 
    } 



    public Login(){}; 
} 

,我使用郵遞員將數據發送給它:

標題:

'Accept':application/x-www-form-urlencoded 
Content-Type:application/x-www-form-urlencoded 

數據:

name:Greet 
password:Me 

但是我不斷收到erorr:

"message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported", 

我已閱讀各種問題和帖子,但我未能找到解決方案來解決這個問題。

所有幫助表示讚賞,謝謝。

回答

1

在使用API​​和Content-type: 'application/x-www-form-urlencoded'時,Spring不使用@RequestBody註釋。

如果是application/x-www-form-urlencoded,則必須使用MultiValueMap

+0

,該類在這種情況下,空 – Darlyn

+0

使用地圖。 –

1

無論何時我們使用MediaType.APPLICATION_FORM_URLENCODED_VALUE Spring並不瞭解它爲RequestBody。所以刪除RequestBody和不喜歡在這種情況下,這個

@RequestMapping(value = "login" , method = RequestMethod.POST , consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 
public void greet(Login l){ 
if(l != null /*or whatever you want to do*/) 
    System.out.println(l.getName()); 

}