0

I am trying to submit form to Spring controller using AJAX call. Despite trying different options, I always get 400 Bad Request "The request sent by the client was syntactically incorrect." response. This is the JavaScript code that I use to send data:

$('#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();
});

This is the Spring controller method:

@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);
}

This is the bean code:

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
}

I am confused because I followed this tutorial and the code appears to be identical to me. I have following Jackson configuration in my .xml Spring configuration file:

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

I have noticed that if I change TransactionFO type to String, the request passes correctly. However, this is not a desired behavior, in this case I will be forced to parse JSON string manually. Any help will be hugely appreciated, I lost 2 days already on this issue.

Data from Chrome console:

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"

Thank you very much in advance.

PS. What is the proper way to send validation info to user via AJAX in order to avoid reloading the page? I have to perform complicated validation that could not be handled by the Hibernate Validator annotations.

7
  • Can you add the TransactionFO source as well? Commented Feb 2, 2014 at 16:18
  • Sure, sorry for that. Commented Feb 2, 2014 at 16:21
  • 3
    You should look at this post: stackoverflow.com/questions/10323957/posting-json-to-rest-api/… to see how to enable trace logging. I suspect Jackson isn't liking your JSON, and this will show you why. Commented Feb 2, 2014 at 16:39
  • if there are any stracktraces in the server, can you post them here, and if too long in pastebin.com? Commented Feb 2, 2014 at 16:45
  • No stack trace related to the issue, that is the main problem. ;) I have the same suspicion as Rob and now I am trying to and SFL4J and Logback and to display the error. Thanks Rob for the hint. Commented Feb 2, 2014 at 17:05

2 Answers 2

1

It seems to me that you are sending a date with incorrect format. Try to send the request with

transaction.date = '2014-02-02';

One more tip. Try to load sources of springframework (if you are using maven run mvn dependency:sources) and put breakpoint in MappingJacksonHttpMessageConverter#readInternal. This method is called for parsing json.

Sign up to request clarification or add additional context in comments.

Comments

0

Do you have all the needed Jackson jars in the classpath? For example for Jackson 2.1, the following artifacts are needed:

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

The exact list of artifacts might be different according to the Jackson version.

1 Comment

Thanks, but it appears that Jackson was imported correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.