Spring(002)-创建rest服务
1.创建基于get参数的rest服务
@RestController @RequestMapping("/action") public class GetController { @GetMapping(value = "/get/{id}",produces = "application/json") public Order getAllOrdersByGet(@PathVariable int id) { Order order = new Order(); order.setOrderId(id); order.setOrderName(String.format("order:%d", id)); return order; } }
2.创建向服务器post数据的rest服务
@RestController
@RequestMapping("/action")
public class PostController {
@PostMapping(value = "/save", consumes = "application/json",produces = "application/json")
public Order getAllOrdersByPost(@RequestBody Order order) {
order.setOrderName("orderFromServer");
return order;
}
}
作者:iBrake
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.