Spring JSON Controller 의 전형적인 예제 입니다. @ResponseBody Map 을 이용하여 리턴 해주는 방법이 인상적입니다.
(굳이 도메인 클래스를 리턴하지 않고 이런 방식을 처리하는게 생산성이나 유지보수 성능이 좋습니다)
출처는 : ...
@RequestMapping(value = "/add", produces = "application/json") public @ResponseBody MapaddJson(HttpServletRequest request, HttpServletResponse response, Model model, @ModelAttribute("addToCartItem") AddToCartItem addToCartItem) throws IOException, PricingException, AddToCartException { Map responseMap = new HashMap (); try { super.add(request, response, model, addToCartItem); if (addToCartItem.getItemAttributes() == null || addToCartItem.getItemAttributes().size() == 0) { responseMap.put("productId", addToCartItem.getProductId()); } responseMap.put("productName", catalogService.findProductById(addToCartItem.getProductId()).getName()); responseMap.put("quantityAdded", addToCartItem.getQuantity()); responseMap.put("cartItemCount", String.valueOf(CartState.getCart().getItemCount())); if (addToCartItem.getItemAttributes() == null || addToCartItem.getItemAttributes().size() == 0) { // We don't want to return a productId to hide actions for when it is a product that has multiple // product options. The user may want the product in another version of the options as well. responseMap.put("productId", addToCartItem.getProductId()); } } catch (AddToCartException e) { if (e.getCause() instanceof RequiredAttributeNotProvidedException) { responseMap.put("error", "allOptionsRequired"); } else { throw e; } } return responseMap; }
아래는 기타...
@RequestMapping(value = "/add", produces = { "text/html", "*/*" }) public String add(HttpServletRequest request, HttpServletResponse response, Model model, @ModelAttribute("addToCartItem") AddToCartItem addToCartItem) throws IOException, PricingException, AddToCartException { try { return super.add(request, response, model, addToCartItem); } catch (AddToCartException e) { Product product = catalogService.findProductById(addToCartItem.getProductId()); return "redirect:" + product.getUrl(); } } @RequestMapping("/updateQuantity") public String updateQuantity(HttpServletRequest request, HttpServletResponse response, Model model, @ModelAttribute("addToCartItem") AddToCartItem addToCartItem) throws IOException, PricingException, UpdateCartException, RemoveFromCartException { return super.updateQuantity(request, response, model, addToCartItem); } @RequestMapping("/remove") public String remove(HttpServletRequest request, HttpServletResponse response, Model model, @ModelAttribute("addToCartItem") AddToCartItem addToCartItem) throws IOException, PricingException, RemoveFromCartException { return super.remove(request, response, model, addToCartItem); } @RequestMapping("/empty") public String empty(HttpServletRequest request, HttpServletResponse response, Model model) throws PricingException { //return super.empty(request, response, model); return "ajaxredirect:/"; } @RequestMapping("/promo") public String addPromo(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam("promoCode") String customerOffer) throws IOException, PricingException { return super.addPromo(request, response, model, customerOffer); } @RequestMapping("/promo/remove") public String removePromo(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam("offerCodeId") Long offerCodeId) throws IOException, PricingException { return super.removePromo(request, response, model, offerCodeId); }