英文:
org.springframework.http.converter.HttpMessageNotReadableException: Found strange character
问题
我发送POST请求时收到以下内容
> 已解决 [org.springframework.http.converter.HttpMessageNotReadableException: 读取输入消息时发生I/O错误; 嵌套异常是java.io.IOException: 发现奇怪的数据: ?Color]
我的请求体
{
"name": "\bColor",
"displayName": "\bColor",
"position": 6
}
控制器
@PostMapping("/name")
public BaseResponseV2 saveNameField(@RequestBody NameField nameField) {
nameService.saveFilterOption(nameField);
return BaseResponseV2.makeResponse("DONE");
}
NameField:
private String name;
private String displayName;
private int position;
那么我如何将特殊/奇怪字符传递给RequestBody?
英文:
I received the following when sending POST request
> Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is java.io.IOException: Found strange data: ?Color]
My request body
{
"name": "\bColor",
"displayName": "\bColor",
"position": 6
}
Controller
@PostMapping("/name")
public BaseResponseV2 saveNameField(@RequestBody NameField nameField) {
nameService.saveFilterOption(nameField);
return BaseResponseV2.makeResponse("DONE");
}
NameField:
private String name;
private String displayName;
private int position;
So How do I pass the special/strange character to RequestBody
答案1
得分: 3
更改ObjectMapper
的配置并添加
mapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
您可以添加类似以下的内容
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> builder.json().featuresToEnable(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
}
到一个使用@Configuration
注解的类中重新配置该映射器
英文:
Change the configuration of the ObjectMapper
and add
mapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
You can add something like
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> builder.json().featuresToEnable(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)
}
to a @Configuration
annotated class to reconfigure the mapper
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论