Posting Json with Spring MVC

We’re improving the Post editing in NixMash Spring v0.4.2 with a “Save and Continue” function, represented by the SAVE button and resulting “Post updated!” Modal. We’re going to talk about how we’re doing that in Json and Spring MVC.

The JavaScript

We’ll get the JavaScript out of the way first. We’re only updating a few fields so those are the only ones we’re adding to our Json data object. Then we’ll post that to our Spring MVC endpoint of /admin/posts/archive with Ajax.

The MVC Method

Our Spring MVC method looks simple but does a lot. We have full validation of our PostDTO object using the @Valid annotation, return “ERROR” if any of the fields fail, like a title is missing or too long.

We could return result error details, but we’ll keep it simple by returning an ERROR string and a special Modal to get the user’s attention.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.