This example shows how to handle an HTTP PATCH request in Spring MVC
PUT vs PATCH vs POST
PUT method creates/replaces the resource at the requested URI.
PATCH method modifies the existing resource (partially) at the requested URI.
POST method creates/modifies the resource without targeting a URI. After modification, how the user can make use of the same resource, that's entirely dependent on the web application logic.
Example
The controller
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/articles")
public class ArticleController {
@Autowired
private ArticleService articleService;
@PatchMapping("/{id}")
@ResponseBody
public String patchArticle(@RequestBody MultiValueMap<String, String> formParams) {
System.out.println(formParams);
long id = Long.parseLong(formParams.getFirst("id"));
String content = formParams.getFirst("content");
articleService.updateArticle(id, content);
return "Article updated.";
}
@GetMapping("/{id}")
public String getArticle(@PathVariable("id") long id,
Model model) {
Article article = articleService.getArticleById(id);
model.addAttribute("article", article);
return "article-form";
}
}
package com.logicbig.example;
public class Article {
private long id;
private String content;
.............
}
src/main/webapp/WEB-INF/views/article-form.jsp<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h3>Article Form</h3>
<form id="article-form">
<pre>
id: <input type="text" name="id" value="${article.id}" readonly>
content: <input type="text" name="content" value="${article.content}">
<input type="submit" value="Submit">
</pre>
</form>
<br/>
<script>
$("#article-form").submit(function(event){
event.preventDefault();
var form = $(this);
var id = form.find('input[name="id"]').val();
var url = 'http://localhost:8080/articles/'+id;
var content = form.find('input[name="content"]').val();
$.ajax({
type : 'PATCH',
url : url,
contentType: 'application/x-www-form-urlencoded',
data : "id=" + id + "&content=" + content,
success : function(data, status, xhr){
//refresh the current page
location.reload();
},
error: function(xhr, status, error){
alert(error);
}
});
});
</script>
</body>
</html>
Java Config
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
}
Running the example
To try examples, run embedded Jetty (configured in pom.xml of example project below):
mvn jetty:run
Accessing http://localhost:8080/articles/1 :
Editing the content form field and submitting the form will refresh the form with updated content:
Using curl
$ curl -s -X PATCH "http://localhost:8080/articles/1" -H "Content-Type: application/x-www-form-urlencoded" -d "id=1&content=new updated content" Article updated.
$ curl -s "http://localhost:8080/articles/1" <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body>
<h3>Article Form</h3> <form id="article-form"> <pre> id: <input type="text" name="id" value="1" readonly> content: <input type="text" name="content" value="new updated content"> <input type="submit" value="Submit"> </pre> </form> <br/> <script> $("#article-form").submit(function(event){ event.preventDefault(); var form = $(this); var id = form.find('input[name="id"]').val(); var url = 'http://localhost:8080/articles/'+id; var content = form.find('input[name="content"]').val(); $.ajax({ type : 'PATCH', url : url, contentType: 'application/x-www-form-urlencoded', data : "id=" + id + "&content=" + content, success : function(data, status, xhr){ //refresh the current page location.reload(); }, error: function(xhr, status, error){ alert(error); } }); });
</script> </body> </html>
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 3.2.9.RELEASE - 7.0.6 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- jakarta.servlet.jsp.jstl 3.0.1 (Jakarta Standard Tag Library Implementation)
- JDK 25
- Maven 3.9.11
|