私は郵便配達員を通じて画像を自分のレストAPIにアップロードしたかったのです。私は春のブートフレームワークを使用しています。私はそれがマルチパートの境界エラーを出したので、他のスタックオーバーフローの答えに見られるよう郵便番号:必須のリクエストパート 'ファイル'がありません
が私も任意の任意のヘッダを設定していない:ここではスクリーンショットです。
は今、以下の私のコントローラのコードです:
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オブジェクトを取っています。私は文字列とこれを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;
私はここで、春のブートを使用しています、ので、私のメインクラスです。
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"
}
どこが間違っていますか?
開始と終了の二重引用符を削除するだけで済みました。ファイルリクエスト部にエラーがスローされてしまったのはとても奇妙です。ありがとう!! –