私はSpringブートを使って簡単なRest APIを構築したいと考えていました。私はmultipartFileの春の例を調べました。私は同じアプローチに従うことにしました。私の残りのAPIを介してアップロードされるファイルには、特定の拡張子が付きます。だから、私はapplication/octet-streamとしてcontent-typeを与えました。私は同じのための私のユニットテストケースを実行しようとすると、コンテンツタイプがtext/plainまたはでno「を消費する」パラメータが存在する場合であれば、 は、私はいつもspring boot multipartFile(アプリケーション/オクテットストリームスロー例外あり)
nested exception is org.springframework.web.multipart.MultipartException: The current request is not a multipart request
の例外は、この例外は表示されません取得しますrequestMapping。
次のように私のコントローラコードが見えます:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestPart;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("/v1/sample")
public class SampleController {
private static Logger log = LoggerFactory.getLogger(SampleController.class);
@RequestMapping(path = "/{id}/upload",
consumes = {MediaType.APPLICATION_OCTET_STREAM_VALUE},
method = RequestMethod.POST)
public ResponseEntity<String> uploadfile(@PathVariable String id,
@RequestParam("file") MultipartFile upgradeFile) {
log.info("Obtained a upload request for the id {}",id);
return new ResponseEntity<String>("file upload has been accepted.",
HttpStatus.ACCEPTED);
}
}
を、次のように私のユニットテストスニペットは次のとおりです。
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.stellapps.devicemanager.fota.Application;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes = Application.class)
@EnableWebMvc
public class ControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void mockMvcBuilder() {
this.mockMvc = webAppContextSetup(webApplicationContext).build();
}
@Test
public void test_ValidPayload() {
String uri = "/v1/sample/1234/upload";
Path path = Paths.get("src/test/resources/someFile");
try {
byte[] bytes = Files.readAllBytes(path);
MockMultipartFile multipartFile =
new MockMultipartFile("file", "someFile.diff", "application/octet-stream", bytes);
mockMvc.perform(fileUpload(uri).file(multipartFile).contentType(MediaType.APPLICATION_OCTET_STREAM_VALUE))
.andExpect(status().isAccepted());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
私は、コンテンツタイプとしてプレーンテキストを/使用して、私は正常を与えた場合テキストファイル、それはうまくいく。私は、アプリケーションなどのコンテンツタイプを追加する場合は/それは
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.web.multipart.MultipartException: The current request is not a multipart request
at org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.assertIsMultipartRequest(RequestPartMethodArgumentResolver.java:204)
at org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:129)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:99)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
がどのように私は私の要求は、アプリケーション/オクテットストリームを受け入れ、私はそれを確実にするために、テストケースにどのような変更を加えなければならない作るか、次の例外がスローされますオクテットは、ストリーム成功する。
UPDATE:
が消費ヘッダを削除し、MockPartFileのコンテンツタイプを指定しないことによって、ファイルをアップロードする方法の一つです。私は、デフォルトでは、コントローラは、アプリケーション/オクテットストリームとして
UPDATEそれを取るとします
は、応答をありがとうございました。私は以前のバージョンの春(1.3.1)を使用していましたが、答えを出した後、私は春バージョンとテストケースを更新し、それに合わせて作業を開始しました。
[mcve]を入力してください。あなたの 'MockMvc'はどのように設定されましたか?マルチパート設定はどのように見えますか? –
また、なぜあなたは悪い要求を期待していますか?ここではチェックアウトしていないものがあります。要求された詳細で質問を編集してください。 –