如何将现有的基于 XML 的 Web 服务转换为 JSON 类型的 Web 服务?
我有这个示例资源:
@Controller
public class CustomerController {
@RequestMapping(value="customers", method=RequestMethod.GET)
public @ResponseBody CustomerList customers(Model model) {
CustomerList list = new CustomerList();
list.getCustomer().add(new Customer("1", "John Doe"));
list.getCustomer().add(new Customer("2", "Jane Doe"));
return list;
}
}
到目前为止,我在访问它时没有遇到任何错误,我只想将此服务返回给客户端的数据从 XML 更改为 JSON。
使用此实体:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"customer"
})
@XmlRootElement(name = "CustomerList")
public class CustomerList {
@XmlElement(name = "Customer", required = true)
protected List<Customer> customer;
public List<Customer> getCustomer() {
if (customer == null) {
customer = new ArrayList<Customer>();
}
return this.customer;
}
}
servlet-context.xml:
<oxm:jaxb2-marshaller id="marshaller" contextPath="com.mycompany.api.model"/>
<beans:bean id="customerList" class="org.springframework.web.servlet.view.xml.MarshallingView">
<beans:constructor-arg ref="marshaller"/>
</beans:bean>
如何将服务的输出更改为 JSON?我需要在实体/模型中放置 JSON 类型的注释吗?