javaspringspring-bootpostman

Spring Boot: Why do i get Content-Type 'application/octet-stream' is not supported , when i'm sending multipart/form-data?


*) I'm newbie to spring boot.Currently was creating a Post service which would accept text , image & userId & store them in h2 DB. *) When i try to test my service with Postman i'm getting 415 Error code which says "error": "Unsupported Media Type". *) This is my controller file code for post call

@PostMapping("/posts")
    public ResponseEntity<Post> createPost(
            @RequestPart(value = "text") String text,
            @RequestPart(value = "image", required = false) MultipartFile image,
            @RequestPart(value ="userId") Long userId
    ) {
        System.out.println("text is : " + text);
        if(image != null) System.out.println("image is : " + image);
        System.out.println("userId is : " + userId);

        PostDTO postDTO = new PostDTO();
        postDTO.setText(text);
        postDTO.setImage(image);
        postDTO.setUserId(userId);

        Post createdPost = postService.createPost(postDTO);
        return new ResponseEntity<>(createdPost, HttpStatus.CREATED);
    }

*) This is my entity modal file

@Entity
@Table(name = "posts")
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String text;

    @Lob
    @Column(columnDefinition = "mediumblob")
    private byte[] image;

    @Column(name = "user_id")
    private Long userId;

    // Default constructor
    public Post() {
    }

    // Constructor with text and userId parameters
    public Post(String text, Long userId) {
        this.text = text;
        this.userId = userId;
    }

    // Constructor with text, image, and userId parameters
    public Post(String text, byte[] image, Long userId) {
        this.text = text;
        this.image = image;
        this.userId = userId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }
}

Below is my postman response

enter image description here

You can see here i'm providing request with Content-Type: multipart/form-data.

I don't even see getting console logs inside my controller .

I tried providing image as an optional form-data thinking might be issue with image type . but still facing the same issue. Had went through different questions on Stackoverflow but didn't found any solution. Expecting someone to help rectifying the issue.


Solution

  • The @RequestParam annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types. The main difference is that when the method argument is not a String, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while @RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. @RequestParam is likely to be used with name-value form fields while @RequestPart` is likely to be used with parts containing more complex content (e.g. JSON, XML)

    In simple terms, if you're dealing with basic data like names and numbers from forms, use @RequestParam. If you're dealing with more complex data like JSON or XML from a file upload or a more intricate form, use @RequestPart.

    Eg.

    @PostMapping("/posts")
        public ResponseEntity<Post> createPost(
                @RequestParam(value = "text") String text,
                @RequestPart(value = "image", required = false) MultipartFile image,
                @RequestParam(value = "userId") Long userId) {
            System.out.println("text is : " + text);
            if (image != null) System.out.println("image is : " + image);
            System.out.println("userId is : " + userId);
    
            PostDTO postDTO = new PostDTO();
            postDTO.setText(text);
            postDTO.setImage(image);
            postDTO.setUserId(userId);
    
            Post createdPost = postService.createPost(postDTO);
            return new ResponseEntity<>(createdPost, HttpStatus.CREATED);
        }