java - RequestBody JSON 格式有单引号

标签 java spring jackson

我的 SpringBoot 应用程序中有一个 Controller 类。

@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 在值中包含单引号时,就会出现问题。例如,如果 requestbody 是 {"name": "peter o'toole"},它会抛出一个 http 400

"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"JSON parse error: Unexpected end-of-input in VALUE_STRING



知道 jackson 如何将单引号值映射到对象字段吗?

最佳答案

在您的 application.properties 中设置以下属性文件:

spring.jackson.parser.allow-single-quotes=true

关于java - RequestBody JSON 格式有单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49159300/

相关文章:

java - 如何在调用任何 url 时提供 ntlm 身份验证?

spring - 不使用Maven构建镜像即可对我的Spring Boot应用程序进行Docker化

javax.transaction.Transactional 与 org.springframework.transaction.annotation.Transactional

java - Jackson JSON objectmapper 反序列化为 LinkedHashMap 而不是 HashMap

java - 保存对象列表引发 Hibernate 异常

Java 邮件。如何检查登录是否成功?

java - 从 Java 中的输入文件数据创建的数组中创建格式化表

spring - Spring 4.0 + Security 3.2 + j_spring_security_check 的 JavaConfiguration

java - 如何@autowire 一些 bean 进入 JsonSerializer?

java - 我可以自定义 Jackson 错误消息吗?