0

I Have a Controller like this:

@RequestMapping(value = "/user/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody 
public UserLesserDTO createUser(@RequestParam("profileImageFile") MultipartFile file, @RequestBody UserDTO user, @PathVariable("method") String method) {
    // Save profileImage as a profile image and store the image file name in user's profileImage attribute.
    // save user.
}

and I have a HTML form with all user's attributes as <input type="text" name="user attribute name"> plus one <input type="file" name="profileImageFile">

I've receiving an error 415 like: The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.

but when I remove @RequestBody UserDTO user from my createUser() method's signature the upload file part starts to work and I can get the file.

Already read this and this to set a data binder.

@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
    binder.setDisallowedFields("user.profileImage");
    binder.setAllowedFields("profileImageFile");
}

But this changes nothing.

Magno C
  • 1,922
  • 4
  • 28
  • 53
  • Are you using Spring Data? – Paul Warren Jun 10 '18 at 16:09
  • I don’t know. Just what I showed here. Need to see my POM? https://github.com/icemagno/geoinfra/blob/master/cerberus/hades/pom.xml – Magno C Jun 10 '18 at 17:31
  • IMHO Spring is confuse what to do with the attributes. Struts maps model attributes using the model in the html like user.name – Magno C Jun 10 '18 at 17:42
  • @PaulWarren no. I’m implementing my own repository but my question is about the presentation layer and have nothing to do with persistence. Even I have no persistence the problem is: I can’t post a file and a model data from web interface (html) to the controller. Nothing relative to persistence layer. – Magno C Jun 11 '18 at 22:43
  • My God. Every tutorial I have seen in web just posting files OR model data. No one, I said no one have posted both at same time. Now I know why Facebook allow you only update your photo OR profile data but NO both at same time. Jesus! – Magno C Jun 11 '18 at 22:49
  • Sorry. When I said “model” actualy I meant my “user” entity. – Magno C Jun 11 '18 at 22:54
  • That's a shame. Had you been using Spring Data then you could have used the companion community project Spring Content. This would have meant you didn't need to write the controller(s) for updating content and managing its association with an entity. We would be prepared to work with you to make Spring Content's controllers support metadata and content. Let us know if anything changes on that front. – Paul Warren Jun 11 '18 at 23:42
  • I know nothing about Spring Content but read this https://paulcwarren.github.io/spring-content/ and I could not see anything related to `POST a file using HTML`. Again, I just found `PERSISTENCE` stuff. It is not what I need becaus I already know how to store my Entity using Spring JPA. Can you tell me how `Spring Data` and `Spring Content` will help me to receive a file from my frontend along with the attributes of my entity? – Magno C Jun 12 '18 at 01:53
  • And please do not use the term "this is a shame". Although I have chosen Spring JPA, Spring Boot, Spring MVC and Spring Security, I'm not forced to choose what you want. I think I was missunderstood because my poor English and you don't got my real needs. – Magno C Jun 12 '18 at 01:57

1 Answers1

0

I need to apologize.

First I don't show my HTML form. Second because I don't tell it is actualy a JSP page. And because I need to do some homework.

I must create my form using form:form (Spring form) with modelAttribute="<THE_MODEL>" and use path="<ATTRIBUTE>" to map HTML form fields to my model at Controller level.

<form:form enctype="multipart/form-data" modelAttribute="userToEdit" />
    <form:input name="username" path="username" /> 
</form:form>

Controller:

@RequestMapping(value = "/user/{method}", method = RequestMethod.POST)
    public String home(@RequestParam("profileImageFile") MultipartFile file, @PathVariable("method") String method, @ModelAttribute("userEdt") User user) {
    // Save image file
    // Save user ...
}

I'm not sure about the modelAttribute part. I think this is only to map the model comming from a controller to the page and is not related to sending the form to the receiver controller. Anyway... it is working now. I can take the file AND the model (entity) in just one post action.

Magno C
  • 1,922
  • 4
  • 28
  • 53