英文:
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中用于导入配置变量或环境变量的值。这是上面显示的问题的根本原因。
问题可以通过两种方式解决 -
-
用不同的分隔符字符替换
${
和}
例如:使用
#data#
代替${data}
这种方法易于实施,因为只需进行小的代码更改即可。
-
为输入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 -
-
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.
-
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论