首页 > 解决方案 > 强制 org.springframework.http.ResponseEntity.ok 保留 Pascal-case json

问题描述

我有一个返回 ResponseEntity 的 Rest Service,它有一个 HashMap,它包含从 findAll() JPA 操作返回的 List<> 作为值。

@GetMapping("/users")
public ResponseEntity getUsers() {

  List<Users> users = userRepository.findAll();
  var model = new HashMap<>();
  model.put("data",users);

  return ok(model);
}

HashMap 的键是 Pascal-case 格式(如“UserMail”),但是当其余服务返回时,它会将键转换为驼峰格式(“userMail”)。有没有办法防止它保持原始格式?

标签: javaspring

解决方案


如果您想将用户下的所有属性强制为 Pascal-case 格式。您可以尝试将 PropertyNamingStrategy 应用于该类

@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
Class Users {
   ...
}

推荐阅读