Darlyn
Darlyn

Reputation: 4938

Content type not support java Spring

I have created a route

@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(){};
}

And i am using postman to send data to it:

headers:

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

data:

name:Greet
password:Me

Yet i keep getting erorr:

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

I have read various questions and post about this but i fail to find solution to fix this.

All help is appreciated, thanks.

Upvotes: 0

Views: 202

Answers (2)

Abhilekh Singh
Abhilekh Singh

Reputation: 2963

In Spring don't use @RequestBody annotation when consuming API with Content-type: 'application/x-www-form-urlencoded'.

You have to use MultiValueMap in case of application/x-www-form-urlencoded.

Upvotes: 1

Tahir Hussain Mir
Tahir Hussain Mir

Reputation: 2626

Whenever we use MediaType.APPLICATION_FORM_URLENCODED_VALUE Spring does not understand it as a RequestBody. So remove RequestBody and do like this

@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());

}

Upvotes: 1

Related Questions