RequestParam注释不适用于POST

时间:2015-08-18 15:16:08

标签: java ajax spring

我有以下控制器:

@RequestMapping(value = { "/member/uploadExternalImage",
            "/member/uploadExternalImage" }, method = RequestMethod.POST)
    public String handleFileUpload(@RequestParam("url") String url,
            Principal principal) throws IOException {
        File file = restTemplate.getForObject("url", File.class);
        file.toString();
        return null;
    }

我有以下ajax:

      $.ajax({
            url: 'uploadExternalImage',  //Server script to process data
            type: 'POST',
            success: function () {

            },
            complete:function(){
                $.fancybox.hideLoading();
            },

            // Form data
            data: {url: files[0].link},

            cache: false,
            contentType: false,
            processData: false
       });

在春季日志中我看到了

org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'url' is not present

如果我用GET更改ajax方法:

$.ajax({
                url: 'uploadExternalImage?url=123',  //Server script to process data
                type: 'POST',
                success: function () {

                },
                complete:function(){
                    $.fancybox.hideLoading();
                },
                error: function (data) {

                },

                cache: false,
                contentType: false,
                processData: false
           });

一切正常

如何正确配置弹簧?

1 个答案:

答案 0 :(得分:0)

Http POST能够用于请求主体上的请求参数。但是你试图以不同的方式使用它。

试试这个:

            $.ajax({
                type: 'POST',
                url: "uploadExternalImage?url=BLABLA",
                data: text,
                success: function (data) {
                    //
                }
            });

如果您需要请求正文,请使用@RequestBody