【问题标题】:RequestParam annotation doesn't work with POSTRequestParam 注释不适用于 POST
【发布时间】:2015-08-18 15:16:07
【问题描述】:

我有以下控制器:

@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
           });

效果很好

如何正确配置spring?

【问题讨论】:

    标签: java ajax spring


    【解决方案1】:

    Http POST 可以用于请求体上的请求参数。但是您正试图以不同的方式使用它。

    试试这个:

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

    如果您希望在请求正文中使用它,请使用@RequestBody

    【讨论】:

    • 是的,这行得通。但我想在正文中传递数据
    • 它正在工作,但在控制器中我看到 [object Object]
    猜你喜欢
    • 2018-05-07
    • 2021-11-06
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2021-11-15
    • 2014-08-31
    相关资源
    最近更新 更多