415 unsupported media type
Solution 1
No need to set headers in @RequestMapping
. Post request from Form
with enctype="multipart/form-data"
is by default consider as a multipart request in Controller
.
So here your request is multipart already, you just need to to add some maven repository for file upload to support your multipart-data
.
Refer this Spring file upload documentation and Go through this example
Solution 2
If you have not done this already, try adding multipartResolver
bean in application context. Add this in application / servlet context xml file.
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
And supporting jar file commons-fileupload jar
. Maven dependency for this jar is :
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
Solution 3
Try to change :
headers = "content-type=multipart/form-data"
to :
consumes="multipart/form-data"
So your @RequestMapping
will become like this :
@RequestMapping(value = "/sms/mass",
method = RequestMethod.POST,
consumes="multipart/form-data")
Solution 4
Try adding exception handler to the controller. It will help you to identify the real cause of the issue:
@ExceptionHandler({Exception.class})
public void resolveException(Exception e) {
e.printStackTrace();
}
Related videos on Youtube
![Tony](https://i.stack.imgur.com/BmRpf.jpg?s=256&g=1)
Comments
-
Tony about 2 years
I have got a form with file upload and sometimes I'm catching
415 unsupported media type
with no messages in log. This is my form:<form:form method="post" enctype="multipart/form-data" class="form-horizontal" modelAttribute="massSMSForm" style="width: 650px;"> <c:if test="${!empty errorFlag}"> <div class="alert alert-danger"> <a href="#" class="close" data-dismiss="alert">×</a> <b>Ошибка. </b> <form:errors path="*" /> </div> </c:if> <div class="well" style="width: 650px;"> <table style="width: 100%"> <tr> <td align="left"><b>Наименование рассылки*</b></td> <td align="right"><form:input path="title" type="text" class="form-control" id="title" style="width:340px;" maxlength="15" /><br /></td> <tr> <td align="left"><b>Отправитель*</b></td> <td align="right"><form:input path="from" type="text" maxlength="15" class="form-control" id="from" style="width:340px;" /><br /></td> </tr> <tr> <td align="left"><b>Дата начала рассылки</b></td> <td align="right"><form:input id="deliveryDate" path="deliveryDate" type="text" autocomplete="off" class="form-control" placeholder="сейчас" style="width:310px;" /><br /></td> </tr> <tr> <td align="left"><b>Срок жизни</b></td> <td align="right"><form:input id="expiration" path="expiration" type="number" min="1" max="72" autocomplete="off" class="form-control" placeholder="в часах" style="width:310px;" /><br /></td> </tr> <tr> <td align="left"><b>Файл рассылки*</b></td> <td align="right"><input type="file" name="file" accept="text/xml, text/plain" id="mass" /><br /></td> </tr> </table> <br /> <b>Текст сообщения рассылки</b><br /> <br /> <c:choose> <c:when test="${massSMSForm.ignore==false}"> <form:textarea path="message" class="form-control" rows="5" id="message" placeholder="..." maxlength="500" /> </c:when> <c:otherwise> <form:textarea path="message" class="form-control" rows="5" id="message" placeholder="..." disabled="true" maxlength="500" /> </c:otherwise> </c:choose> <br /> <form:checkbox path="ignore" id="ignoreBox" /> <small>Игнорировать сообщение</small> </div> <div style="text-align: right"> <a href="/${initParam['appName']}/userRole/" class="btn btn-link btn-sm">Назад</a><input class="btn btn-success btn-sm" type="submit" name="send" onclick="myAlert()" value="Отправить" /> </div> </form:form> <script type="text/javascript"> function confirmSend() { if (confirm("Отправляем сообщения?")) { return true; } else { return false; } } </script> <script type="text/javascript"> jQuery('#deliveryDate') .datetimepicker( { lang : 'ru', i18n : { ru : { months : [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ], dayOfWeek : [ "Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб", ] } }, dayOfWeekStart : 1, timepicker : true, format : 'Y-m-d H:i' }); </script> <script type="text/javascript"> document.getElementById('ignoreBox').onchange = function() { document.getElementById('message').disabled = this.checked; }; </script>
And method:
@RequestMapping(value = "/sms/mass", method = RequestMethod.POST, headers = "content-type=multipart/form-data") public String massSMSProcess(Map<String, Object> map, @ModelAttribute("massSMSForm") MassSMSForm massSMSForm, BindingResult result, HttpSession session) throws IOException { session.removeAttribute(SESSION_KEY); massSMSFormValidator.validate(massSMSForm, result); if (result.hasErrors()) { map.put("errorFlag", true); return massSMSPage(map, massSMSForm, session); } else { try { SMS sms = SmsBuilder.build(massSMSForm); session.setAttribute(SESSION_KEY, sms); map.put("title", "Подтверждение операции"); map.put("count", sms.getSmsEntityList().size()); } catch (IOException e) { // mailer.send("/sms/mass :" + e.getMessage() + "form:" // + massSMSForm); map.put("error", "Ошибка ввода/вывода"); return distributionListPage(map); } return "confirmMassSMSPage"; } }
What is wrong? Is it js problem or what? Should I add new headers? What's your thoughts?
-
Sotirios Delimanolis almost 10 yearsTurn your logs to DEBUG.
-