SpringMVC—将JSON对象从Ajax post请求映射到Java对象
我试图发出一个AJAX post请求,但是我的JSON对象值没有从我的控制器映射到Java对象。调试Java对象字段时,返回的值为null。请参阅下面的代码 AJAX请求SpringMVC—将JSON对象从Ajax post请求映射到Java对象,java,jquery,ajax,json,spring,Java,Jquery,Ajax,Json,Spring,我试图发出一个AJAX post请求,但是我的JSON对象值没有从我的控制器映射到Java对象。调试Java对象字段时,返回的值为null。请参阅下面的代码 AJAX请求 $('#form').submit(function(e) { e.preventDefault(); var account = {}; account.type = $('#account-type option:selected').text(); account.name = $('#a
$('#form').submit(function(e) {
e.preventDefault();
var account = {};
account.type = $('#account-type option:selected').text();
account.name = $('#account-names option:selected').text();
account.amount = $(this).find('input[name=amount]').val();
$.ajax({
contentType: 'application/json',
url: '/spring-mvc-practice/account/create',
type: 'POST',
dataType: 'json',
data: JSON.stringify(account),
success: function(response) {
console.log("success");
},
error: function() {
console.log("error");
}
});
});
AccountController.java
@Controller
public class AccountController {
@RequestMapping(value = "/account/create", method = RequestMethod.POST, headers = {"Content-type=application/json"})
@ResponseBody
public String createAccount(@ModelAttribute Account account) {
System.out.println("name = " + account.getName());
System.out.println("type = " + account.getType());
System.out.println("amount = " + account.getAmount());
return null;
}
}
Account.java
public class Account {
private String name;
private String type;
private double amount;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
调试结果:
name=nulltype=null
金额=0.0 我还尝试将控制器方法中的
@modeldattribute
更改为@RequestBody
(根据本教程:),但在发出AJAX请求时出现以下错误:
POST http://localhost:8080/spring-mvc-practice/account/create 415 (Unsupported Media Type)
任何帮助都将不胜感激。谢谢。请尝试将您的paramaeter帐户分配为类似以下内容。因为现在您将它们作为数组传递,该数组主要用于传递列表。因此,例如,您想要列出帐户,然后您将使用上面的语法 但现在请尝试以下方法:
var account = {type: $('#account-type option:selected').text(),name: $('#account-names option:selected').text(),amount : $(this).find('input[name=amount]').val()};
如果您想使用
@RequestBody
,只需将createAccount
方法替换为:
@ResponseBody
@RequestMapping(value = "/account/create", method = RequestMethod.POST)
public String createAccount(@RequestBody final Account account) {
System.out.println("name = " + account.getName());
System.out.println("type = " + account.getType());
System.out.println("amount = " + account.getAmount());
return null;
}
…您的AJAX调用应该具有:
$.ajax({
contentType: 'application/json;charset=UTF-8',
url: '/spring-mvc-practice/account/create',
dataType: 'json',
type: 'POST',
cache: false, // Force requested pages not to be cached by the browser
processData: false, // Avoid making query string instead of JSON
data: JSON.stringify(account)
}).done(function (data) {
console.log('AJAX call was successfully executed ;)');
console.log('data = ', data);
}).fail(function () {
console.log('AJAX call failed :(');
});
…最重要的是,您必须在类路径中具有此依赖关系(您可以使用版本,以防无法使用特定版本…请注意,我使用的是Maven):
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
com.fasterxml.jackson.core
杰克逊数据绑定
2.6.0
它是不是一个Spring Boot应用程序?嗨,我是Spring新手——我想我没有使用Spring Boot。我正在Tomcat上运行我的应用程序。好吧,我想你必须配置一个ObjectMapper。(当您使用Spring Boot时,它和许多其他东西都会自动配置,我认为使用Spring Boot是学习Spring的更好方法,而不是不使用它)尝试控制台。log(account)在$.ajax调用之前,签入浏览器控制台是指数据正在传递到java。是的,account JSON对象在控制台中打印出来,但仍然不起作用。我的Java对象中仍有空值。@David您是否从浏览器开发工具检查了发送的请求?在网络选项卡中,您可以看到它发送到控制器的json字符串是什么。尝试将内容类型设置为contentType:“application/json;charset=utf-8”,在网络选项卡中,我在请求负载{type:“Savings”,name:“Savings Account”,amount:“23123”}中看到json@David尝试保留contentType:“application/json”remove charset=utf-8I用您的代码替换了我的代码,并添加了依赖项,但我在执行AJAX请求时遇到了这个错误:POST 415(不支持的媒体类型)@DavidFoster,这意味着您无法将帐户
序列化为json;仔细检查类路径中是否有jackson数据绑定
。你在用Maven吗?另外,您使用的是哪个版本的Spring?我使用的是Maven,jackson数据绑定jar与其他Spring jar位于同一个lib文件夹中。我使用的是Spring版本4.1。7@DavidFosterMaven的项目中没有/lib
文件夹……除非您正在谈论生成的WAR/EAR文件中的/lib
文件夹是WAR中的/lib文件夹