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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why Is the Agent Card Important?
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Document Generation API: How to Automate Personalized Document Creation at Scale
  • Token-Efficient APIs for the Agentic Era

Trending

  • Every Generation Fears the Machine. Here Is How the AI Job Panic Usually Ends
  • Managing Technical Debt in AI-Generated Code
  • Designing a Polling Pipeline for Rate-Limited APIs Without Losing Data
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Mar. 26, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
22.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why Is the Agent Card Important?
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Document Generation API: How to Automate Personalized Document Creation at Scale
  • Token-Efficient APIs for the Agentic Era

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook