これは構成からのみ取得でき、特別なプログラムを作成する必要はありません。方法は次のとおりです。最初に構成を正しく設定します。Jackson + JAXB を使用し、どちらも ContentNegotiatingViewResolver Bean の下に設定します。
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1"/>
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<oxm:jaxb2-marshaller id="marshaller">
<oxm:class-to-be-bound name="com.shay.dashboard.data.structure.page.PageObject" />
<oxm:class-to-be-bound name="com.shay.dashboard.data.structure.tab.TabObject" />
<oxm:class-to-be-bound name="com.shay.dashboard.data.structure.section.SectionObject" />
<oxm:class-to-be-bound name="com.shay.dashboard.data.structure.element.nonembedded.ElementObject"/>
<oxm:class-to-be-bound name="com.shay.dashboard.data.structure.element.embedded.EmbeddedElementObject"/>
<oxm:class-to-be-bound name="com.shay.dashboard.data.structure.chart.common.ChartManager"/>
</oxm:jaxb2-marshaller>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</list>
</property>
</bean>
マーシャラーの下に oxm:class-to-be-bound を設定したことに注目してください。これらは JAXB によってバインドされるクラスです。
モジュールには、マーシャラー固有ではない通常の注釈パッケージ (javax.xml.bind.annotation) を使用しました。Jackson Json と JAXB はどちらもそれを読み取る方法を知っています。
例えば:
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="page")
public class PageObject implements ComponentTypeObject{
@XmlAttribute(name="name")
private String name;
@XmlAttribute(name="id",required=true)
private String id;
@XmlElements({@XmlElement(name="tab", type=TabXmlAdapter.class)})
private List<TabXmlAdapter> tabRef;
最後に、MVC のコントローラーはモデルとビューを返す必要があります。
@RequestMapping(value="/get_page", method = RequestMethod.GET)
public ModelAndView initPage()
{
ModelAndView mav = null;
try
{
PageObject myPage = (PageObject) Utilities.getUtilities().loadObjectFromFile(XmlComponentType.page);
mav = new ModelAndView("page","page",myPage);
}
catch (Exception e)
{
e.getMessage();
}
return mav;
}
.json で終わる URL を呼び出すと、JSON 表現と .xml および XML を取得できます。モジュールに注釈を付けるときに正しいマッピングを指定した場合、両方ともビューアーによって翻訳されます。