HttpMediaTypeNotSupportedException

1

I was sending an excell through Angular2 and I was receiving the SpringBoot, the problem is when in addition to the Excell I want to send a 2nd object. When I send the second object, I miss an error.

Angular Code where I declare the class and the object:

export class DataUser {
  addData: Date;
  addUser: string;
  dateModification: Date;
  userModification: string;
}
public newParam: DataUser = {
    addData: null,
    addUser: "",
    dateModification: null,
    userModification: ""
}

addExcell() {
    const url = "http://localhost:8080/gn/insert";
    this.newParam = {
      addData: new Date(),
      addUser: this.cookie.get('user'),
      dateModification: new Date(),
      userModification: this.cookie.get('user')
    }
    this.service.sendFile(url, this.formData, this.newParam).subscribe(data => {
        console.log(data);
      }
    );
  }

  sendFile(url, myFile, newParam ) {
    return this.http.post(url,   myFile ,  newParam);
  }

Afterwards to pick it up at the SpringBoot->

@RequestMapping(method = RequestMethod.POST, value = "/insert", headers = "Content-Type= multipart/form-data", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public MyMessage insert(@RequestParam("uploadFile") MultipartFile multipart , @RequestBody DataUser dataUser) {

Where the first parameter is the Excell and the second the object (user).

User class

public class DataUser {

    public Date addData;
    public String addUser;
    public Timestamp dateModification;
    public String usermodification;
    public DataUser(Date addData, String addUser, Timestamp dateModification, String usermodification) {
        super();
        this.addData = addData;
        this.addUser = addUser;
        this.dateModification = dateModification;
        this.usermodification = usermodification;
    }

With its getters and setters, to match the 2 classes.

This is the error that comes out in the SpringBoot

2018-07-17 11:58:30.880  WARN 4092 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;boundary=----WebKitFormBoundary2i0ZEb9BQ1SkKbEc;charset=UTF-8' not supported

Error in Angular2

http://localhost:8080/gn/insert 415 ()
HttpErrorResponse {headers: HttpHeaders, status: 415, statusText: "OK", url: "http://localhost:8080/gn/insert", ok: false, …}
error:
{timestamp: 1531825158243, status: 415, error: "Unsupported Media Type", exception: "org.springframework.web.HttpMediaTypeNotSupportedException", message: "Content type 'multipart/form-data;boundary=----Web…darypMcoVNA9Ov1DBx7J;charset=UTF-8' not supported", …}
headers:HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message:"Http failure response for http://localhost:8080/gn/insert: 415 OK"
name:"HttpErrorResponse"
ok:false
status:415
statusText:"OK"
url:"http://localhost:8080/gn/insert"

A friend says that it may be because he has missing letters or petitions, I do not know, I'm very new, but I've taken the error that says Content type 'multipart / form-data and I have put it into the SpringBoot so that it has multipatter / form and keeps giving the error, so I do not know what header is missing, assuming it is true that it is a matter of headers. Thanks.

    
asked by EduBw 17.07.2018 в 13:15
source

1 answer

0

The correct way to do it is as follows:

In Angular, add your form to the formData where you already have the file:

this.formData.append('dataUser',
    new Blob([JSON.stringify(this.newParam)],
    {
        type: "application/json"
    })
);

In Spring, you receive each part like this:

@RequestMapping(value = "/insert",method = RequestMethod.POST, consumes = "multipart/form-data")
@ResponseBody
public ResponseEntity<String> insert(@RequestPart("uploadFile") MultipartFile file, @RequestPart("dataUser") DataUser model){
    // código aquí
}

Notice that we are using RequestPart and not RequestParam or RequestBody

Similar question in SO: link

    
answered by 17.07.2018 в 13:30