我有簡單的登錄控制器。在它裏面,我有一個叫做signIn()的動作方法。我正在提交我的憑證以通過ajax呼叫登錄到網站。Spring MVC中不受支持的媒體類型
這是我的AJAX調用程序功能 -
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);
,其中 '人' 的數據,如 -
{"emailId":"[email protected]","password":"12345"}
並登錄控制器的登入()方法的代碼是─
@PostMapping(value = "/login",consumes = MediaType.APPLICATION_JSON_VALUE)
public String signIn(@RequestBody Person person) {
if(person.getEmailId().equals("[email protected]") && person.getPassword().equals("12345")){
return "Success";
}
else{
return "Invalid";
}
}
我已創建簡單的人POJO類 -
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);
}
}
現在,當我嘗試登錄時,它給我415不支持的媒體類型錯誤。你能幫我解釋爲什麼這個錯誤發生了嗎?在此先感謝..
@Surely我已經作出這些變化jQuery.ajax({ '類型':類型, 'URL':URL, '報頭'?:頭標頭:{}, '的contentType':contentType中, ' data':JSON.stringify(content), 'success':success, 'error':error }); –
https://stackoverflow.com/questions/7181534/http-post-using-json-in-java。什麼是整個信息?你有兩個應用程序/ XXX值。你有沒有嘗試從命令行卷曲? – efekctive
謝謝。我已經解決了這個問題。我已經指定了 ,但我忘了提及將使用哪個HttpMessageConverter。我需要一個依賴到Jackson庫(jackson-databind),以便我可以將HttpRequestBody轉換爲Person對象,現在,我可以支持MappingJackson2HttpMessageConverter –