Có, điều này có thể được thực hiện. Dưới đây là một ví dụ đơn giản (với chú thích mùa xuân) của một điều khiển RESTful:
@Controller
@RequestMapping("/someresource")
public class SomeController
{
@Autowired SomeService someService;
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public String getResource(Model model, @PathVariable Integer id)
{
//get resource via someService and return to view
}
@RequestMapping(method=RequestMethod.POST)
public String saveResource(Model model, SomeResource someREsource)
{
//store resource via someService and return to view
}
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
public String modifyResource(Model model, @PathVariable Integer id, SomeResource someResource)
{
//update resource with given identifier and given data via someService and return to view
}
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteResource(Model model, @PathVariable Integer id)
{
//delete resource with given identifier via someService and return to view
}
}
Lưu ý rằng có nhiều cách để xử lý các dữ liệu đến từ http-request (@RequestParam, @RequestBody, lập bản đồ tự động sau khi tham số để một chiếc đậu vv). Để có các giải thích và hướng dẫn dài hơn và có thể tốt hơn, hãy thử googling cho một cái gì đó như 'rest spring mvc' (không có dấu ngoặc kép).
Thông thường, trình khách (trình duyệt) được thực hiện với JavaScript và AJAX, tôi là một lập trình viên máy chủ phụ trợ và không biết nhiều về JavaScript, nhưng có rất nhiều thư viện có sẵn để trợ giúp, ví dụ thấy jQuery
xem thêm: REST in Spring 3 MVC
đây trông giống như một ví dụ điển hình: http://www.stupidjavatricks.com/?p=54 – bwobbones
@bwobbones, bạn nên chuyển đổi bình luận của bạn trong một câu trả lời;) –
@RC, không muốn yêu cầu công việc của người khác ... – bwobbones