邮递员:所需的请求部分“文件"不存在 [英] Postman: Required request part 'file' is not present

查看:98
本文介绍了邮递员:所需的请求部分“文件"不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过邮递员将图像上传到我的Rest API.我正在使用Spring Boot框架.这是屏幕截图:

我还没有设置任何标头,因为我在其他堆栈溢出答案中发现它给出了多部分边界错误.

现在,下面是我的控制器代码:

package com.practice.rest.assignment1.controller;

import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.practice.rest.assignment1.model.Product;
import com.practice.rest.assignment1.service.CatalogueService;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

@RestController
@RequestMapping("/CatalogueController/")
public class CatalogueController{

    @Autowired
    private CatalogueService catalogueService;

    @RequestMapping(value = "addProduct", method = RequestMethod.POST , consumes = "multipart/form-data")
    public Product addProduct(@RequestParam String productJson, @RequestParam MultipartFile file) throws JsonParseException, JsonMappingException, IOException {


        Product product = new ObjectMapper().readValue(productJson, Product.class);
        byte[] mediaBytes = file.getBytes();
        product.setImage(mediaBytes);
        return catalogueService.saveInDb(product);

    }

}

现在,我正在使用一个Product对象,该对象内部包含一个定义为byte []数组的图像.我将此作为字符串和图像分别作为Multipart文件使用.

下面是我定义的产品类别属性:

    private Long pId;
    private String model;
    private String brand;
    private byte[] image; // This is where I want the image to save
    private Long price;
    private String currency;
    private String transmissionType;
    private String fuelType;

因为我使用的是Spring Boot,所以这是我的Main class:

package com.practice.rest.assignment1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class App {

  public static void main(String[] args) {
      SpringApplication.run(App.class, args);  
  }

}

我收到的邮递员错误是:

{
  "timestamp": 1478611635977,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.multipart.support.MissingServletRequestPartException",
  "message": "Required request part 'file' is not present",
  "path": "/CatalogueController/addProduct"
}

我在哪里错了?

解决方案

我认为问题出在要发送的JSON参数上.在邮递员中,您无需放置"开头和结尾以字符串形式表示参数. 而且,如果您使用开始和结束",那么在JSON(对于JSON对象而言,意味着属性键和值)之内,您应该使用(单引号).

I wanted to upload an image to my Rest API through postman. I am using spring boot framework. Here is the screen shot:

I also have not set any any headers as I found in other stack overflow answers that it gives multipart boundary error.

Now, below is my controller code:

package com.practice.rest.assignment1.controller;

import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.practice.rest.assignment1.model.Product;
import com.practice.rest.assignment1.service.CatalogueService;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

@RestController
@RequestMapping("/CatalogueController/")
public class CatalogueController{

    @Autowired
    private CatalogueService catalogueService;

    @RequestMapping(value = "addProduct", method = RequestMethod.POST , consumes = "multipart/form-data")
    public Product addProduct(@RequestParam String productJson, @RequestParam MultipartFile file) throws JsonParseException, JsonMappingException, IOException {


        Product product = new ObjectMapper().readValue(productJson, Product.class);
        byte[] mediaBytes = file.getBytes();
        product.setImage(mediaBytes);
        return catalogueService.saveInDb(product);

    }

}

Now, I am taking a Product object which consists internally an image defined as byte[] array. I take this as string and image separately as Multipart file.

Below is my product class attributes defined :

    private Long pId;
    private String model;
    private String brand;
    private byte[] image; // This is where I want the image to save
    private Long price;
    private String currency;
    private String transmissionType;
    private String fuelType;

Since , I am using spring boot , here is my Main class :

package com.practice.rest.assignment1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class App {

  public static void main(String[] args) {
      SpringApplication.run(App.class, args);  
  }

}

The error on postman I get is :

{
  "timestamp": 1478611635977,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.multipart.support.MissingServletRequestPartException",
  "message": "Required request part 'file' is not present",
  "path": "/CatalogueController/addProduct"
}

Where am I wrong ?

解决方案

I think the problem lies in the JSON parameter you are sending. In postman you don't need to put the starting and trailing " to represent a parameter as string. And also if you use starting and ending " then inside the JSON( mean for JSON object the properties key and value) you should use '(single quote).

这篇关于邮递员:所需的请求部分“文件"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆