相关疑难解决方法(0)

Spring启动控制器 - 将Multipart和JSON上传到DTO

我想将表单中的文件上传到Spring Boot API端点.

UI是用React编写的:

export function createExpense(formData) {
  return dispatch => {
    axios.post(ENDPOINT,
      formData, 
      headers: {
        'Authorization': //...,
        'Content-Type': 'application/json'
      }
      ).then(({data}) => {
        //...
      })
      .catch(({response}) => {
        //...
      });
    };
}

  _onSubmit = values => {
    let formData = new FormData();
    formData.append('title', values.title);
    formData.append('description', values.description);
    formData.append('amount', values.amount);
    formData.append('image', values.image[0]);
    this.props.createExpense(formData);
  }
Run Code Online (Sandbox Code Playgroud)

这是java端代码:

@RequestMapping(path = "/{groupId}", method = RequestMethod.POST)
public ExpenseSnippetGetDto create(@RequestBody ExpensePostDto expenseDto, @PathVariable long groupId, Principal principal, BindingResult result) throws IOException {
   //..
}
Run Code Online (Sandbox Code Playgroud)

但是我在java方面得到了这个例外:

org.springframework.web.HttpMediaTypeNotSupportedException: Content …
Run Code Online (Sandbox Code Playgroud)

javascript java spring-boot

23
推荐指数
3
解决办法
3万
查看次数

带有混合多部分请求的@RequestPart,Spring MVC 3.2

我正在开发基于Spring 3.2的RESTful服务.我正面临一个控制器处理混合多部分HTTP请求的问题,第二部分使用XML或JSON格式化数据,第二部分使用图像文件.

我正在使用@RequestPart注释来接收请求

@RequestMapping(value = "/User/Image", method = RequestMethod.POST,  consumes = {"multipart/mixed"},produces="applcation/json")

public
ResponseEntity<List<Map<String, String>>> createUser(
        @RequestPart("file") MultipartFile file, @RequestPart(required=false) User user) {

    System.out.println("file" + file);

    System.out.println("user " + user);

    System.out.println("received file with original filename: "
            + file.getOriginalFilename());

    // List<MultipartFile> files = uploadForm.getFiles();
    List<Map<String, String>> response = new ArrayList<Map<String, String>>();
    Map<String, String> responseMap = new HashMap<String, String>();

    List<String> fileNames = new ArrayList<String>();

    if (null != file) {
        // for (MultipartFile multipartFile : files) {

        String fileName = file.getOriginalFilename(); …
Run Code Online (Sandbox Code Playgroud)

rest spring multipartform-data

14
推荐指数
3
解决办法
4万
查看次数