2017-03-04 89 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()); 

}