Spring MVCを使用してRESTを開発すると、XMLとJSONのデータがサポートされます。私は私の春のコンフィグ豆app-servlet.xml
XMLとJSONをサポートするSpring REST 3
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
p:order="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">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller"
p:autodetectAnnotations="true" />
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
そして、私の春RESTコントローラーでContentNegotiationViewResorverを書いています:
@Controller
@RequestMapping("/rest/customers")
class CustomerRestController {
protected Log log = LogFactory.getLog(CustomerRestController.class);
@RequestMapping(method = POST)
@ResponseStatus(CREATED)
public void createCustomer(@RequestBody Customer customer,
HttpServletResponse response) {
log.info(">>>" + customer.getName());
response.setHeader("Location", String.format("/rest/customers/%s",
customer.getNumber()));
}
@RequestMapping(value = "/{id}", method = GET)
@ResponseBody
public Customer showCustomer(@PathVariable String id) {
Customer c = new Customer("0001", "teddy", "bean");
return c;
}
@RequestMapping(value = "/{id}", method = PUT)
@ResponseStatus(OK)
public void updateCustomer(@RequestBody Customer customer) {
log.info("customer: " + customer.getName());
}
は、私は私の顧客のドメインクラスに@XStreamAlias("customer")
注釈を設定します。 しかし、私はhttp://localhost:8080/rest/customers/teddy.xml
にアクセスしようとすると、いつもJSONデータに応答します。
お客様のドメインドメインに@XmlRootElement(name="customer")
注釈を設定しました。 しかし、私はhttp://localhost:8080/rest/customers/teddy.json
にアクセスしようとすると、常にXMLデータに応答します。
何か問題がありますか?
? – chris
申し訳ありません..、urlは/rest/customers/teddy.xmlです。このurlはshowCustomerメソッドを呼び出すと想定しています。テディは{id}パラメータです。 –
どのようにそのURLにアクセスしようとしていますか?ウェブブラウザ?要求に適切なコンテンツタイプのエンコーディングヘッダーを送信していますか? – skaffman