首页 \ 问答 \ Spring Boot预处理原始JSON数据(Spring Boot Preprocess JSON data)

Spring Boot预处理原始JSON数据(Spring Boot Preprocess JSON data)

我有一个RESTful Spring Boot API,它有一个注册端点。

在我的@RestController类中,我编写了一个简单的String值预处理器,用于修剪和替换只有null字符串的null值。

  @InitBinder
  public void blankStringBinder(WebDataBinder dataBinder) {
    dataBinder.setDisallowedFields("password", "confirmPassword");
    StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
    dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
  }

但是当我从Postman提交数据作为原始JSON ,修剪编辑没有发生。 我在blankStringBinder方法中设置了一个断点,我看到它在每个传入的请求上被调用。

WebDataBinder似乎适用于form-data 。 有没有办法使它适用于原始JSON数据?


I have a RESTful Spring Boot API, that has a registration end point.

Inside my @RestController class I have written a simple String value pre-processor, to trim and replace whitespace only strings with null values.

  @InitBinder
  public void blankStringBinder(WebDataBinder dataBinder) {
    dataBinder.setDisallowedFields("password", "confirmPassword");
    StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
    dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
  }

But when I submit the data from Postman as raw JSON, the trimming edit is not taking place. I put a break point inside blankStringBinder method and I see that it gets called on each incoming request.

The WebDataBinder seems to work for form-data. Is there a way to make it work for raw JSON data too?


原文:https://stackoverflow.com/questions/43001173
更新时间:2021-08-04 20:08

最满意答案

您需要指定php文件的绝对路径而不是相对路径。 您可以通过在您发出请求的URL的开头添加斜杠/来实现。 这是因为那些php文件位于域的根目录中。

所以你需要改变你的功能:

function replacecount(url, inittext, text, id) {
        var vars = new Object();
        vars['cat'] = id;


        $.ajax({
            type: "POST",
            url: "/getComptage.php",
            data: vars,
            success: function (msg) {
                replacetext(url, inittext, text, msg);
            }
        });
        return false;
    }

 function getdataroom(url, inittext, text, id) {
        var vars = new Object();
        vars['idc'] = id;

        $.ajax({
            type: "POST",
            url: "/getDataroomAccessCount.php",
            data: vars,
            success: function (msg) {
                replacetextDataroom(url, inittext, text, msg);
            }
        });
        return false;
    }

You need to specify absolute path to the php files instead of relative path. You can do that by adding a slash / at the start of the URL you're making the request to. This is because those php files are located in the root of your domain.

So you need to change your functions to be like this:

function replacecount(url, inittext, text, id) {
        var vars = new Object();
        vars['cat'] = id;


        $.ajax({
            type: "POST",
            url: "/getComptage.php",
            data: vars,
            success: function (msg) {
                replacetext(url, inittext, text, msg);
            }
        });
        return false;
    }

 function getdataroom(url, inittext, text, id) {
        var vars = new Object();
        vars['idc'] = id;

        $.ajax({
            type: "POST",
            url: "/getDataroomAccessCount.php",
            data: vars,
            success: function (msg) {
                replacetextDataroom(url, inittext, text, msg);
            }
        });
        return false;
    }

相关问答

更多
  • 鉴于你的限制,我只看到两个选择: 使用window.load事件: (function() { if (window.addEventListener) { // Standard window.addEventListener('load', jQueryCheck, false); } else if (window.attachEvent) { // Microsoft window.attachEvent('on ...
  • 我无法在IE中测试您的代码。 但是在你的javascript代码的最后一行代码中看起来像是一个拼写错误。 它应该是一个自调用函数,所以部分(jQuery)应该在结束括号之后。