Anton Chertash
Anton Chertash

Reputation: 663

Spring MVC 415 error when posting form

Getting 415 error when doing POST request. This is my form:

<c:url value="/createOrUpdate" var="actionUrl"/>
  <form  action="${actionUrl}" method="post" enctype="application/x-www-form-urlencoded">
    <table class="editor silver">
      <tr>
        <th colspan="2">Edit advert details</th>
        <input type="number" hidden value="${advert.id}" name="id"/>
      </tr>
      <tr>
        <td>Owner:</td>
        <td>
          <input type="text" disabled value="${advert.owner}" name="owner"/>
        </td>
      </tr>
      <tr>
        <td>Publication date:</td>
        <td>
          <input type="datetime" disabled value="${advert.publicationDate}" name="publicationDate"/>
        </td>
      </tr>
      <tr>
        <td>Select rubric</td>
        <td>
          <select name="rubric">
            <option value="sale" selected>Sale</option>
            <option value="buy">Buy</option>
            <option value="lease">Lease</option>
            <option value="services">Services</option>
            <option value="dating">Dating</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>Enter header:</td>
        <td>
          <input type="text" value="${advert.header}" name="header"/>
        </td>
      </tr>
      <tr>
        <td>Enter text:</td>
        <td>
          <input type="text" value="${advert.text}" name="text"/>
        </td>
      </tr>

And this is controller where i'm doing request

@RequestMapping(value = "/createOrUpdate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ModelAndView editAdvert(@RequestBody Advert advert) {//some actions

Advert is a simple POJO with getters and setters:

private long id;

private String owner;

private Date publicationDate;

private String rubric;

private String header;

private String text;

What i'm doing wrong? p.s. As i understand spring should map that 'name-fields' into fields of POJO. Right? Added Ok, i found that some values didn't send in form. I checked console in browser and now it's ok, but still i get 415 error.

Upvotes: 0

Views: 2305

Answers (2)

Gurkan Yesilyurt
Gurkan Yesilyurt

Reputation: 2675

You can use @ModelAttribute instead of @RequestBody. RequestBody consume json,xml. Remove application/x-www-form-urlencoded because it is already default type.

If you want to use @Requestbody then you post your form data as json, using a JavaScript-library like JQuery, you would post a JSON data with Content-Type:application/json. And use jackson-lib convert json to java object easily.

400 - bad request

The problem might be date sending. Focus on how to send date spring mvc. You can use @DateTimeFormat like this or @InıtBinder

Upvotes: 5

Anton Chertash
Anton Chertash

Reputation: 663

The problem was in date conversion. Change @RequestBody to @MadelAttribute and put @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) private Date publicationDate; to a model

Upvotes: 1

Related Questions