Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以在Spring Rest方法中混合介质类型吗?_Java_Spring_Rest_Spring Mvc - Fatal编程技术网

Java 我可以在Spring Rest方法中混合介质类型吗?

Java 我可以在Spring Rest方法中混合介质类型吗?,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,我目前正在尝试编写一个ReST方法来接受文件上传。当用户提交文件时,我还希望他们添加一个描述,以及与文件内容有关的一些其他元数据(例如,与文件内容关联的“类型”)。我正在使用Spring4使用SpringMVC控制器 这是我想做的一个例子: @RequestMapping(value = "/file", method = RequestMethod.POST) public @ResponseBody ResponseEntity<MyFileDTO> uploadCustomAn

我目前正在尝试编写一个ReST方法来接受文件上传。当用户提交文件时,我还希望他们添加一个描述,以及与文件内容有关的一些其他元数据(例如,与文件内容关联的“类型”)。我正在使用Spring4使用SpringMVC控制器

这是我想做的一个例子:

@RequestMapping(value = "/file", method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<MyFileDTO> uploadCustomAnnotationFile(
        @RequestParam("file") MultipartFile uploadFile,
        @RequestBody MyFileDetailsDTO fileDetails) {
    log.debug("This is working!");
}
我怀疑我不能在一个请求中混合应用程序/json和多部分/表单数据

更新: 根据zeroflagL的回复,并按照提供的指向我正在尝试执行的特定文档的链接,使用@RequestPart而不是@RequestBody,我取得了一些进展,但这仍然不起作用

新方法签名:

@RequestMapping(value = "/file", method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<MyFileDTO> uploadCustomAnnotationFile(
        @RequestPart MultipartFile uploadFile,
        @RequestPart MyFileDetailsDTO fileDetails) {
    log.debug("This is working!");
}
提前感谢,, Tonya

明确地解释了该做什么

混合使用application/json和multipart/form数据的想法是错误的,因为
application/json
将是multipart(原文如此!)请求的一部分

WebKitFormBoundary
让我想到您试图从浏览器发送AJAX请求,我想知道您是否真的发送了JSON。AFAIK不可能像Spring文档中所示那样显式添加(真实的)JSON部分。也许你可以给我们看一下客户端代码


无论如何,您不能使用
@RequestBody
,因为这将包括您要上载的文件。您必须使用
@RequestPart
。如果将表单数据(而不是JSON)与文件一起发送,则根本不需要任何注释。

您可以在Spring REST方法中混合媒体类型@zeroflagL/答案可能是对的。但是@RequestPart很好用。这是我的密码,和你的没什么不同

@RequestMapping(value = "/interface/member/v1/register.do", method = RequestMethod.POST)
@ResponseBody
public RegisterResponse register(@RequestPart(value = "registerRequest") RegisterRequest registerRequest,
@RequestPart(value = "attachment", required = false) MultipartFile file) throws ServiceException {
    return memberV1Service.register(registerRequest, file);
}
我建议你检查一下你的要求。不是你的服务器端代码。 这是我的测试预览代码(我使用Postman在Chrome中测试)

请求中缺少内容类型。此测试失败,因为缺少ServletRequestPartException导致所需的请求部分“registerRequest”不存在


您可以使用RESTClient或其他工具进行测试,并检查内容类型是否存在。

目前还没有客户端代码。正如我在最初的帖子中所说,我正在使用Swagger UI测试我的ReST端点。我已经使用Swing注释一段时间了,而
@RequestBody
是我如何配置ReST端点来接受/期望请求“body”中的JSON数据的。即使它与查询参数和/或路径参数结合使用,我以前在使用
@RequestBody
时也从未遇到过问题。但是,与多部分文件结合使用。。。这现在是一个问题。该方法不需要该参数的JSON数据,并引发无效媒体类型的异常。@TonyOhrel Swagger UI在浏览器中运行,因此如果最终客户端未在浏览器中运行,则会产生不同。在这种情况下,您也不需要任何注释。请注意,如果@Controller类在其
@RequestMapping
consumes
属性中声明了错误的端点格式,例如,如果您声明
consumes={“application/json”},则也可以抛出
HttpMediaTypeNotSupportedException
在类级别,但有一个端点需要使用“多部分/表单数据”。
2014-12-11 09:21:45.237 [http-nio-8443-exec-8] ERROR c.i.h.c.ControllerExceptionHandler   [ControllerExceptionHandler.groovy:58] - Controller Exception
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'fileDetails' is not present.
@RequestMapping(value = "/interface/member/v1/register.do", method = RequestMethod.POST)
@ResponseBody
public RegisterResponse register(@RequestPart(value = "registerRequest") RegisterRequest registerRequest,
@RequestPart(value = "attachment", required = false) MultipartFile file) throws ServiceException {
    return memberV1Service.register(registerRequest, file);
}
    POST /was/interface/member/v1/register.do HTTP/1.1
    Host: localhost:8080
    Cache-Control: no-cache

    ----WebKitFormBoundaryE19zNvXGzXaLvS5C
    Content-Disposition: form-data; name="registerRequest"

    { -- some of my json format content}
    ----WebKitFormBoundaryE19zNvXGzXaLvS5C
    Content-Disposition: form-data; name="attachment"; filename="IMG_0089.JPG"
    Content-Type: image/jpeg


    ----WebKitFormBoundaryE19zNvXGzXaLvS5C