org.springframework.http.converter.HttpMessageNotReadableException: 发现奇怪的字符

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

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

huangapple
  • 本文由 发表于 2023年2月27日 15:28:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577737.html
匿名

发表评论

匿名网友

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

确定