このガイドとまったく同じように、Spring でオブジェクトを XML として返そうとしています: http://spring.io/guides/gs/rest-service/
オブジェクトをJSONではなくxmlとして返すことを除いて。
どうすればそれができるか知っている人はいますか?Spring には、XML に対してこれを簡単に実行できる依存関係がありますか? または、マーシャラーを使用してから、別の方法で xml ファイルを返す必要がありますか?
Spring はデフォルトで JSON をサポートしていますが、XML もサポートするには、次の手順を実行します。
@XmlRootElement(name = "response")
@XmlAccessorType(XmlAccessType.FIELD) => this is important, don't miss it.
public class Response {
@XmlElement
private Long status;
@XmlElement
private String error;
public Long getStatus() {
return status;
}
public void setStatus(Long status) {
this.status = status;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}
@RequestMapping(value = "/api", method = RequestMethod.POST, consumes = {"application/xml", "application/json"}, produces = {"application/xml", "application/json"})
公衆
public Response produceMessage(@PathVariable String topic, @RequestBody String message) {
return new Response();
}
Bean で JAXB アノテーションを使用して定義@XmlRootElement
し@XmlElement
、それを xml にマーシャリングする必要がある場合。Spring は、次の場合に Bean を xml にマーシャリングします。
詳細については、このサンプルに従ってください。
http://www.mkyong.com/spring-mvc/spring-3-mvc-and-xml-example/