简体   繁体   中英

Unsupported Media Type in Spring MVC

I have simple Login Controller. With in it, I have one action method called signIn(). I am submitting my credential to log into the website through ajax call.

This is my ajax caller function-

var AJAXCaller = function () {
};

AJAXCaller.prototype.call = function (type, uri, header, contentType, content, success, error) {
    var config = new SSKSConfig();
    var url = config.getLocation() + uri;
    return jQuery.ajax({
        'type': type,
        'url': url,
        'headers': header,
        'data': JSON.stringify(content),
        'success': success,
        'error': error
    });
};
var caller= new AJAXCaller();
caller.call('POST', '/login', { 'Content-Type': 'application/json' }, 'application/x-www-form-urlencoded', person, fnSuccess, fnError);

where 'person' data like -

{"emailId":"aaa@gmail.com","password":"12345"}

And login controller's signIn() method code is-

@PostMapping(value = "/login",consumes = MediaType.APPLICATION_JSON_VALUE)
    public String signIn(@RequestBody Person person) {
        if(person.getEmailId().equals("aaa@gmail.com") && person.getPassword().equals("12345")){
            return "Success";
        }
        else{
            return "Invalid";
        }
    }

I have created simple Person POJO class-

public class Person implements PersonSupport, AddressSupport {
    private String code;
    private String name;
    private String emailId;
    private String password;
    private String contactNo;
    private final Address address = new Address();

    public Person() {
    }

    @Override
    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String getCode() {
        return this.code;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setContactNo(String contactNo) {
        this.contactNo = contactNo;
    }

    @Override
    public String getContactNo() {
        return contactNo;
    }

    @Override
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

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

    @Override
    public String getEmailId() {
        return emailId;
    }

    @Override
    public String getRoadNo() {
        return address.getRoadNo();
    }

    @Override
    public String getRoadName() {
        return address.getRoadName();
    }

    @Override
    public String getCity() {
        return address.getCity();
    }

    @Override
    public String getPinCode() {
        return address.getPinCode();
    }

    @Override
    public StateList getStateList() {
        return address.getStateList();
    }

    @Override
    public void setRoadNo(String roadNo) {
        address.setRoadNo(roadNo);
    }

    @Override
    public void setRoadName(String roadName) {
        address.setRoadName(roadName);
    }

    @Override
    public void setCity(String city) {
        address.setCity(city);
    }

    @Override
    public void setPinCode(String pinCode) {
        address.setPinCode(pinCode);
    }

    @Override
    public void setStateList(StateList stateList) {
        address.setStateList(stateList);
    }

}

Now when i am try to login it gives me 415 Unsupported Media Type error. Can you help me why this error has occurred??? Thanks in advance..

您需要在jquery ajax调用中设置dataType: "json"

use

@PostMapping(value = "/login",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)

since the data will be UTF encoded you have to use this value or just remove the consumes part

Mixture of headers and content type

$.ajax({
    type: type,
    url: url,
    data: JSON.stringify(content),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
 ...
});

You pass contentType ='application/x-www-form-urlencoded' . You can still use the contentType variable but pass correct value. No need to use header in case of contentType.

从请求映射中删除consumes = MediaType.APPLICATION_JSON_VALUE ,它应该开始工作。

Thank you. I have solve this issue. I have specified <mvc:annotation-driven/> , but i forgot to mentioned which HttpMessageConverter will be used. I need a dependency to Jackson library (jackson-databind) so that I can convert HttpRequestBody to Person object and now, i can take support for MappingJackson2HttpMessageConverter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM