Khi xây dựng REST API, JSON thường là định dạng mặc định. Nhưng trong trường hợp một client yêu cầu định dạng XML thì sao? Bài viết này sẽ hướng dẫn bạn cách cấu hình Spring REST API để trả về XML và JSON theo yêu cầu của client.
Hỗ trợ song song XML và JSON trong Spring REST API
Vì chúng ta sẽ chỉnh sửa dựa trên một project có sẵn, đầu tiên bạn cần tải nó về từ liên kết này.
Sau đây, ta sẽ thực hiện các thay đổi sau trong file cấu hình Spring bean.
Định nghĩa một bean có kiểu là Jaxb2RootElementHttpMessageConverter.
Thêm bean vừa được cấu hình ở trên vào thuộc tính messageConverters của RequestMappingHandlerAdapter.
Sau các thay đổi trên, file cấu hình Spring bean servlet-context.xml hoàn chỉnh của chúng ta sẽ có dạng như sau:
Chúng ta đã biết để hỗ trợ việc marshal (chuyển đổi một Java object thành XML) với JAXB cho một class, ta cần thêm annotation @XmlRootElement cho class đó. Vì vậy, hãy thêm annotation này vào model class trong Employee.java.
@XmlRootElement
public class Employee implements Serializable{
//no change in code
}
Ứng dụng Spring của chúng ta giờ đây sẽ hỗ trợ cả JSON lẫn XML. Nó thậm chí còn hỗ trợ request XML đi kèm response JSON và ngược lại.
Dưới đây là một vài hình minh họa cho kết quả thực tế. Lưu ý là bài này sử dụng ứng dụng Postman trong Chrome, nhưng bạn có thể dùng bất kỳ REST client nào để kiểm thử.
Response dạng XML: Cần đảm bảo truyền header Accept là “application/xml”.
Response dạng JSON: Cần đảm bảo truyền header Accept là “application/json”.
Request XML với Response JSON: Cần đảm bảo header Accept là “application/json” và header Content-Type là “text/xml” như trong các hình dưới đây.
Tổng kết
Như vậy, chỉ với vài thay đổi nhỏ trong cấu hình và một annotation, ứng dụng Spring REST của bạn đã có thể linh hoạt phục vụ cả định dạng XML lẫn JSON. Điều này một lần nữa cho thấy sức mạnh và sự tiện lợi của Spring Framework trong việc xây dựng các web API hiện đại để đáp ứng tốt hơn các yêu cầu đa dạng từ phía client.
Về tác giả
Chris PhamTechnical Writer
I have over 5 years of experience writing technical documentation for tech products, making them accessible and user-friendly. My focus is always on providing clear and precise information.
@#@
Tôi đã có hơn 5 năm kinh nghiệm viết tài liệu kỹ thuật cho các sản phẩm công nghệ, giúp người dùng dễ dàng tiếp cận và sử dụng. Tôi luôn tập trung vào việc cung cấp thông tin chính xác và dễ hiểu.
Cập nhật thông tin mới nhấtNhận các thông tin mới nhất về mối đe dọa, báo cáo an ninh mạng từ CyStack về hòm thư điện tử của bạn
Thảo luận (0)
Đăng nhập để thảo luận
Bài viết liên quan
{"success":true,"head":"<title>Spring REST API: Hướng dẫn hỗ trợ cả XML và JSON - CyStack Tutorial</title>\n<meta name=\"description\" content=\"Spring REST API giúp bạn xây dựng dịch vụ web nhanh chóng, hỗ trợ JSON và XML, dễ mở rộng, bảo mật và tương thích với nhiều ứng dụng.\"/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-video-preview:-1, max-image-preview:large\"/>\n<link rel=\"canonical\" href=\"https://blog.cystack.org/tutorial/2025/09/23/spring-rest-api/\" />\n<meta property=\"og:locale\" content=\"en_US\" />\n<meta property=\"og:type\" content=\"article\" />\n<meta property=\"og:title\" content=\"Spring REST API: Hướng dẫn hỗ trợ cả XML và JSON - CyStack Tutorial\" />\n<meta property=\"og:description\" content=\"Spring REST API giúp bạn xây dựng dịch vụ web nhanh chóng, hỗ trợ JSON và XML, dễ mở rộng, bảo mật và tương thích với nhiều ứng dụng.\" />\n<meta property=\"og:url\" content=\"https://blog.cystack.org/tutorial/2025/09/23/spring-rest-api/\" />\n<meta property=\"og:site_name\" content=\"CyStack Tutorial\" />\n<meta property=\"article:tag\" content=\"vi\" />\n<meta property=\"article:section\" content=\"Java\" />\n<meta property=\"og:image\" content=\"https://s2.cystack.net/tutorial/23105817/spring-rest-api.jpg\" />\n<meta property=\"og:image:secure_url\" content=\"https://s2.cystack.net/tutorial/23105817/spring-rest-api.jpg\" />\n<meta property=\"og:image:width\" content=\"1200\" />\n<meta property=\"og:image:height\" content=\"630\" />\n<meta property=\"og:image:alt\" content=\"Spring REST API\" />\n<meta property=\"og:image:type\" content=\"image/jpeg\" />\n<meta property=\"article:published_time\" content=\"2025-09-23T10:59:14+07:00\" />\n<meta name=\"twitter:card\" content=\"summary_large_image\" />\n<meta name=\"twitter:title\" content=\"Spring REST API: Hướng dẫn hỗ trợ cả XML và JSON - CyStack Tutorial\" />\n<meta name=\"twitter:description\" content=\"Spring REST API giúp bạn xây dựng dịch vụ web nhanh chóng, hỗ trợ JSON và XML, dễ mở rộng, bảo mật và tương thích với nhiều ứng dụng.\" />\n<meta name=\"twitter:image\" content=\"https://s2.cystack.net/tutorial/23105817/spring-rest-api.jpg\" />\n<meta name=\"twitter:label1\" content=\"Written by\" />\n<meta name=\"twitter:data1\" content=\"Chris Pham\" />\n<meta name=\"twitter:label2\" content=\"Time to read\" />\n<meta name=\"twitter:data2\" content=\"3 minutes\" />\n<script type=\"application/ld+json\" class=\"rank-math-schema\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https://blog.cystack.org/tutorial/#person\",\"name\":\"CyStack Tutorial\"},{\"@type\":\"WebSite\",\"@id\":\"https://blog.cystack.org/tutorial/#website\",\"url\":\"https://blog.cystack.org/tutorial\",\"name\":\"CyStack Tutorial\",\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https://s2.cystack.net/tutorial/23105817/spring-rest-api.jpg\",\"url\":\"https://s2.cystack.net/tutorial/23105817/spring-rest-api.jpg\",\"width\":\"1200\",\"height\":\"630\",\"caption\":\"Spring REST API\",\"inLanguage\":\"en-US\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://blog.cystack.org/tutorial/2025/09/23/spring-rest-api/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":\"1\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":\"2\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/category/java/\",\"name\":\"Java\"}},{\"@type\":\"ListItem\",\"position\":\"3\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/09/23/spring-rest-api/\",\"name\":\"Spring REST API: H\\u01b0\\u1edbng d\\u1eabn h\\u1ed7 tr\\u1ee3 c\\u1ea3 XML v\\u00e0 JSON\"}}]},{\"@type\":\"WebPage\",\"@id\":\"https://blog.cystack.org/tutorial/2025/09/23/spring-rest-api/#webpage\",\"url\":\"https://blog.cystack.org/tutorial/2025/09/23/spring-rest-api/\",\"name\":\"Spring REST API: H\\u01b0\\u1edbng d\\u1eabn h\\u1ed7 tr\\u1ee3 c\\u1ea3 XML v\\u00e0 JSON - CyStack Tutorial\",\"datePublished\":\"2025-09-23T10:59:14+07:00\",\"dateModified\":\"2025-09-23T10:59:14+07:00\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/#website\"},\"primaryImageOfPage\":{\"@id\":\"https://s2.cystack.net/tutorial/23105817/spring-rest-api.jpg\"},\"inLanguage\":\"en-US\",\"breadcrumb\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/09/23/spring-rest-api/#breadcrumb\"}},{\"@type\":\"Person\",\"@id\":\"https://blog.cystack.org/tutorial/author/codeweaver/\",\"name\":\"Chris Pham\",\"url\":\"https://blog.cystack.org/tutorial/author/codeweaver/\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https://secure.gravatar.com/avatar/7a3584c941fa360ac061b2a8f15684ced7b92daa1f09481be135a684d989e9b9?s=96&d=mm&r=g\",\"url\":\"https://secure.gravatar.com/avatar/7a3584c941fa360ac061b2a8f15684ced7b92daa1f09481be135a684d989e9b9?s=96&d=mm&r=g\",\"caption\":\"Chris Pham\",\"inLanguage\":\"en-US\"}},{\"@type\":\"BlogPosting\",\"headline\":\"Spring REST API: H\\u01b0\\u1edbng d\\u1eabn h\\u1ed7 tr\\u1ee3 c\\u1ea3 XML v\\u00e0 JSON - CyStack Tutorial\",\"keywords\":\"Spring REST API\",\"datePublished\":\"2025-09-23T10:59:14+07:00\",\"dateModified\":\"2025-09-23T10:59:14+07:00\",\"articleSection\":\"Java\",\"author\":{\"@id\":\"https://blog.cystack.org/tutorial/author/codeweaver/\",\"name\":\"Chris Pham\"},\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"description\":\"Spring REST API gi\\u00fap b\\u1ea1n x\\u00e2y d\\u1ef1ng d\\u1ecbch v\\u1ee5 web nhanh ch\\u00f3ng, h\\u1ed7 tr\\u1ee3 JSON v\\u00e0 XML, d\\u1ec5 m\\u1edf r\\u1ed9ng, b\\u1ea3o m\\u1eadt v\\u00e0 t\\u01b0\\u01a1ng th\\u00edch v\\u1edbi nhi\\u1ec1u \\u1ee9ng d\\u1ee5ng.\",\"name\":\"Spring REST API: H\\u01b0\\u1edbng d\\u1eabn h\\u1ed7 tr\\u1ee3 c\\u1ea3 XML v\\u00e0 JSON - CyStack Tutorial\",\"@id\":\"https://blog.cystack.org/tutorial/2025/09/23/spring-rest-api/#richSnippet\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/09/23/spring-rest-api/#webpage\"},\"image\":{\"@id\":\"https://s2.cystack.net/tutorial/23105817/spring-rest-api.jpg\"},\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/09/23/spring-rest-api/#webpage\"}}]}</script>\n"}