Spring boot - partial update best practise?

Viewed 6157

I am using Spring boot v2 with mongo database. I was wondering what is the best way to do partial updates on the data model. Say I have a model with x attributes, depending on the request I may only want to update 1, 2 , or x of them attributes. Should I be exposing an end point for each type of update operation, or is it possible to expose one end pint and do it in a generic way? Note I will need to be able to validate the contents of the request attributes (e.g tel no must be numbers only)

Thanks,

3 Answers

HTTP PATCH is a nice way to update a resource by specifying only the properties that have changed. The following blog explain it very well

You can actually expose just one endpoint. This is the situation I had a few months ago:

I wanted people to modify any (or even all)fields of a Projects document (who am I to force the users to manually supply all fields lol). So I have my Model, Project.java:

package com.foxxmg.jarvisbackend.models;
//imports

@Document(collection = "Projects")
public class Project {
    @Id
    public String id;
    public String projectTitle;
    public String projectOverview;
    public Date startDate;
    public Date endDate;
    public List<String> assignedTo;
    public String progress;

   //constructors

    //getters & setters
}

I have my repository:

ProjectRepository.java

 package com.foxxmg.jarvisbackend.repositories;
//imports

    @Repository
    public interface ProjectRepository extends MongoRepository<Project, String>, QuerydslPredicateExecutor<Project> {
//please note, we are going to use findById(string) method for updating
        Project findByid(String id);
//other abstract methods 


    }

Now to my Controller, ProjectController.java:

package com.foxxmg.jarvisbackend.controllers;
        //import

        @RestController
        @RequestMapping("/projects")
        @CrossOrigin("*")
        public class ProjectController {
            @Autowired
            private ProjectRepository projectRepository;

    @PutMapping("update/{id}")
        public ResponseEntity<Project> update(@PathVariable("id") String id, @RequestBody Project project) {
            Optional<Project> optionalProject = projectRepository.findById(id);
            if (optionalProject.isPresent()) {
                Project p = optionalProject.get();
                if (project.getProjectTitle() != null)
                    p.setProjectTitle(project.getProjectTitle());
                if (project.getProjectOverview() != null)
                    p.setProjectOverview(project.getProjectOverview());
                if (project.getStartDate() != null)
                    p.setStartDate(project.getStartDate());
                if (project.getEndDate() != null)
                    p.setEndDate(project.getEndDate());
                if (project.getAssignedTo() != null)
                    p.setAssignedTo(project.getAssignedTo());
                return new ResponseEntity<>(projectRepository.save(p), HttpStatus.OK);
            } else
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
     }

That will allow partial update in MongoDB with Spring Boot.

Related