我正在使用Spring Data MongoDB和Spring Data Rest创建一个REST API,该API允许对MongoDB数据库进行GET,POST,PUT和DELETE操作,除了更新操作(PUT)之外,其他所有功能都工作正常。仅当我在请求正文中发送完整对象时,此方法才有效。
例如,我有以下实体:
@Document
public class User {
@Id
private String id;
private String email;
private String lastName;
private String firstName;
private String password;
...
}
要更新lastName字段,我必须发送所有用户对象,包括密码!这显然是非常错误。如果我仅发送要更新的字段,则所有其他字段在我的数据库中均设置为null。我什至尝试在这些字段上添加@NotNull约束,现在除非我发送所有用户对象的字段,否则更新甚至不会发生。
我尝试在此处搜索解决方案,但我仅找到以下帖子,但没有解决方案:How to update particular field in mongo db by using MongoRepository Interface?
有没有一种方法可以实现?
Spring Data Rest使用Spring Data存储库通过Rest调用自动检索和处理持久数据(签出https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reference)。
使用Spring Data MongoDB时,您具有MongoOperations
接口,该接口用作Rest端点的存储库。但是MongoOperations当前不支持特定字段更新!
PS:如果他们在Spring Data JPA中添加@DynamicUpdate之类的功能,那就太棒了。] >> 但这并不意味着可以完成,这是我遇到此问题时所采取的解决方法。
首先让我解释一下我们要做什么:
N.B。我们不想为应用程序中的每个模型重新执行这些步骤,因此我们将检索要动态更新的模型。为此,我们将创建一个实用程序类。 [这是可选的]
让我们开始将org.reflections api添加到我们的项目依赖项中,这使我们能够获取所有具有特定批注的类(在本例中为@Document
:]]
<dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version>0.9.12</version> </dependency>
然后创建一个名为UpdateUtility
的新类,并添加以下方法,并将MODEL_PACKAGE
属性替换为您自己的包含实体的包:public class UpdateUtility { private static final String MODEL_PACKAGE = "com.mycompany.myproject.models"; private static boolean initialized = false; private static HashMap<String, Class> classContext = new HashMap<>(); private static void init() { if(!initialized) { Reflections reflections = new Reflections(MODEL_PACKAGE); Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Document.class); // Get all the classes annotated with @Document in the specified package for(Class<?> model : classes) { classContext.put(model.getSimpleName().toLowerCase(), model); } initialized = true; } } public static Class getClassFromType(String type) throws Exception{ init(); if(classContext.containsKey(type)) { return classContext.get(type); } else { throw new Exception("Type " + type + " does not exists !"); } } }
使用该实用程序类,我们可以检索模型类以从其类型进行更新。例如:
UpdateUtility.getClassFromType()
将返回User.class
现在让我们创建控制器:
public class UpdateController { @Autowired private MongoTemplate mongoTemplate; @PutMapping("/{type}/{id}") public Object update(@RequestBody HashMap<String, Object> fields, @PathVariable(name = "type") String type, @PathVariable(name = "id") String id) { try { Class classType = UpdatorUtility.getClassFromType(type); // Get the domain class from the type in the request Query query = new Query(Criteria.where("id").is(id)); // Update the document with the given ID Update update = new Update(); // Iterate over the send fields and add them to the update object Iterator iterator = fields.entrySet().iterator(); while(iterator.hasNext()) { HashMap.Entry entry = (HashMap.Entry) iterator.next(); String key = (String) entry.getKey(); Object value = entry.getValue(); update.set(key, value); } mongoTemplate.updateFirst(query, update, classType); // Do the update return mongoTemplate.findById(id, classType); // Return the updated document } catch (Exception e) { // Handle your exception } } }
现在,我们可以更新指定的字段,而无需更改呼叫。因此,在您的情况下,呼叫将是:
PUT http://MY-DOMAIN/user/MY-USER-ID { lastName: "My new last name" }
PS:您可以通过添加更新嵌套对象中特定字段的可能性来改进它...