【问题标题】:how to send Retrofit List<MultipartBody.Part> and Object to Spring mvc如何将 Retrofit List<MultipartBody.Part> 和 Object 发送到 Spring mvc
【发布时间】:2017-07-21 01:28:39
【问题描述】:
MultipartBody.Part.createFormData(contentType, file.getName(), progressRequestBody);// mulitpart

//parameter
String json = gson.toJson(story);
RequestBody requestBody = RequestBody.create(MultipartBody.FORM, json);
parts.add(mulitpart);

//execute ...
Call<Integer> callGetStoryIdByUploadingStoryApi = storyRepository.saveStory(requestBody, parts);

@Multipart
@POST("./")
Call<Integer> saveStory(
        @Part("story") RequestBody story,
        @Part List<MultipartBody.Part> files);

我尝试将 Retrofit List 和 Object 发送到 spring MVC 得到下面的输出

<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.32 (Ubuntu) - Error 
    report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-
    family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;}
     H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;
    color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;
    color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;
    background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.
    line {height: 1px; background-color: #525D76; border: none;}</style> </head><body>
    
    <h1>HTTP Status 415 - </h1><div class="line"></div><p><b>type</b> Status 
    report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server 
    refused this request because the request entity is in a format not supported 
    by the requested resource for the requested method.</u></p><hr class="line">
    <h3>Apache Tomcat/8.0.32 (Ubuntu)</h3></body></html>

这是我在春天的代码

@RequestMapping(value = "/", method = RequestMethod.POST)
@ResponseBody
public long insertStory(@RequestBody Story story, @RequestPart  List<MultipartFile> files )
        throws Exception {
    return storyService.insertStory(story, files);
}

我该怎么办? OTL 我想得到一些提示..请^^

【问题讨论】:

    标签: android spring retrofit multipartform-data multipart


    【解决方案1】:

    你的 Spring 代码是错误的。

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public long insertStory(@RequestPart("Story") Story story, @RequestPart  List<MultipartFile> files )
            throws Exception {
        return storyService.insertStory(story, files);
    }
    

    这样改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-16
      • 2016-09-21
      • 1970-01-01
      • 2022-07-26
      • 2017-07-20
      • 2021-02-03
      • 2020-03-29
      • 2011-11-17
      相关资源
      最近更新 更多