我正在尝试使用AJAX调用将表单提交给Spring控制器。尽管尝试了不同的选项,我总是得到400 Bad Request“客户发送的请求在语法上是不正确的。”响应。这是我用来发送数据的JavaScript代码:
$('#addNewTransactionForm').submit(function(event) {
var transaction = new Object();
transaction.date = $('#date').val();
transaction.value = $('#value').val();
transaction.categoryID = $('#categoryID').val();
transaction.accountToID = $('#accountTo').val();
transaction.accountFromID = $('#accountFrom').val();
transaction.description = $('#description').val();
$.ajax({
url: "${pageContext.request.contextPath}/transactions/create.json",
data: JSON.stringify(transaction),
type: "POST",
contentType: "application/json; charset=utf-8",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
}
});
event.preventDefault();
});
这是Spring控制器方法:
@RequestMapping(value = "/create", method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody TransactionFO createTransaction(@RequestBody @Valid TransactionFO transaction, final Principal principal) {
logger.info("createTransaction");
String name = principal.getName();
return transactionService.addNewTransaction(transaction, name);
}
这是bean代码:
public class TransactionFO {
private Date date;
@Digits(integer=8, fraction=4)
private BigDecimal value;
@NotNull
private Integer categoryID;
private Integer accountToID;
private Integer accountFromID;
@Length(min=0, max=255)
private String description;
// getters and setters
}
我很困惑,因为我遵循this教程,代码似乎与我完全相同。我在.xml Spring配置文件中跟随Jackson配置:
<beans:bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></beans:bean>
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jacksonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>
我注意到如果我将TransactionFO类型更改为String,则请求会正确传递。但是,这不是一个理想的行为,在这种情况下,我将被迫手动解析JSON字符串。任何帮助将非常感激,我已经在这个问题上失去了2天。
Chrome控制台中的数据:
Request URL:http://localhost:8080/finager/transactions/create.json
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json
Accept-Encoding:gzip,deflate,sdch
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:127
Content-Type:application/json
Cookie:JSESSIONID=31B00AA068448DB235C26F023F60A0BD
DNT:1
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/finager/transactions
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/32.0.1700.77 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source
{date:Sun Feb 02 17:22:29 CET 2014, value:0, categoryID:100, accountToID:35,accountFromID:null,…}
accountFromID: null
accountToID: "35"
categoryID: "100"
date: "Sun Feb 02 17:22:29 CET 2014"
description: ""
value: "0"
非常感谢你。
PS。通过AJAX向用户发送验证信息以避免重新加载页面的正确方法是什么?我必须执行Hibernate Validator注释无法处理的复杂验证。
答案 0 :(得分:1)
在我看来,您发送的格式不正确。尝试使用
发送请求transaction.date = '2014-02-02';
还有一个提示。尝试加载springframework的源代码(如果你使用maven run mvn dependency:sources
)并在MappingJacksonHttpMessageConverter#readInternal
中放置断点。调用此方法用于解析json。
答案 1 :(得分:0)
你是否在类路径中拥有所有需要的杰克逊罐子?例如,对于Jackson 2.1,需要以下工件:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
根据杰克逊版本,工件的确切列表可能会有所不同。