DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Unlock AI Power: Generate JSON With GPT-4 and Node.js for Ultimate App Integration
  • Custom Elements Manifest: The Key to Seamless Web Component Discovery and Documentation
  • Instant App Backends With API and Logic Automation
  • Migrating MuleSoft System API to AWS Lambda (Part 1)

Trending

  • How to Convert XLS to XLSX in Java
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  1. DZone
  2. Data Engineering
  3. Databases
  4. Updating and Deleting with a Spring Boot/Elide JSON API Server

Updating and Deleting with a Spring Boot/Elide JSON API Server

Learn how to update and delete using a Spring Boot/Elide JSON API server.

By 
Matthew Casperson user avatar
Matthew Casperson
·
Mar. 26, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
21.8K Views

Join the DZone community and get the full member experience.

Join For Free

Up until now we have been working with the Elide get() and post() methods, which allow us to read and create entities. The final two methods we need to support are patch() and delete(), which allow us to update and delete entities.

There is actually very little code to write to support these two new operations. We create two new methods in our REST API to respond to the PATCH and DELETE HTTP requests, and pipe those requests into Elide.

First, the delete method.

    @CrossOrigin(origins = "*")
    @RequestMapping(
            method = RequestMethod.DELETE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            value={"/{entity}/{id}", "/{entity}/{id}/relationships/{entity2}"})
    @Transactional
    public String jsonApiDelete(
            @RequestBody final String body,
            final HttpServletRequest request,
            final Principal principal) {

        return elideRunner(
                request,
                (elide, path) -> elide.delete(path, body, principal, SecurityMode.SECURITY_ACTIVE).getBody());
    }

Sending a DELETE request to http://localhost:8080/child/1 will delete that child. You can also delete relationships by sending the following JSON:

{
  "data": [
      { "type": "child", "id": "1" }
  ]
}

to http://localhost:8080/parent/2/relationships/children, but in our case the database has been configured in a way that requires all children to have a parent, so attempting to do this will result in a constraint violation, and the request will be denied. Still, seeing that violation exception proves that Elide was trying to set the relationship to null as expected, even if we don’t allow it.

Then we have the update method.

    @CrossOrigin(origins = "*")
    @RequestMapping(
            method = RequestMethod.PATCH,
            produces = MediaType.APPLICATION_JSON_VALUE,
            value={"/{entity}/{id}", "/{entity}/{id}/relationships/{entity2}"})
    @Transactional
    public String jsonApiPatch(
            @RequestBody final String body,
            final HttpServletRequest request,
            final Principal principal) {
        /*
            Note that the patch operation here is the standard update, not the JSON Patch extension
            (http://jsonapi.org/extensions/jsonpatch/)
         */
        return elideRunner(
                request,
                (elide, path) -> elide.patch("application/vnd.api+json", "application/vnd.api+json", path, body, principal, SecurityMode.SECURITY_ACTIVE).getBody());
    }

The JSON API made the decision to use the HTTP PATCH verb instead of PUT because PATCH better expresses an update that only modifies a subset of fields. You can read more on the reasons for this decision in the JSON API FAQ.

Sending a PATCH request to http://localhost:8080/child/4 with the following JSON:

{
  "data": {
    "type": "child",
    "id": 4,
    "attributes": {
      "description": "Updated description"
    }
  }
}

will update the supplied attributes. You can also update a to-one relationship by sending a PATCH request to http://localhost:8080/child/4/relationships/parent with the JSON

{
  "data": {
    "type": "parent",
    "id": 2
  }
}

Keep in mind though that all updates and deletes will need to be done by the admin user, who is the only user to have permissions to apply these changes after we configured the authorization in this article.

Another thing to note is that we have only supported the standard PATCH request defined by the JSON API spec. There is a JSON Patch Extension that makes use of the JSON API extension functionality that you may like to take a look at, but we haven’t enabled it here.

Download the source code for this article from GitHub.

JSON API

Published at DZone with permission of Matthew Casperson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Unlock AI Power: Generate JSON With GPT-4 and Node.js for Ultimate App Integration
  • Custom Elements Manifest: The Key to Seamless Web Component Discovery and Documentation
  • Instant App Backends With API and Logic Automation
  • Migrating MuleSoft System API to AWS Lambda (Part 1)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: