@RequestPart with mixed multipart request,Spring MVC 3.2

niwlg2el  于 2023-06-21  发布在  Spring
关注(0)|答案(5)|浏览(109)

我正在开发一个基于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();
        fileNames.add(fileName);

        try {
            file.transferTo(new File("C:/" + file.getOriginalFilename()));
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    responseMap.put("displayText", file.getOriginalFilename());
    responseMap.put("fileSize", "" + file.getSize());
    response.add(responseMap);

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("Accept", "application/json");
    return new ResponseEntity<List<Map<String, String>>>(response,
            httpHeaders, HttpStatus.OK);

}

User.java 会是这样的-

@XmlRootElement(name = "User")

public class User implements Serializable { 
    private static final long serialVersionUID = 1L;

    private int userId;
    private String name;
    private String email;

    private String company;
    private String gender;

    //getter setter of the data members
}

根据我的理解,使用@RequestPart注解,我希望XML multipart部分根据其Content-Type进行评估,并最终解组到我的User类中(我使用的是Jaxb 2,在应用程序上下文中正确配置了marshaller/unmarhaller,并且当我将XML数据作为body传递并使用@RequestBody注解时,该过程对于所有其他控制器方法都可以正常工作)。
但实际发生的情况是,尽管文件被正确地找到并解析为MultipartFile,但“用户”部分从未被看到,并且请求总是失败,与控制器方法签名不匹配。
我复制了几个客户端类型的问题,我相信多部分请求的格式是确定的。
请帮助我解决这个问题,也许一些变通办法将有接收混合/多部分请求.
感谢和问候,
拉格文德拉

5tmbdcev

5tmbdcev1#

我已经设法解决了这个问题

端点示例:

@PostMapping("/")
public Document create(@RequestPart Document document,
                       @RequestPart(required = false) MultipartFile file) {
    log.debug("#create: document({}), file({})", delegation, file);
    //custom logic
    return document;
}

异常:

"error_message": "Content type 'application/octet-stream' not supported"

从下一个方法引发异常:

org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(HttpInputMessage,MethodParameter,Type)

解决方案:

我们必须创建自定义转换器@Component,它实现 HttpMessageConverterHttpMessageConverter,并知道 MediaType.APPLICATION_OCTET_STREAM。对于简单的解决方法,扩展 * AbstractJackson 2 HttpMessageConverter * 就足够了

@Component
public class MultipartJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter {

/**
 * Converter for support http request with header Content-Type: multipart/form-data
 */
public MultipartJackson2HttpMessageConverter(ObjectMapper objectMapper) {
    super(objectMapper, MediaType.APPLICATION_OCTET_STREAM);
}

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
    return false;
}

@Override
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {
    return false;
}

@Override
protected boolean canWrite(MediaType mediaType) {
    return false;
}
}
wsewodh2

wsewodh22#

不知道你是否已经解决了你的问题,但我也有一个类似的问题,当我的JSON对象混合在一起时,我的控制器没有拾取@RequestPart和MultipartFile。
调用的方法签名看起来正确:

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

// ... CODE ... 
}

但是,请确保您的请求看起来像这样:

POST /createUser
Content-Type: multipart/mixed; boundary=B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E

--B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E
Content-Disposition: form-data; name="user";
Content-Type: application/xml; charset=UTF-8

<user><!-- your user xml --></user>
--B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E
Content-Disposition: form-data; name="file"; filename="A551A700-46D4-470A-86E7-52AD2B445847.dat"
Content-Type: application/octet-stream

/// FILE DATA
--B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E--
tzxcd3kk

tzxcd3kk3#

您可以使用org.springframework.web.bind.annotation.RequestPart中的@RequestPart;它被用作组合@RequestBody和文件上传。
使用@RequestParam像这样的@RequestParam(“file”)MultipartFile文件你可以只上传文件和多个单个数据(键值)像

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST,  consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
    public void saveFile(
                         @RequestParam("userid") String userid,
                         @RequestParam("file") MultipartFile file) {

    }

您可以使用@RequestPart来发布JSON对象数据和文件,如

@RequestMapping(value = "/patientp", method = RequestMethod.POST,  consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<?> insertPatientInfo(
                                            @RequestPart PatientInfoDTO patientInfoDTO,
                                            @RequestPart("file") MultipartFile file) {
}

您不限于直接使用多部分文件上载作为控制器方法参数。您的表单对象可以包含Part或MultipartFile字段,Spring自动知道它必须从文件部分获取值并适当地转换值。
上面的方法可以响应前面演示的包含单个文件的多部分请求。这是因为Spring有一个内置的HTTP消息转换器,可以识别文件部分。除了javax.servlet.http.Part类型之外,您还可以将文件上传转换为org. springframework. web. multipart. MultipartFile。如果file字段允许多个文件上传,如第二个multipart请求所示,只需使用Part或MultipartFiles的数组或Collection。

@RequestMapping(value = "/patientp", method = RequestMethod.POST,  consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<?> insertPatientInfo(
                                                @RequestPart PatientInfoDTO patientInfoDTO,
                                                @RequestPart("files") List<MultipartFile> files) {
    }

很高兴能帮到你

r1zk6ea1

r1zk6ea14#

我设法解决了一个问题:

@SuppressWarnings("rawtypes")
@RequestMapping(value = "/DataTransfer", method = RequestMethod.POST, produces = {
        MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes = {  MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE} )
@ApiOperation(value = "Sbm Data Transfer Service", response = Iterable.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successfully find."),
        @ApiResponse(code = 400, message = "There has been an error."),
        @ApiResponse(code = 401, message = "You are not authorized to save the resource"),
        @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
        @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
ResponseEntity processDataTransfer(@RequestPart(name="file") MultipartFile  file, @RequestPart(name="param") DataTransferInputDto param);
h4cxqtbf

h4cxqtbf5#

你试过了吗

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

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

如果这不起作用,你能给我们看mapping.xml吗?

相关问题