接收 JSON 参数包含 ${} 在 Spring Boot 中引发错误。

huangapple go评论54阅读模式
英文:

Receiving JSON parameter contains ${} cause error in Spring Boot

问题

I have translated the code-related parts from your provided text. Here they are:

requested JSON

{
  "name": "$123$",
  "str1": "${str1}",
  "str2": "${str222",
  "str3": "${str3}333",
  "str4": "${str4}44444"
}

Controller

@RestController
public class TestController {
    @PostMapping("std")
    public Std test$2(@RequestBody Std std) {
        return std;
    }
}

Std.java

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Std {
    private String name;
    private String str1;
    private String str2;
    private String str3;
    private String str4;
    private String aa;
}

Result

{
  "name": "$123$",
  "str1": "str1",
  "str2": "${str222",
  "str3": "str3333",
  "str4": "str444444"
}

When I manually parse JSON through code, the result is correct. The output JSON contains ${}.
like this

    public void test() {
        try {
            String str = "{
                \"name\":\"$123$\",
                \"str1\":\"${str1}\",
                \"str2\":\"${str2}22\",
                \"str3\":\"${str3}333\",
                \"str4\":\"${str4}44444\"
            }";
            Std std = JSON.parseObject(str, Std.class);
            System.out.println(JSON.toJSONString(std));
            Std std1 = objectMapper.readValue(str, Std.class);
            System.out.println(objectMapper.writeValueAsString(std1));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

Please note that I've only translated the code parts as requested, and I haven't included any additional content.

英文:

When the JSON I received the request contains ${}, the ${} is removed from the received object.

requested JSON

{
  "name":"$123$",
  "str1":"${str1}",
  "str2":"${str222",
  "str3":"${str3}333",
  "str4":"${str4}44444"
}

Controller

@RestController
public class TestController {
    @PostMapping("std")
    public Std test$2(@RequestBody Std std) {
        return std;
    }
}

Std.java

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Std {
    private String name;
    private String str1;
    private String str2;
    private String str3;
    private String str4;
    private String aa;
}

Result

{
"name": "$123$",
"str1": "str1",
"str2": "${str222",
"str3": "str3333",
"str4": "str444444"
}

When I manually parse JSON through code, the result is correct. The output JSON contains ${}.
like this

    public void test() {
        try {
            String str = "{\n" +
                    "  \"name\":\"$123$\",\n" +
                    "  \"str1\":\"${str1}\",\n" +
                    "  \"str2\":\"${str2}22\",\n" +
                    "  \"str3\":\"${str3}333\",\n" +
                    "  \"str4\":\"${str4}44444\"\n" +
                    "}";
            Std std = JSON.parseObject(str, Std.class);
            System.out.println(JSON.toJSONString(std));
            Std std1 = objectMapper.readValue(str, Std.class);
            System.out.println(objectMapper.writeValueAsString(std1));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

Any help will be appreciated!

答案1

得分: 1

${variableName} 表示在Spring Boot中用于导入配置变量或环境变量的值。这是上面显示的问题的根本原因。

问题可以通过两种方式解决 -

  1. 用不同的分隔符字符替换${}

    例如:使用 #data# 代替 ${data}

    这种方法易于实施,因为只需进行小的代码更改即可。

  2. 为输入JSON中的闭合括号添加显式转义序列,即 } 字符。

    这种方法难以实施,因为它可能需要比其他方法更多的代码更改。

英文:

The ${variableName} notation is used in Spring Boot for the purpose of importing the values of configuration variables or environment variables. This is the root cause for the issue shown above.

The problem can be fixed in 2 ways -

  1. Replace ${ and } with a different de-limiter character

    Example: Use #data# instead of ${data}

    This approach is easy to implement as it can be done with small code change.

  2. Add an explicit escape sequence for the closing brace, I.E., for } character in the input JSON.

    This approach is difficult to implement, as it it may require more code changes than the other approach.

huangapple
  • 本文由 发表于 2020年8月5日 18:50:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63263580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定