This tutorial shows how to handle XML body data of HTTP PUT request in Spring MVC
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.web.bind.annotation.*;
@Controller
@RequestMapping("/articles")
public class ArticleController {
@Autowired
private ArticleService articleService;
@GetMapping
public String getArticleForm() {
return "article-form";
}
@PutMapping(value = "/{id}")
@ResponseBody
public String createNewArticle(@RequestBody Article article) {
articleService.saveArticle(article);
return "Article created.";
}
@GetMapping(value = "/{id}")
public String getArticle(@PathVariable("id") long id,
Model model) {
Article article = articleService.getArticleById(id);
model.addAttribute("article", article);
return "article-page";
}
}
package com.logicbig.example;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement
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>HTTP PUT request with XML Body Example</h3>
<form id="article-form">
<pre>
id: <input type="text" name="id">
content: <input type="text" name="content">
<input type="submit" value="Submit">
</pre>
</form>
<br/>
<div id="result"></div>
<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();
//preparing xml document
var xmlDocument = $.parseXML("<article/>");
var idNode = xmlDocument.createElement('id');
idNode.appendChild(document.createTextNode(id));
xmlDocument.documentElement.appendChild(idNode);
var contentNode = xmlDocument.createElement('content');
contentNode.appendChild(document.createTextNode(content));
xmlDocument.documentElement.appendChild(contentNode);
var xmlString = xmlToString(xmlDocument);
console.log(xmlString);
$.ajax({
type: 'PUT',
url: url,
contentType: 'application/xml',
data: xmlString,
success: function (data, status, xhr) {
$("#result").html(data +
" link: <a href='" + url + "'>" + url + "</a>");
},
error: function (xhr, status, error) {
alert(error);
}
});
});
function xmlToString(xmlDoc) {
return (typeof XMLSerializer !== "undefined") ?
(new window.XMLSerializer()).serializeToString(xmlDoc) :
xmlDoc.xml;
}
</script>
</body>
</html>
src/main/webapp/WEB-INF/views/article-page.jsp<html>
<body>
<h3>Article</h3>
${article}
</form>
</body>
</html>
Java Config
package com.logicbig.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@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:
Filling up and submitting the form:
In Chrome's developer tools window, we will see the logs:
Clicking on the hyper link of the article:
Using curl
$ curl -s -X PUT "http://localhost:8080/articles/1" -H "Content-Type: application/xml" -d "<article><id>1</id><content>test</content></article>" Article created.
$ curl -s "http://localhost:8080/articles/1" <html> <body> <h3>Article</h3> Article{id=1, content='test'} </form> </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)
- jackson-dataformat-xml 2.21.2 (Data format extension for Jackson to offer
alternative support for serializing POJOs as XML and deserializing XML as POJOs)
- JDK 25
- Maven 3.9.11
|