0

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..

  • @Surely I have made those changes jQuery.ajax({ 'type': type, 'url': url, 'headers': header ? header : {}, 'contentType': contentType, 'data': JSON.stringify(content), 'success': success, 'error': error }); – Subhabrata Mondal Jun 20 '17 at 12:56
  • https://stackoverflow.com/questions/7181534/http-post-using-json-in-java. What is the whole message? you have two application/XXX values. Have you tried curl from the command line? – bichito Jun 20 '17 at 12:59
  • Thank you. I have solve this issue. I have specified , 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 – Subhabrata Mondal Jun 21 '17 at 06:53

5 Answers5

0

You need to set dataType: "json" inside your jquery ajax call.

secondbreakfast
  • 4,194
  • 5
  • 47
  • 101
0

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

M.Navy
  • 155
  • 7
0

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.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

Remove consumes = MediaType.APPLICATION_JSON_VALUE from the request mapping and it should start working.

kk.
  • 3,747
  • 12
  • 36
  • 67
0

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.