RequestBody JSON格式具有单引号 [英] RequestBody JSON format has single quotes
问题描述
我的SpringBoot应用程序中有一个控制器类.
I have a controller class in my SpringBoot app.
@RestController
@RequestMapping("/{database}/alerts")
public class ControllerB {
@Autowired private ServiceB serviceB;
@Autowired
private B b;
@Autowired
ControllerB(B b,ServiceB serviceB){
this.b = b;
this.serviceB = serviceB;
}
@RequestMapping(method = RequestMethod.POST)
public B dosomethingCrazy(@RequestBody BImpl bimpl) {
String response = serviceB.dosomethingImportant();
return bimpl;
}
}
当requestBody的值中包含单引号时,就会出现问题.例如,如果请求正文为{"name":"peter o'toole"},则会抛出http 400
The problem arises when the requestBody has a single quote in the value. For example if the requestbody is {"name" : "peter o'toole"}, it throws a http 400
状态":400,错误":错误的请求",异常":"org.springframework.http.converter.HttpMessageNotReadableException",消息":"JSON解析错误:意外的输入结束VALUE_STRING
"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"JSON parse error: Unexpected end-of-input in VALUE_STRING
您知道杰克逊如何将单引号值映射到对象字段吗?
Any idea how Jackson can map a single quote value to the object field?
推荐答案
创建此类以设置Jackson并启用单引号:
Create this class to setup the Jackson and enable Single quotes:
@Configuration
public class JsonConfigurer {
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.build();
objectMapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES)
return objectMapper;
}
}
这篇关于RequestBody JSON格式具有单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!