1
@Entity
public class Application{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer applicationId;
@NotBlank
private String name;
@NotBlank
private String emailId;
@Lob
private byte[] resume;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "jobTitle", nullable = false)
@JsonUnwrapped
@JsonIgnoreProperties({"createdAt","noOfPost"})
private Offer offer;
@Enumerated(EnumType.STRING)
@NotNull
private ApplicationStatus applicationStatus;

Above is my Application class. I am testing this service via postman.. I want to send resume as multipart and rest details as json from postman. But I am getting unsupported media type error.

@RequestMapping(value = "/api/file/upload", method = RequestMethod.POST,
        consumes = {"multipart/mixed","multipart/form-data","application/json"})
public Application uploadMultipartFile(@RequestParam("uploadfile") MultipartFile file,@RequestBody Application application)

how can I persist my application through JPA ?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Ankit
  • 81
  • 2
  • 10
  • It's not usual to deal with `multipart` content types and `application/json` in the same controller (and use the `MediaType` constants to name them), since you'll want the request body in some cases but not others. You can use the same URL with multiple controller methods, distinguishing by `consumes`. – chrylis -cautiouslyoptimistic- Oct 15 '18 at 06:20
  • Refer this link: https://stackoverflow.com/questions/48691649/how-to-upload-form-with-image-file-in-it-angularjs-spring/48692040#48692040 – Neeraj Benjwal Oct 15 '18 at 06:36
  • Suppose in a single request i want to persist all user details, like name ,emailId from RequestBody and for resume i want to upload a multipart file.in that case how to approach @chrylis – Ankit Oct 15 '18 at 07:10
  • File upload is only supported for `multipart/form-data` not for other types. Also if one of the parts is `application/json` instead of a file you should be using `@RequestPart` with a nam e(although I haven't tested that it marshals son correctly). What you generally do is create a form object which binds all parameters, i.e. a "regular" form submit. Doing async form posting doesn't imply you have to use JSON. – M. Deinum Oct 15 '18 at 07:13
  • If i use `@RequestPart` again that would be multipart/mixed content-type. My requirement is to get multipart file and other field in application/json in a single request from postman. @M. Deinum – Ankit Oct 15 '18 at 07:29
  • Why do you need `application/json`? I don't see that... You want to fill out a form why do you want to maintain the json part? – M. Deinum Oct 15 '18 at 08:26
  • we just dealing with this service which aim is to update a user details via postman client. we want to send details such as name ,emailId in json format and a multipart file for resume in a single request. want to update all userDetails in a single request. @M. Deinum – Ankit Oct 15 '18 at 09:08
  • And why on earth would you need json for that... That has been done for about 18 years already even without JSON. Just send a multipart form request, bind the parameter to a model object (`Application`) and retrieve the parts. That is all supported out-of-the-box. – M. Deinum Oct 15 '18 at 09:47

0 Answers0