---
# System prepended metadata

title: Spring 輸出 XML
tags: [spring, xml]

---

# Spring 輸出 XML

[TOC]

###### tags: `spring` `xml`

---

## Web

- 在 controller method 的 argument(s)裡帶入 `HttpServletResponse`。
- 該 controller method 的 return type 為 `void`，也就是不回傳。

```java
	@GetMapping(path = "/proofOfConcept.xml", produces = MediaType.APPLICATION_XML_VALUE)
	void proofOfConcept(HttpServletResponse response) {
		Document document = DocumentBuilderFactory.
			newDefaultInstance().
			newDocumentBuilder().
			newDocument();
		Element documentElement = document.createElement("document");
		document.appendChild(documentElement);

		// DOM construction omitted for brevity

		TransformerFactory.
			newDefaultInstance().
			newTransformer().
			transform(
				new DOMSource(
					document
				),
				new StreamResult(
					response.getOutputStream()
				)
			);
	}
```