必需的请求部分不存在 - Spring

时间:2018-06-18 02:38:32

标签: javascript angularjs spring ng-file-upload

我正在尝试将附加的文件信息从agularjs控制器传递给spring控制器。我在将信息从angularjs传递给spring控制器时遇到问题,它将错误抛给浏览器控制台而不是调用spring控制器。

错误讯息:

error
:
"Required request part 'ad' is not present"
status
:
500

下面是我在我的应用程序中使用的示例代码,用于将附加的文件和信息从angularjs发送到弹簧控制器。

HTML:

<div ng-controller="sendEmailController">
     <button class="btn btn-primary" style="align-items: left" type="button"
             ng-click="cancelModal()">Close</button>
     </div><div class="col-sm-1">
                    <button class="btn btn-primary" type="button"
                            ng-click="saveForm()">Send</button>
                 </div>
            </div>
        </div>
    </div>

    <div class="modal-body">
        <div class="row" >
             <input type="email" style="alignment: right" autofocus
                    name="fromEmail" size="75" 
                    ng-model="fromEmail" required ng-trim="true"</inputtext>
        </div>
        <div class="row">
             <input type="email" style="alignment: right" name="ccEmail" size="75"
                    ng-model="ccEmail" ng-trim="true"></inputtext>
        </div>
        <div class="row">
             <input type="text" style="alignment: right" name="subject" size="75"
                    ng-model="subject"  ng-trim="true"></inputtext>
        </div>
        <div class="row">
             <input type="file" name="attachFile" size="60" />
        </div>
        <div class="row" >
            <label style="color: #0099ff;">Message: </label>
             <textarea rows="20" maxlength=35000
                       background-color = grey; name="message"
                       ng-model="message" >
             </textarea>
        </div>
    </div></div>

JS:

app.controller('sendEmailController', function ($rootScope, $scope, $uibModalInstance, MyService) {
    $scope.saveForm = function () {
        var formData = new FormData();
        var file = $scope.myFile;
        $scope.emailData = [
            {
                'from': $scope.fromEmail,
                'ccEmail': $scope.ccEmail,
                'subject': $scope.subject,
                'message': $scope.message,
            }];

        var json = $scope.emailData;
        console.log("JSOn String: " + JSON.stringify(json));
        formData.append("file", file);
        formData.append("ad", JSON.stringify(json));//important: convert to JSON!
        MyService.sendWithAttachments(formData).then(         // $scope.fromEmail,$scope.ccEmail,$scope.subject,$scope.fileName,$scope.fileObj,$scope.message).then(
            function (response) {
                 //response
            },
            function (errResponse) {

            }
        );
    }

});

MyService.js

 _repServiceFactory.sendWithAttachments = function (formData) {

        var myUrl = appURL + '/sendData/sendEmailsTest.form'; 
        $http({
            method: "post",
            url: myUrl,
            //headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            headers: {'Content-Type': undefined},
            data: formData
            /*transformRequest: function (data, headersGetterFunction) {
                return data;
            }*/

        }).success(function (result) {
            console.log(result);
        });
        return deferred.promise;
}

Springcontroller:

@Controller
@RequestMapping("/sendData")
public class DataController {
 @RequestMapping(value = "/sendEmailsTest", method = RequestMethod.POST,consumes = {"multipart/form-data"})
    public
    @ResponseBody
    String sendEmailsTest(@RequestPart("file") MultipartFile file) throws Exception {

        System.out.println("request body form data  " + file); 
//logic to get the files and the information and do accordingly..
}
}

弹簧servlet.xml中:

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
     </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000"/>
    </bean>

任何输入都会有所帮助。

1 个答案:

答案 0 :(得分:0)

您正在从服务器获取500响应代码。 您需要在spring控制器方法中再添加一个RequestPart,即将方法定义更新为以下

String sendEmailsTest(@RequestPart("file") MultipartFile file, @RequestPart("ad") String jsonAd) throws Exception {

}

您还可以将ad绑定到Class而不是String。我做了类似here的事情。