Si desarrollamos REST utilizando Spring MVC, admitirá datos XML y JSON. He escribió ContentNegotiationViewResorver en mi primavera config frijol app-servlet.xml
Spring REST 3 para admitir XML y JSON
<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>
Y mi primavera RESTO controlador es:
@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());
}
puse @XStreamAlias("customer")
anotación en mi clase de dominio del cliente. Pero cuando intento acceder a http://localhost:8080/rest/customers/teddy.xml
siempre responden datos JSON.
Anoté @XmlRootElement(name="customer")
anotación en mi clase de dominio de cliente. Pero cuando intento acceder http://localhost:8080/rest/customers/teddy.json
siempre responden datos XML.
¿Hay algo raro?
Dónde está app/clientes/teddy.xml asignada en el controlador? – chris
lo siento ..., la url es: /rest/customers/teddy.xml, esta url supone invocar el método ShowCustomer. y teddy es el parámetro {id}. –
¿Cómo está intentando acceder a esa URL? Un navegador web? ¿Está enviando el encabezado de codificación de tipo de contenido apropiado en la solicitud? – skaffman